【问题标题】:VBA paste excel data or multiple cells to web text areaVBA 将 Excel 数据或多个单元格粘贴到 Web 文本区域
【发布时间】:2015-04-13 03:30:03
【问题描述】:
我一直在尝试将 Excel 中的一系列单元格复制并粘贴或使用 .value 到 Web 文本区域。我只能用一个单元格做。该链接 (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
【问题讨论】:
标签:
vba
excel
internet-explorer
copy-paste
【解决方案1】:
您的代码试图发送一个数组对象(由Range("A25:C27") 返回),而它实际上应该是一个字符串。一种方法是使用 textarea 所需的格式将数组对象转换为文本字符串。下面的代码循环遍历数组以将每一行作为制表符分隔的字符串返回,后跟一个新行。它似乎可以使用 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