VBA 将 excel 数据或多个单元格粘贴到网络文本区域

VBA paste excel data or multiple cells to web text area

我一直在尝试将 excel 中的一系列单元格复制并粘贴或使用 .value 到 Web 文本区域。我只能处理一个单元格。 link (https://en.batchgeo.com/) 允许您复制和粘贴多个地址并为您映射。我似乎无法在 excel vba 中做到这一点。

VBA Excel 宏尝试:

Sub BatchGeo2()
Dim IE As Object
Dim MyURL As String


Set IE = CreateObject("InternetExplorer.Application")
'create new instance of IE. use reference to return current open IE if
'you want to use open IE window. Easiest way I know of is via title bar.
MyURL = "https://en.batchgeo.com/"


IE.Navigate MyURL
'go to web page listed inside quotes
IE.Visible = True

While IE.busy
DoEvents 'wait until IE is done loading page.

Wend



With IE.Document

    .getElementById("sourceData").innerText = Range("A25:C27")
    .all("mapnow_button").Click

End With

End Sub

您的代码试图发送一个数组对象(由 Range("A25:C27") 编辑 return),但实际上它应该是一个字符串。一种方法是使用文本区域所需的格式将数组对象转换为文本字符串。下面的代码循环遍历数组 return 每行作为制表符分隔的字符串,后跟一个新行。它似乎可以使用 batchgeo 上提供的电子表格模板工作。

Sub BatchGeo2()
Dim IE As Object
Dim MyURL As String


Set IE = CreateObject("InternetExplorer.Application")
'create new instance of IE. use reference to return current open IE if
'you want to use open IE window. Easiest way I know of is via title bar.
MyURL = "https://en.batchgeo.com/"


IE.Navigate MyURL
'go to web page listed inside quotes
IE.Visible = True

While IE.busy
DoEvents 'wait until IE is done loading page.

Wend

'Generate text string
Dim str As String
Dim arr() As Variant
Dim tableRow As Integer
Dim tableCol As Integer

'Assign range to an array
arr = Range("A25:C27")

'Loop through each row of the range to format a tab delimited text string
For tableRow = LBound(arr) To UBound(arr)
    For tableCol = LBound(arr, 2) To UBound(arr, 2)
        str = str & arr(tableRow, tableCol) & vbTab
    Next tableCol
    str = str & vbNewLine
Next tableRow

With IE.Document

    'Assign text string to textarea
    .getElementById("sourceData").innerText = str
    .all("mapnow_button").Click

End With

End Sub