Worksheet_change代码用于不同纸张,并在一张纸上的数据

发布于 2025-02-13 23:34:58 字数 600 浏览 0 评论 0原文

我在30张列表中列出了下拉列表,其中C列是下拉列表。根据所选标准,在D列中粘贴了另一个值。

我创建的代码仅适用于我的数据所在的表格。我想根据我的“数据”表在文件中的每个电子表格上执行代码。

Private Sub Worksheet_Change(ByVal Target As Range)
Dim Res As Variant
If Target.CountLarge > 1 Then Exit Sub
If Not Intersect(Target, Range("c6:c10")) Is Nothing Then
Res = Evaluate("INDEX(b2:b63,MATCH(" & Target.Address & ",A2:a63,0))").
If Not IsError(Res) Then Target.Offset(, 1) = Res
End If
End Sub

该代码与我的值一起在页面上起作用。用我的数据从表格中制作代码是什么,适用于文件上的其余表?

也许select.worksheet.data在我的代码线之一或类似行之前?

I'm making a dropdown list on 30 sheets where column C is the dropdown list. Based on the selected criteria another value is pasted in column D.

I created code that only works for the sheet where my data is located. I want to execute my code on every spreadsheet in the file based on my ‘Data’ sheet.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim Res As Variant
If Target.CountLarge > 1 Then Exit Sub
If Not Intersect(Target, Range("c6:c10")) Is Nothing Then
Res = Evaluate("INDEX(b2:b63,MATCH(" & Target.Address & ",A2:a63,0))").
If Not IsError(Res) Then Target.Offset(, 1) = Res
End If
End Sub

The code works on the page with my values. What do make the code from the sheet with my data apply to rest of the sheets on the file?

Maybe a select.worksheet.data before one of my lines of code or something like that?

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

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

发布评论

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

评论(1

心的憧憬 2025-02-20 23:34:58

如果您希望上述代码在所有其他纸张中都可以使用(更改特定表的“ C6:C10”范围中的某些内容,以在“ D:D”列中带来res相应的工作表),请在thisWorkBook代码模块中复制下一个代码事件(并从表中删除/评论现有的代码):

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
  Dim Res As Variant
  If Sh.name <> "MySheet1" Or Sh.name <> "MySheet2" Then 'sheet to be excepted (if the case)
     If Target.CountLarge > 1 Then Exit Sub
       If Not Intersect(Target, Sh.Range("c6:c10")) Is Nothing Then
            Res = Evaluate("INDEX(" & Sh.Range("B2:B63").Address(external:=True) & _
                     ",MATCH(" & Target.Address & "," & Sh.Range("A2:A63").Address(external:=True) & ",0))")
       If Not IsError(Res) Then Target.Offset(, 1) = Res
    End If
 End If
End Sub

但是,如果所有范围都有同步范围床单,需要复制与事件触发的表格中返回的res,请下一步改编您的代码:

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
 Dim Res As Variant, ws As Worksheet
 If Target.CountLarge > 1 Then Exit Sub
    If Not Intersect(Target, Range("c6:c10")) Is Nothing Then
        Res = Evaluate("INDEX(b2:b63,MATCH(" & Target.Address & ",A2:a63,0))")
    If Not IsError(Res) Then Target.Offset(, 1) = Res
 
    For Each ws In Me.Parent.Worksheets 'place the same Res in the correspondent cell from all sheets
        If ws.name <> "MySheet1" Or ws.name <> "MySheet2" Or ws.name <> Me.name Then  'except some sheets if necessary
           ws.Range(Target.Address).Offset(, 1).value = Res
        End If
    Next
 End If
End Sub

请,请在之后发送一些反馈测试看起来很方便的解决方案。

If you want the above code to work the same in all the other sheets (changing something in "C6:C10" range of a specific sheet to bring Res in column "D:D" of the respective sheet), please copy the next code event in ThisWorkbook code module (and delete/comment the existing one from the sheet):

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
  Dim Res As Variant
  If Sh.name <> "MySheet1" Or Sh.name <> "MySheet2" Then 'sheet to be excepted (if the case)
     If Target.CountLarge > 1 Then Exit Sub
       If Not Intersect(Target, Sh.Range("c6:c10")) Is Nothing Then
            Res = Evaluate("INDEX(" & Sh.Range("B2:B63").Address(external:=True) & _
                     ",MATCH(" & Target.Address & "," & Sh.Range("A2:A63").Address(external:=True) & ",0))")
       If Not IsError(Res) Then Target.Offset(, 1) = Res
    End If
 End If
End Sub

But if there are synchronized ranges in all sheets and need to copy the same returned Res from the sheet having the event triggered, please adapt your code in the next way:

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
 Dim Res As Variant, ws As Worksheet
 If Target.CountLarge > 1 Then Exit Sub
    If Not Intersect(Target, Range("c6:c10")) Is Nothing Then
        Res = Evaluate("INDEX(b2:b63,MATCH(" & Target.Address & ",A2:a63,0))")
    If Not IsError(Res) Then Target.Offset(, 1) = Res
 
    For Each ws In Me.Parent.Worksheets 'place the same Res in the correspondent cell from all sheets
        If ws.name <> "MySheet1" Or ws.name <> "MySheet2" Or ws.name <> Me.name Then  'except some sheets if necessary
           ws.Range(Target.Address).Offset(, 1).value = Res
        End If
    Next
 End If
End Sub

Please, send some feedback after testing the solution which looks convenient for you.

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