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

守給你的承諾、 提交于 2019-12-02 03:27:28

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!