Excel:触发后无法自动更新

时间:2018-12-22 17:58:44

标签: excel vba

我有一个单元格A1,每隔 n 秒从服务器提取值,但是不适合使用下面的宏(当前使用):

Dim preVal As String
Dim count As Integer

'Intention is if cell A1 changes, record changes to Column C and Column D
Private Sub Worksheet_Change(ByVal Target As Range)
        If Target = Range("A1") Then
                Call cellchange(Range("A1"))
        End If
End Sub

Private Sub cellchange(ByVal a As Range)
   'If row is empty, filled into that row, if not skip to next one  
    If a.Value <> preVal Then
        count = count + 1
'copy the value of A1 from sheet 1 
        preVal = Sheets("Sheet1").Range("A1").Value
        Cells(count, 4).Value = a.Value
 'copy the values of time of which data change detected       
        Cells(count, 3) = Now()
    End If
End Sub

以最简单的方式,将每隔几秒钟从服务器更新单元格A1,因此当我检测到不是来自人工输入的单元格A1中的更改​​时,我需要对宏进行更新/触发。

1 个答案:

答案 0 :(得分:1)

您需要使用一些可以真正检查目标单元格是否已更新的东西。通常使用application.intersect。我在这里使用地址属性。

Dim preVal As String
Dim count As Integer

'Intention is if cell A1 changes, record changes to Column C and Column D
Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.address = Range("A1").address Then
                cellchange target 
        End If
End Sub

Private Sub cellchange(ByVal a As Range)
   'If row is empty, filled into that row, if not skip to next one  
    If a.Value <> preVal Then
        count = count + 1
'copy the value of A1 from sheet 1 
        preVal = Sheets("Sheet1").Range("A1").Value
        Cells(count, 4).Value = a.Value
 'copy the values of time of which data change detected       
        Cells(count, 3) = Now
    End If
End Sub

希望有帮助。

致谢,
M