首页 文章

通过VBA Excel在站点中更改数据

提问于
浏览
1

我正在尝试通过以下代码更改网站中的值,但如何通过代码单击“提交”按钮?

另外如果我的.Navigate值应该从A1 - > A2 - >继续移动而不是静态值?(如何使用'For'循环代替.Navigate)?

Sub FillForm()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "https://www.morovia.com/bulk-check-digit-calculation/index.php"
IE.Visible = True
While IE.busy
DoEvents
Wend
IE.Document.All("").Value = ThisWorkbook.Sheets("Sheet1").Range("b1")
End Sub

任何帮助,将不胜感激 . 谢谢!

3 回答

  • 2

    有几点需要注意:

    1)您需要在代码中使用正确的等待行:

    While ie.Busy Or ie.readyState < 4: DoEvents: Wend
    

    2)提交按钮有一个id . 这通常是最快的检索方法,所以使用它 .

    ie.document.querySelector("#submit").click
    

    3)尽可能使用ID . 例如,输入初始搜索号码时

    ie.document.querySelector("#sourceNumbers").value = ThisWorkbook.Sheets("Sheet1").Range("B1").value
    

    4)最新的导航方法是:

    ie.navigate2
    

    5)在 .click.submit 之后总是有另一个等待行,以允许页面加载新内容

    6)始终记住 .Quit 应用程序以防止进程继续运行并可能导致系统资源不足 .

    Public Sub FillForm()
        Dim ie As Object
        Set ie = CreateObject("InternetExplorer.Application")
        With ie
            .Visible = True
            .Navigate2 "https://www.morovia.com/bulk-check-digit-calculation/index.php"
    
            While .Busy Or .readyState < 4: DoEvents: Wend
            .document.querySelector("#sourceNumbers").Value = ThisWorkbook.Sheets("Sheet1").Range("B1").Value
    
            .document.querySelector("#submit").Click
    
            While .Busy Or .readyState < 4: DoEvents: Wend
    
            Debug.Print .document.querySelector("#results").Value
            .Quit
        End With
    End Sub
    

    列A中的链接循环可能如下所示:

    Option Explicit
    Public Sub LoopLinks()
        Dim ie As Object, ws As Worksheet, arr(), i As Long
        Set ws = ThisWorkbook.Worksheets("Sheet1")
        Set ie = CreateObject("InternetExplorer.Application")
        arr = Application.Transpose(ws.Range("A1:A3").Value) '<== amend range as required
    
        With ie
            .Visible = True
    
            For i = LBound(arr) To UBound(arr)
                If InStr(arr(i, 1), "http") > 0 Then '<=check is an URL
                    .Navigate2 arr(i)
    
                    While .Busy Or .readyState < 4: DoEvents: Wend
    
                    'do something......
                End If
            Next
            .Quit
        End With
    End Sub
    
  • 0
    Sub FillForm()
    Dim IE As Object
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Navigate "https://www.morovia.com/bulk-check-digit-calculation/index.php"
    IE.Visible = True
    While IE.busy
    DoEvents
    Wend
    
    Value = ThisWorkbook.Sheets("Sheet1").Range("b1")
    IE.Document.getelementbyid("sourceNumbers").Value = Value
    IE.Document.getelementbyid("submit").Click
    End Sub
    
  • 2
    Sub FillForm()
    Dim IE As Object
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Navigate "https://www.morovia.com/bulk-check-digit-calculation/index.php"
    IE.Visible = True
    While IE.busy
    DoEvents
    Wend
    For i = 1 to 10 ' Loop according to your Number Of Rows Containing Values
    Value = ThisWorkbook.Sheets("Sheet1").Range("B" & i).Value
    IE.Document.getelementbyid("sourceNumbers").Value = Value
    IE.Document.getelementbyid("submit").Click 'here submit should be in all small letters
    Next i
    End Sub
    

相关问题