我正在尝试从列的“excel”单元格中获取地址值“我'并使用VBA将其作为查询字符串传递给URL。嵌入了Microsoft对象浏览器'在Excel中加载页面。
这甚至可能吗?因为我担心传递的数据量因为查询字符串太高(近似1000行)。
代码不起作用,有什么方法可以通过将查询字符串作为数组传递来做同样的事情吗?
我还需要VBA语法来解析字典值。
我是VBA的新手。请帮助。
<table width="100%">
<tr>
<td>
<div style="height:35px">
<div style="float:left" class="controlLabelBold">Check Number:</div>
<div style="float:left">
<input type="password" name="checkNumber" class="text ui-widget-content ui-corner-all" style="width:250px; height: 17px;" />
</div>
</div>
</td>
<td>
<div style="height:35px">
<div style="float:left" class="controlLabel">Check Date:</div>
<div style="float:left">
<input name="checkDate" class="checkDate text ui-widget-content ui-corner-all" style="width:250px;" readonly />
</div>
</div>
</td>
</tr>
<tr>
<td>
<div style="height:35px">
<div style="float:left" class="controlLabel">Client Name:</div>
<div style="float:left">
<input name="clientName" class="text ui-widget-content ui-corner-all" style="width:250px;" readonly />
</div>
</div>
</td>
<td>
<div style="height:35px">
<div style="float:left" class="controlLabel">Amount:</div>
<div style="float:left">
<input name="amount" class="text ui-widget-content ui-corner-all" style="width:250px;" readonly/>
</div>
</div>
</td>
</tr>
<tr>
<td>
<div style="height:35px">
<div style="float:left" class="controlLabel">Pesos:</div>
<div style="float:left">
<input id="amountInWords" name="amountInWords" class="text ui-widget-content ui-corner-all" style="min-width:250px;" value="One Thousand Nine Hundred Forty Nine Pesos only and this is fake msg" readonly />
</div>
</div>
</td>
</tr>
</table>
答案 0 :(得分:1)
有很多事情要发生,所以我会尝试解决字典部分,因为那是你标记的内容。
首先使用词典,您可以按如下方式添加项目:
dict(“your key”) = “your value”
我发现您已正确设置字典,并始终确保在运行代码之前在VBA编辑器中添加字典引用(转到工具 - &gt;参考 - &gt; Microsoft脚本运行时)
在这种情况下,看起来您的键值是增量整数。那么为什么不使用数组,如下面的代码?
另一个问题是循环整个列(所有> 1百万行)会产生溢出错误。也许开始手动指定要在for循环中循环的行(请参阅“rowsToLoop”变量):
Sub der()
Dim rowsToLoop As Integer
rowsToLoop = 1000
Dim Arr() As Variant 'define empty array
ReDim Arr(rowsToLoop) 'redefine with variable length
Dim dict As Dictionary
Set dict = CreateObject("Scripting.Dictionary")
Dim x As Integer
For x = 1 To rowsToLoop
'With an array
Arr(x - 1) = Sheet1.Range("I1").Cells(x, 1).Value 'note array index starts at 0
'With a dictionary
dict(x - 1) = Sheet1.Range("I1").Cells(x, 1).Value
Next x
MsgBox "This is from array: " & Arr(1)
MsgBox "This is from dictionary: " & dict(1)
End Sub
答案 1 :(得分:0)
似乎IE的最大URL长度是2083个字符:
https://support.microsoft.com/en-us/kb/208427
要构建查询,我将使用字符串构建器(&#34; System.Text.StringBuilder&#34;)。 您还需要对所有参数进行URL编码。
以下是使用范围[A1:B10]:
中的名称/值构建网址的示例Sub BuildURL
' Read the names/values from a sheet
Dim names_values()
names_values = [A1:B10].Value2
' Create a string builder
Dim sb As Object
Set sb = CreateObject("System.Text.StringBuilder")
sb.Append_3 "http://localhost/excelmaps/maps.php"
' Build the query
Dim i&, name$, value$
For i = 1 To UBound(names_values)
name = names_values(i, 1)
value = names_values(i, 2)
If i = 1 Then sb.Append_3 ("?") Else sb.Append_3 ("&")
sb.Append_3 URLEncode(name) ' Adds the name
sb.Append_3 "="
sb.Append_3 URLEncode(value) ' Adds the value
Next
' Print the result
Debug.Print sb.ToString()
End Sub
Public Function URLEncode(url As String, Optional space_to_plus As Boolean) As String
Static ToHex(15), IsLiteral%(127), buffer() As Byte, bufferCapacity&
Dim urlBytes() As Byte, bufferLength&, i&, u&, b&, space&
If space_to_plus Then space = 32 Else space = -1
If bufferCapacity = 0 Then GoSub InitializeOnce
urlBytes = url
For i = 0 To UBound(urlBytes) Step 2
If bufferLength >= bufferCapacity Then GoSub IncreaseBuffer
u = urlBytes(i) + urlBytes(i + 1) * 256&
If u And -128 Then ' U+0080 to U+1FFFFF '
If u And -2048 Then ' U+0800 to U+1FFFFF '
If (u And 64512) - 55296 Then ' U+0800 to U+FFFF '
b = 224 + (u \ 4096): GoSub WriteByte
b = 128 + (u \ 64 And 63&): GoSub WriteByte
b = 128 + (u And 63&): GoSub WriteByte
Else ' surrogate U+10000 to U+1FFFFF '
i = i + 2
u = ((urlBytes(i) + urlBytes(i + 1) * 256&) And 1023&) _
+ &H10000 + (u And 1023&) * 1024&
b = 240 + (u \ 262144): GoSub WriteByte
b = 128 + (u \ 4096 And 63&): GoSub WriteByte
b = 128 + (u \ 64 And 63&): GoSub WriteByte
b = 128 + (u And 63&): GoSub WriteByte
End If
Else ' U+0080 to U+07FF '
b = 192 + (u \ 64): GoSub WriteByte
b = 128 + (u And 63&): GoSub WriteByte
End If
ElseIf IsLiteral(u) Then ' unreserved ascii character '
buffer(bufferLength) = u
bufferLength = bufferLength + 2
ElseIf u - space Then ' reserved ascii character '
b = u: GoSub WriteByte
Else ' space character '
buffer(bufferLength) = 43 ' convert space to + '
bufferLength = bufferLength + 2
End If
Next
URLEncode = LeftB$(buffer, bufferLength)
Exit Function
WriteByte:
buffer(bufferLength) = 37 '%
buffer(bufferLength + 2) = ToHex(b \ 16)
buffer(bufferLength + 4) = ToHex(b And 15&)
bufferLength = bufferLength + 6
Return
IncreaseBuffer:
bufferCapacity = UBound(buffer) * 2
ReDim Preserve buffer(bufferCapacity + 25)
Return
InitializeOnce:
bufferCapacity = 2048
ReDim buffer(bufferCapacity + 25)
For i = 0 To 9: ToHex(i) = CByte(48 + i): Next '[0-9]'
For i = 10 To 15: ToHex(i) = CByte(55 + i): Next '[A-F]'
For i = 48 To 57: IsLiteral(i) = True: Next '[0-9]'
For i = 65 To 90: IsLiteral(i) = True: Next '[A-Z]'
For i = 97 To 122: IsLiteral(i) = True: Next '[a-z]'
IsLiteral(45) = True ' - '
IsLiteral(46) = True ' . '
IsLiteral(95) = True ' _ '
IsLiteral(126) = True ' ~ '
Return
End Function