简体   繁体   English

如何根据另一个工作表中的值填充不同工作表中的单元格,反之亦然

[英]How to populate a cell in a different sheet based on the value in another sheet and vice versa

I am trying to use VBA so that I can input a value in cell B7 in sheet2 and then it would automatically populate in C7 in sheet3 and also work vice versa.我正在尝试使用 VBA 以便我可以在 sheet2 的单元格 B7 中输入一个值,然后它会自动填充到 sheet3 的 C7 中,反之亦然。 I tried the code below and couldn't get it to work, any suggestions?我尝试了下面的代码,但无法正常工作,有什么建议吗? Also would the code be the same for a string of a number?对于一串数字,代码也会相同吗?

Private Sub Worksheet_Change(ByVal Target As Range)
  On Error GoTo eh
  If Not Intersect(Target, ThisWorkbook.Sheets("sheet 2").Range("B7")) Is Nothing Then
    Application.EnableEvents = False
    ThisWorkbook.Sheets("sheet 3").Range("C" & Target.Row - 0).Value = Target.Value
eh:
Application.EnableEvents = True
    If Err <> 0 Then MsgBox Err & " " & Err.Description, , "Error in Worksheet_Change event, sheet 2"
  End If
  
End Sub

A Workbook SheetChange: Same Value in Cells of Worksheets工作簿 SheetChange:工作表单元格中的相同值

  • Note that the code needs to be copied to the ThisWorkbook module.请注意,需要将代码复制到ThisWorkbook模块。
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
     
    Dim wsNames As Variant: wsNames = VBA.Array("sheet 2", "sheet 3")
    Dim CellAddresses As Variant: CellAddresses = VBA.Array("B7", "C7")
     
    Dim iCell As Range
    Dim n As Long
    
    For n = 0 To UBound(wsNames)
        If StrComp(Sh.Name, wsNames(n), vbTextCompare) = 0 Then
            Set iCell = Intersect(Sh.Range(CellAddresses(n)), Target)
            If Not iCell Is Nothing Then
                Application.EnableEvents = False
                    Me.Worksheets(wsNames(1 - n Mod 2)) _
                        .Range(CellAddresses(1 - n Mod 2)).Value = iCell.Value
                Application.EnableEvents = True
            End If
            Exit For
        End If
    Next n

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据列中的值填充其他工作表中的excel单元格值 - Populate excel cell value from a different sheet based on value in a column 如何根据另一张表中各列的条目填充单元格内容 - How to populate cell contents based on entries in the columns from the another sheet 根据另一个工作表单元格值的值更改工作表中的单元格值 - Change a cell value in a sheet based on the value of another sheet cell value 在Excel中,如何根据一个工作表中的行与另一工作表中的列之间的匹配来填充单元格 - In Excel, how to populate cell based on match between row in one sheet and column in another sheet 如何根据另一个工作表上的工作表值将图片添加到另一个工作表? - How to add a picture to another sheet based on the sheet value on another sheet? 基于另一张纸上的值(日期)的颜色单元格 - colour cell based on value (date) on another sheet Excel:基于另一个工作表中的单元格值动态更改工作表标签 - Excel: Dynamically change sheet tag based on cell value in another sheet VBA根据另一个工作表的单元格值选择工作表 - VBA to select sheet based on cell value of another sheet 宏可根据不同工作表中的值自动填充单元格 - Macro to autofill a cell based on a value in a different sheet 根据单元格中的值将行复制到另一张工作表 - Copy row to another sheet based on value in a cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM