如果工作表上的某些单元格已更改,请更新UserForm上的标签

时间:2019-07-03 09:43:01

标签: excel vba

我有UserForm,可使用Google Maps API计算距离和行驶时间。除了在用户窗体上更新标签外,其他所有操作都正常。我必须单击两次按钮才能在标签上看到更新的值。将XML和Excel公式转换为=FILTERXML与例如将“ 2小时35分钟”转换为“ 3”之间存在差距

=IFERROR(IF(VALUE(IF(FIND("min";BZ21;1)=11;MID(BZ21;FIND("min";BZ21;1)-2;1);MID(BZ21;FIND("min";BZ21;1)-3;2)))<30; VALUE(LEFT(BZ21;FIND("h";BZ21;1)-2))&","&5;VALUE(LEFT(BZ21;FIND("h";BZ21;1)-2))+1);"")

如何使其工作,这样我就不必两次单击UserForm按钮?

我有一个将XML文件数据获取到工作表(用户代码)的

Private Sub CommandButton3_Click()

On Error Resume Next

Dim xmlhttp As Object
Set xmlhttp = CreateObject("MSXML2.serverXMLHTTP")
Dim myurl As String

ThisWorkbook.Worksheets("Other Data").Range("BY21").Value = DistanceFortumSite.TextBox1.Text

myurl = "https://maps.googleapis.com/maps/api/distancematrix/xml?origins=" & ThisWorkbook.Worksheets("Other Data").Range("BY21").Value _
& "&destinations=" & ThisWorkbook.Worksheets("Other Data").Range("BY18").Value & "&mode=" & ThisWorkbook.Worksheets("Other Data").Range("BY3").Value _
& "&key=" & ThisWorkbook.Worksheets("Other Data").Range("CE1").Value
xmlhttp.Open "GET", myurl, False
xmlhttp.send
ThisWorkbook.Worksheets("Contact database").Range("A155") = xmlhttp.responseText

    If Err <> 0 Then
       Windows:=False
       MsgBox "Check your Internet connection! This feature does not work offline!"
       Else
       End If
       On Error GoTo 0

End Sub

Private Sub UserForm_Activate()

DistanceFortumSite.TextBox1.Text = ThisWorkbook.Worksheets("Other Data").Range("BY21").Value
DistanceFortumSite.TextBox2.Text = ThisWorkbook.Worksheets("Other Data").Range("BY18").Value

DistanceFortumSite.Controls("Label4").Caption = ThisWorkbook.Sheets("Other Data").Range("CA21").Value
DistanceFortumSite.Controls("Label5").Caption = ThisWorkbook.Sheets("Other Data").Range("CA22").Value

End Sub

Private Sub UserForm_Initialize()

DistanceFortumSite.TextBox1.Text = ThisWorkbook.Worksheets("Other Data").Range("BY21").Value
DistanceFortumSite.TextBox2.Text = ThisWorkbook.Worksheets("Other Data").Range("BY18").Value

DistanceFortumSite.Controls("Label4").Caption = ThisWorkbook.Sheets("Other Data").Range("CA21").Value
DistanceFortumSite.Controls("Label5").Caption = ThisWorkbook.Sheets("Other Data").Range("CA22").Value

End Sub

然后通过工作表上的公式,我使用=FILTERXML

获取值

如果在“其他数据”上进行了某些更改,这是将值获取到用户窗体标签的代码:

Private Sub Worksheet_Change(ByVal Target As Range)

                    DistanceFortumSite.Controls("Label4").Caption = ThisWorkbook.Sheets("Other Data").Range("CA21").Value
                    DistanceFortumSite.Controls("Label5").Caption = ThisWorkbook.Sheets("Other Data").Range("CA22").Value


        If ThisWorkbook.Sheets("Other Data").Range("P24").Value <> "" Then
            Call GoogleAPIall
        End If

End Sub

0 个答案:

没有答案