excel – 如果一个工作表中的列被更新,则更新多个列

我有多个工作表,所有工作表中的一列包含相同的列(称为“名称”)。 在每个列中的“名称”是相应的“状态”列。 每个“名称”列可以包含一张或多张相同的logging。 如果“状态”列中的一条logging被更新,我想获得其相应的“名称”列值,然后search所有的表,如果它也有相同的“名称”logging,我应该能够更新其对应的“状态”值基于来自第一个“状态”更新的值。

现在,我可以从正在工作的networkingsearch一个答案(见下文),问题是只有一种方法。 我可以从工作表更新值(比如sheet1),并且可以从一个或多个工作表中更新值。 但是,当我试图从另一个工作表(而不是sheet1)更新值时,它返回一个错误Method "Find' of object 'Range' failed

示例(这应该在每个工作表中,列,表名更改):

 Private Sub Worksheet_Change(ByVal Target As Range) If Target.Cells.Count > 1 Then Exit Sub Dim fn2 As Range If Not Intersect(Target, Range("B:B")) Is Nothing Then Set fn2 = Sheets("Sheet1").Range("D:D").Find(Target.Offset(, -11).Value, , xlValues, xlWhole) If Not fn2 Is Nothing Then fn2.Offset(, 13) = Target.Value MsgBox "A record has been updated name_tab tab" End If End If End Sub 

请帮忙。 谢谢。

你的代码错误,因为你想抵消列D左边的11列

在OP澄清之后编辑

  • 待被search的表格名称被硬编码,因为没有常见的模式可以被发现在所有其他工作表中被过滤

  • NamesStatus栏相应地在每个待search工作表的第1行中具有“名称”和“状态”标题

edit2考虑到第一个更新行索引(见“****”行)

将此代码放在ThisWorkbook代码窗格中:

 Option Explicit Const STATUSHEADER As String = "Status" Const NAMESHEADER As String = "Names" Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) Dim nameValue As String Dim sht As Worksheet Dim sheetNames As Variant Dim firstUpdatedRowIndex As Long '<--| this will store first updated row index **** If Not Proceed(Target) Then Exit Sub sheetNames = Array("Sheet0001", "Sheet0002", "Sheet0003") '<--| fill array with sheets-to-be-searched names On Error GoTo EXITSUB Application.EnableEvents = False nameValue = GetNameValue(Target) '<--| get value in the "Names" column corresponding row For Each sht In Worksheets(sheetNames) '<--| loop through to-be-searched-in sheets If sht.name <> Sh.name Then UpdateSheet sht, nameValue, Target.value, firstUpdatedRowIndex '**** Next sht EXITSUB: Application.EnableEvents = True End Sub Sub UpdateSheet(sht As Worksheet, nameValue As String, statusValue As Variant, firstUpdatedRowIndex As Long) '**** Dim namesCol As Long, statusCol As Long Dim f As Range With sht '<--| refer to current to-be-searched-in sheet With .Rows(1).SpecialCells(XlCellType.xlCellTypeConstants) '<--| refer to its 1st row non blank cells range namesCol = FindFirstCell(NAMESHEADER, .Cells).Column '<--| get its "Names" column index statusCol = FindFirstCell(STATUSHEADER, .Cells).Column '<--| get its "Status" column index End With Set f = FindFirstCell(nameValue, .Columns(namesCol).SpecialCells(XlCellType.xlCellTypeConstants)) '<--| search its "names" column for the "name" value corresponding to the changed "Status" in the originally changed sheet If Not f Is Nothing Then '<--| if any matching cell found in "Names" column ... .Cells(f.row, statusCol) = statusValue '<--| update its corresponding "Status" column value MsgBox "record in row " & f.row & " has been updated in " & .name & " tab" If firstUpdatedRowIndex = 0 Then firstUpdatedRowIndex = f.row '<--| store first updated row **** End If End With End Sub Function FindFirstCell(value As String, rng As Range) As Range Set FindFirstCell = rng.Find(what:=value, lookat:=xlWhole, LookIn:=xlValues, MatchCase:=True) End Function Function GetNameValue(Target As Range) As String With Target.Parent GetNameValue = .Cells(Target.row, FindFirstCell(NAMESHEADER, .Range(.Cells(1, 1), .Cells(1, Target.Column - 1))).Column).value '<--| search columns preceeding the 'Target' (ie: "Status") one for the "Names" column and get its value at the row corresponding to changed "Status" one End With End Function Function Proceed(rng As Range) As Boolean Proceed = rng.Cells.Count = 1 '<--| proceed if only one changed cell If Proceed Then Proceed = rng.Offset(-rng.row + 1) = STATUSHEADER '<--| proceed only if the changed cell is a "Status" column End Function