EXCEL-VBA。获取单元格值作为键值对

舞会

我正在尝试从列“ I”的Excel单元格获取地址值,并将其作为查询字符串传递给使用VBA的URL。在Excel中嵌入了“ Microsoft Object Browser”以加载页面。

这有可能吗?因为我担心作为查询字符串传递的数据量太高(大约1000行)。

代码无法正常工作,有什么办法可以通过将查询字符串作为数组传递来做同样的事情吗?

我也需要VBA语法来解析字典值。

我是VBA的新手。请帮忙。

    Dim Arr() As Variant ' declare an unallocated array.
Arr = Range("I:I") ' Arr is now an allocated array
Set dict = CreateObject("Scripting.Dictionary")
Dim iRow As Integer
iRow = 1
 Dim parms As Variant
   Dim rg As Range
    For Each rg In Sheet1.Range("I:I")
        ' Print address of cells that are negative
        'MsgBox (rg.Value)
         'result = result & rg.Value
          dict.Add rg.Value
          iRow = (iRow + 1)        
    Next
MsgBox (dict.Item(1))
Set dict = Nothing
'WebBrowser1.Navigate2 "http://localhost/excelmaps/maps.php?adr=" & parms
End Sub
弗洛伦特·B。

IE的最大URL长度似乎为2083个字符:

https://support.microsoft.com/zh-CN/kb/208427

要构建查询,我将使用字符串生成器(“ System.Text.StringBuilder”)。您还需要对所有参数进行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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Excel VBA重新计算后获取单元格值

来自分类Dev

Excel VBA重现单元格的值

来自分类Dev

VBA Excel单元格值是“ not” NULL

来自分类Dev

VBA Excel单元格值是“ not” NULL

来自分类Dev

Excel VBA - 使用单元格中的值作为地址来定位另一个单元格

来自分类Dev

Excel VBA从selection.address获取行,单元格值

来自分类Dev

如何在Excel中获取VBA函数以返回活动单元格的值?

来自分类Dev

Excel VBA-以RTF格式的文本获取单元格值

来自分类Dev

如何使用VBA在Excel中的错误单元格中获取基础值

来自分类Dev

如何使用VBA在Excel中获取单元格字体值的列表

来自分类Dev

如何使用VBA在Excel中的错误单元格中获取基础值

来自分类Dev

如何在Excel中获取VBA函数以返回活动单元格的值?

来自分类Dev

Excel Vba获取单元格的值并设置字符串

来自分类Dev

如何使用 VBA If 语句的环境变量在 Excel 中获取单元格值

来自分类Dev

Excel 单元格值作为 .SetSourceData 范围

来自分类Dev

Excel VBA / 如果单元格的值与参考单元格中的值不符,则删除单元格

来自分类Dev

VBA Excel:获取多个单元格的结果

来自分类Dev

如何从当前函数VBA Excel获取单元格行

来自分类Dev

Excel VBA递归获取相关单元格

来自分类Dev

Excel VBA根据单元格值选择范围

来自分类Dev

Excel VBA:查找具有相似值的单元格

来自分类Dev

Excel VBA-For循环。使用值查找单元格

来自分类Dev

Excel VBA:基于单元格值的颜色范围

来自分类Dev

在Excel VBA中循环遍历单元格值

来自分类Dev

通过更改单元格上的值,Excel VBA崩溃

来自分类Dev

通过VBA在excel中移动单元格值

来自分类Dev

需要使用VBA转换Excel单元格值

来自分类Dev

Excel VBA案例无法识别单元格的值

来自分类Dev

根据Excel VBA中的公式创建单元格值

Related 相关文章

  1. 1

    Excel VBA重新计算后获取单元格值

  2. 2

    Excel VBA重现单元格的值

  3. 3

    VBA Excel单元格值是“ not” NULL

  4. 4

    VBA Excel单元格值是“ not” NULL

  5. 5

    Excel VBA - 使用单元格中的值作为地址来定位另一个单元格

  6. 6

    Excel VBA从selection.address获取行,单元格值

  7. 7

    如何在Excel中获取VBA函数以返回活动单元格的值?

  8. 8

    Excel VBA-以RTF格式的文本获取单元格值

  9. 9

    如何使用VBA在Excel中的错误单元格中获取基础值

  10. 10

    如何使用VBA在Excel中获取单元格字体值的列表

  11. 11

    如何使用VBA在Excel中的错误单元格中获取基础值

  12. 12

    如何在Excel中获取VBA函数以返回活动单元格的值?

  13. 13

    Excel Vba获取单元格的值并设置字符串

  14. 14

    如何使用 VBA If 语句的环境变量在 Excel 中获取单元格值

  15. 15

    Excel 单元格值作为 .SetSourceData 范围

  16. 16

    Excel VBA / 如果单元格的值与参考单元格中的值不符,则删除单元格

  17. 17

    VBA Excel:获取多个单元格的结果

  18. 18

    如何从当前函数VBA Excel获取单元格行

  19. 19

    Excel VBA递归获取相关单元格

  20. 20

    Excel VBA根据单元格值选择范围

  21. 21

    Excel VBA:查找具有相似值的单元格

  22. 22

    Excel VBA-For循环。使用值查找单元格

  23. 23

    Excel VBA:基于单元格值的颜色范围

  24. 24

    在Excel VBA中循环遍历单元格值

  25. 25

    通过更改单元格上的值,Excel VBA崩溃

  26. 26

    通过VBA在excel中移动单元格值

  27. 27

    需要使用VBA转换Excel单元格值

  28. 28

    Excel VBA案例无法识别单元格的值

  29. 29

    根据Excel VBA中的公式创建单元格值

热门标签

归档