开发者

How to edit info in cells displayed via macro code in Excel?

开发者 https://www.devze.com 2023-02-17 11:52 出处:网络
I have a macro so that when you highlight a row on sheet1, the macro takes all the info from this row and displays this by itself on sheet2. If you highlight a different row on sheet1, the info on she

I have a macro so that when you highlight a row on sheet1, the macro takes all the info from this row and displays this by itself on sheet2. If you highlight a different row on sheet1, the info on sheet2 is changes to show the info from that row.

My problem is that if I change the info displayed on sheet2, it doesn't change the info on sheet1. Is there a way I could add this functionality?

I have the following code at the moment:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim myList
If Target.Address <> Target.EntireRow.Ad开发者_如何学运维dress Then Exit Sub
If Target.Rows.Count > 1 Then Exit Sub
myList = [{"B1","B2","B3","B4","B5","B6","B7","B8","B9","B10","B11","B12","B13","B14","B15"}] '<- adjust to your need
With Target.EntireRow
    For i = 1 To UBound(myList)
        Sheets("sheet2").Range(myList(i)).Value = .Cells(i).Value
    Next
End With
End Sub

Any Help would be awesome! :)


After copying your sheet1 row to sheet2 you could also record the original row # that the values came from. Then you can add an additional macro that would compare the sheet2 values with the values in sheet1 - any changes could then be migrated over.

A possible basic flow:

  • copy sheet1 row to sheet2 (current macro)
  • copy sheet1 row # to sheet2 (ie one row down)
  • make changes on sheet2
  • copy sheet2 row to sheet1 row (use row # saved on sheet2) -> this assumes that no changes will be made to sheet1.


You are currently using a Worksheet_SelectionChange event macro to recognize when a full single row has been selected. You need a Worksheet_Change event macro for Sheet2 to recognize when values in the B1:B15 range have been changed and pass the changes back to Sheet1.

Because the Worksheet_Change is triggered on a change in values, you will need to disable the Application.EnableEvents property so that it is not triggered when you write the values from Sheet1's Worksheet_SelectionChange sub.

You are going to require a couple of public variables. One to remember the position that changes should be returned to and another to locate the target cells on Sheet2. These can only be made public in a module code sheet.

Book1 - Module1 (Code)

Option Explicit

Public Const sRNG As String = "B1:B15"
Public rRNG As Range

I've made a couple of small modifications to your original Worksheet_SelectionChange and added the disabling of event handling.

Book1 - Sheet1 (Code)

Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Cells.Count = Columns.Count And Target.Rows.Count = 1 And _
      CBool(Application.CountA(Target)) Then   '<~~ one complete non-blank row 
        On Error GoTo bm_Safe_Exit
        Application.EnableEvents = False
        With Sheet2.Range(sRNG)
            Set rRNG = Target.Cells(1, 1).Resize(.Columns.Count, .Rows.Count)
            .Cells = Application.Transpose(rRNG.Value)
        End With
    End If

bm_Safe_Exit:
    Application.EnableEvents = True
End Sub

The Worksheet .CodeName property was used to identify Sheet2 since this does not change if the worksheet is conventionally renamed.

It is a little unclear on how you were planning to identify the row to return the values to once they were changed. I've used a public range-type variable declared in Module1 to record the last location that values were transferred from Sheet1 to Sheet2. Changes on Sheet2 will return them to the last recorded location.

Book1 - Sheet2 (Code)

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range(sRNG)) Is Nothing Then
        Debug.Print rRNG.Address(0, 0, external:=True)
        On Error GoTo bm_Safe_Exit
        Application.EnableEvents = False
        rRNG = Application.Transpose(Range(sRNG).Value)
    End If

bm_Safe_Exit:
    Application.EnableEvents = True
End Sub

Note that the 'remembered' location is in memory only. Closing and reopening the workbook effectively 'zeroes' it. Do not make changes on Sheet2 unless you have freshly loaded values from Sheet1.

0

精彩评论

暂无评论...
验证码 换一张
取 消