如何在Excel中编辑通过宏代码显示的单元格中的信息?

发布于 2024-10-24 00:43:57 字数 680 浏览 2 评论 0原文

我有一个宏,这样当您突出显示sheet1上的一行时,该宏就会获取该行中的所有信息,并将其单独显示在sheet2上。如果突出显示sheet1上的不同行,sheet2上的信息将更改以显示该行的信息。

我的问题是,如果我更改sheet2上显示的信息,它不会更改sheet1上的信息。有什么办法可以添加这个功能吗?

我现在有以下代码:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim myList
If Target.Address <> Target.EntireRow.Address 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

任何帮助都会很棒! :)

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.Address 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! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伊面 2024-10-31 00:43:57

将sheet1行复制到sheet2后,您还可以记录值来自的原始行#。然后,您可以添加一个额外的宏,将sheet2 值与sheet1 中的值进行比较——然后可以迁移任何更改。

一个可能的基本流程:

  • 将sheet1行复制到sheet2(当前宏)
  • 将sheet1行#复制到sheet2(即向下一行)
  • 在sheet2上进行更改
  • 将sheet2行复制到sheet1行(使用sheet2上保存的行#)->这假设不对sheet1 进行任何更改。

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.
我不吻晚风 2024-10-31 00:43:57

您当前正在使用 Worksheet_SelectionChange 事件宏来识别何时完成已选择单行。您需要一个 Worksheet_Change 事件宏,以便 Sheet2 识别何时B1:B15 范围已更改并将更改传递回 Sheet1。

由于 Worksheet_Change 是在值更改时触发的,因此您需要禁用 Application.EnableEvents 属性,以便当您从 Sheet1 的 Worksheet_SelectionChange 子项写入值时不会触发该属性。

您将需要几个公共变量。一个用于记住应返回更改的位置,另一个用于定位 Sheet2 上的目标单元格。这些只能在模块代码表中公开。

Book1 - Module1(代码)

Option Explicit

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

我对原始 Worksheet_SelectionChange 进行了一些小修改,并添加了禁用事件处理的功能。

Book1 - Sheet1(代码)

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

工作表.CodeName property 用于标识 Sheet2,因为如果按惯例重命名工作表,该属性不会改变。

目前还不清楚您计划如何识别值更改后要返回到的行。我使用了 Module1 中声明的公共范围类型变量来记录值从 Sheet1 传输到 Sheet2 的最后位置。 Sheet2 上的更改会将它们返回到最后记录的位置。

Book1 - Sheet2(代码)

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

请注意,“记住”的位置仅位于内存中。关闭并重新打开工作簿可以有效地将其“归零”。除非您从 Sheet1 中重新加载了值,否则请勿对 Sheet2 进行更改。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文