excel - 在特定单元格更改时捕获数据

标签 excel vba

当某些单元格发生变化时,我正在尝试从特定单元格中捕获数据:

当单元格 B5 更改时,我想在 Sheet2 的 A 列和 B 列中捕获单元格 B3 和 B4 中的数据。

当单元格 C5 更改时,我还希望能够在 C 和 D Sheet2 列中的单元格 C3 和 C4 中捕获数据。

宏确实会执行此操作 - 但是,当 B5 或 C5 更改时,它会从 B 和 C 列中捕获数据 - 而不是当 B5 更改时仅捕获单元格 B3 和 B4 中的数据,而当 C5 仅更改来自单元格 C3 和 C4 中的数据时。

非常感谢任何帮助 - 这是我目前的代码:

    Sub Worksheet_Calculate()
Worksheet_Change Range("B5:C5")
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)

'Capture data when cell B5 changes
  If Not Intersect(Target, Range("B5")) Is Nothing Then
Application.EnableEvents = False
Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Value = Range("B3").Value
Application.EnableEvents = True
Application.EnableEvents = False
Sheets("Sheet2").Range("B" & Rows.Count).End(xlUp).Offset(1, 0).Value = Range("B4").Value
Application.EnableEvents = True

 End If

'Capture data when cell C5 changes
         If Not Intersect(Target, Range("C5")) Is Nothing Then
Application.EnableEvents = False
Sheets("Sheet2").Range("C" & Rows.Count).End(xlUp).Offset(1, 0).Value = Range("C3").Value
Application.EnableEvents = True
Application.EnableEvents = False
Sheets("Sheet2").Range("D" & Rows.Count).End(xlUp).Offset(1, 0).Value = Range("C4").Value
Application.EnableEvents = True



  End If

End Sub

最佳答案

这似乎对我有用..

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim oWS As Worksheet: Set oWS = ThisWorkbook.Worksheets("Sheet2")

    Application.EnableEvents = False
    With oWS
        If Not Intersect(Range("B5"), Target) Is Nothing Then
            .Range("A" & .Rows.count).End(xlUp).Offset(1, 0).Value = .Range("B3").Value
            .Range("B" & .Rows.count).End(xlUp).Offset(1, 0).Value = .Range("B4").Value
        ElseIf Not Intersect(Range("C5"), Target) Is Nothing Then
            .Range("C" & .Rows.count).End(xlUp).Offset(1, 0).Value = .Range("C3").Value
            .Range("D" & .Rows.count).End(xlUp).Offset(1, 0).Value = .Range("C4").Value
        End If
    End With
    Application.EnableEvents = True
End Sub

编辑

When the cell value is changed by a formula



如果您希望在公式更改单元格值时更新值,则方法不同:

首先,在您的 Module ,声明公共(public)变量:
Public sB5Value As String
Public sC5Value As String

然后,在 Workbook_Open捕获单元格的当前值
Private Sub Workbook_Open()
    With Thisworkbook.Worksheets("Sheet2")
        sB5Value = .Range("B5").Value
        aC5Value = .Range("C5").Value
    End With
End Sub

现在在您的Worksheet (我假设是 Sheet2 )计算功能,您可以执行检查
Private Sub Worksheet_Calculate()
   With Thisworkbook.Worksheets("Sheet2")
       ' B5 change check
       If .Range("B5").Value <> sB5Value Then
           .Range("A" & .Rows.count).End(xlUp).Offset(1, 0).Value = .Range("B3").Value
           .Range("B" & .Rows.count).End(xlUp).Offset(1, 0).Value = .Range("B4").Value

           ' We have to update the value for B5 in the variable
           sB5Value = .Range("B5").Value
       End If

       ' C5 change check
       If .Range("C5").Value <> sC5Value Then
           .Range("C" & .Rows.count).End(xlUp).Offset(1, 0).Value = .Range("C3").Value
           .Range("D" & .Rows.count).End(xlUp).Offset(1, 0).Value = .Range("C4").Value

           ' We have to update the value for C5 in the variable
           sC5Value = .Range("C5").Value
       End If
    End With
End Sub

注意:我留下了原始代码以防万一
您也可以制作 IF条件不区分大小写(即使用 LCase UCase ),但我会把它留给你。我还没有测试过这段代码,但这个想法应该可行

关于excel - 在特定单元格更改时捕获数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49560549/

相关文章:

通过表单中的代码选择后,Excel 无法正确激活单元格

vba - Excel 宏用于查找和替换任何文本文件中的多个字符串

vba - Excel VBA - 多个子对不同的项目做同样的事情

excel - 如果单元格内容与列表值不匹配,则删除工作表

excel - 按多个标准和从列中的最新日期排序/查找数据

python - 在 python 中 reshape 多头表

c# - 当用户取消文件覆盖时出现 Excel COMException 0x800A03EC

excel - 使用 VBA 和 IE 解析 Internet HTML 页面的数据

excel - 如何根据条件自定义图标

javascript - SheetJS xlsx-style需要excel中的单元格样式