我有需要更改的公式,这些公式具有输出和解决方案。我创建了整行A46:CV42
,该行显示了每个单元格中每个受影响的值(公式解决方案)。我需要的是,每当任何单元格值发生更改时,就会自动将对整个行的每次更改记录下来。可以重复使用它在它下面的同一张纸上,也可以在另一张纸上。
我已经尝试了一些发现的东西,但是它们都处理要更改的单个单元格或当您手动更改数据时。
我尝试过的例子。我是该领域的新手。
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$2" Then
a = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row + 1
Sheets("Sheet2").Range("A" & a).Value = Sheets("Sheet1").Range("A").Value
End If
End Sub
答案 0 :(得分:0)
我不太确定您要第46
行还是第42
行。请在下面看到一种方法,并在代码中注释以获取更多详细信息:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim wb As Workbook: Set wb = ThisWorkbook
Dim wsWatch As Worksheet: Set wsWatch = wb.Sheets("Sheet1") 'declare and set the sheet (change the name or use Target.Worksheet instead as needed)
Dim rngWatch As Range: Set rngWatch = wsWatch.Range("A46:CV46") 'declare and set the range to watch over
Dim arrWatch As Variant: arrWatch = rngWatch 'allocate the range to an array
Dim wsHistory As Worksheet: Set wsHistory = wb.Sheets("Sheet2") 'declare and set the sheet
With wsHistory
Dim lRow As Long: lRow = .Cells(.Rows.Count, "A").End(xlUp).Row 'get the last row in the history
Dim rngHistory As Range: Set rngHistory = .Range(.Cells(lRow, 1), .Cells(lRow, 100)) 'declare and set the range of the last populated row (last historic change)
End With
Dim arrHistory As Variant: arrHistory = rngHistory 'allocate the range to an array
Dim C As Long
'Only one row in the arrays, let's loop over the columns
For C = LBound(arrWatch, 2) To UBound(arrWatch, 2) 'for each column in the ranges
If arrWatch(1, C) <> arrHistory(1, C) Then 'if there is a mismatch
rngHistory.Offset(1) = rngWatch.Value 'allocate the values in the next free row
Exit For 'exit here if mismatch found
End If
Next C
End Sub
这可能不是理想的解决方案,但是我期待看到其他解决方案...同时,我认为它可以满足您的要求。