在按钮上识别更改的单元格单击

时间:2016-08-12 14:42:57

标签: excel vba excel-vba

单击Excel工作表中的按钮,我想知道已更改的单元格。

我已经将POST的宏写入了将返回值的API的更改值。我不想在VBA中进行此计算,因为它不满足我的其他应用程序的要求。

到目前为止我目前的职能,

Sub MyChangeSub()
    'Get value/s and name of cell/s that has/have changed
    Dim query, request As String, result As String
    request = "{ ""cell"":""" & cell_name & """, ""value"":""" & value & """ }"

    Dim xmlHttp As Object
    Set xmlHttp = CreateObject("MSXML2.ServerXMLHTTP")
    xmlHttp.Open "POST", URL, False
    xmlHttp.setRequestHeader "Content-Type", "application/json"
    xmlHttp.send (request)

    result = xmlHttp.responseText
    Set xmlHttp = Nothing
    ' Doing something with the result and update the values
End Sub

如何获取已更改的任何单元格的值?现在我想将更改应用于一个已更改的单元格。稍后我可以修改它以获取多个已更改单元格的值。

1 个答案:

答案 0 :(得分:3)

创建全局范围。每次更改内容时,请将该范围添加到全局范围。单击时,突出显示全局范围内的所有单元格。

Public ChangeRng as Range

Private Sub Worksheet_Change(ByVal Target as Range) 
    If ChangeRng Is Nothing Then
        Set ChangeRng = Target
    Else
        Set ChangeRng = Application.Union(ChangeRng, Target)
    End If       
End Sub

Private Sub CommandButton1_Click()
    Dim cel as Range
    For Each cel in ChangeRng
        cel.Interior.Color = RGB(255,0,0)
    Next cel
    Set ChangeRng = Nothing
End Sub