1

所以我有一些代码可以对 API 的一些输出进行排序,并将信息提取到电子表格中。问题是我可以设置的字符串变量的最大字符数似乎约为 26513 个字符(使用 debug.print Len(my-oversized-string) 找到。现在 Excel 2010 中有一种方法可以扩展字符串的数据量可以持有吗?理想情况下,我的字符串至少需要包含 3,500,000 个字符。这可能吗?或者我应该以不同的方式解决这个问题吗?

我收到的错误消息是下标超出范围,并且 sParagraph = Paragraph(i) 在单击调试后突出显示。

谢谢你的帮助!贾斯汀

Dim URL As String: URL = "someAPIurlputhere"
Dim Http As New WinHttpRequest
    Http.Open "GET", URL, False
    Http.Send
Dim Resp As String: Resp = Http.ResponseText
Dim Paragraph As Variant
    Debug.Print Len(Resp)
    For i = 1 To 365
        Paragraph = Split(Resp, "date")
        ActiveCell.Offset(1, 0).Activate
        ActiveCell.Range("A1:E1").Select
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Dim sParagraph As String: sParagraph = Paragraph(i)
        Dim Lines As Variant: Lines = Split(sParagraph, ",")
        ActiveCell.Offset(0, 1).FormulaR1C1 = Lines(0)
        ActiveCell.Offset(0, 2).FormulaR1C1 = Lines(9)
        ActiveCell.Offset(0, 3).FormulaR1C1 = Lines(27)
        ActiveCell.Offset(0, 4).FormulaR1C1 = Lines(29)
        Erase Paragraph
    Next i
4

1 回答 1

2

你确定26513 characters我能够创建一个98,333,767长度94,833,767超过你要求的字符串3,500,000

98,333,767 - 3,500,000 = 94,833,767

看这个例子

Sub Sample()
    Dim s As String
    Dim i As Long

    '~~> This will create a string of 32767 in length
    s = Application.WorksheetFunction.Rept("a", 32767)

    '~~> Adding it in a loop 3000 times to check what length we get
    For i = 1 To 3000
        s = s & Application.WorksheetFunction.Rept("a", 32767)
    Next i

    '~~> 98,333,767
    Debug.Print Len(s)
End Sub

如您所见,我们得到了一个98,333,767长度为的字符串。长度符合预期

32767 + (32767 X 3000) = 98333767

如果我没记错的话,那么可变长度字符串最多可以包含大约2 billion (2^31)字符。而固定长度的字符串最多可以包含大约64K (2^16)字符。

评论跟进

您正在对值进行硬编码,For i = 1 To 365并且数组没有必要包含那么多项目。使用lboundandubound循环遍历数组。

另外,请不要使用 Activecell/select 等。有趣的阅读

试试这个代码(未测试

Sub Sample()
    Dim URL As String: URL = "someAPIurlputhere"
    Dim Http As New WinHttpRequest
    Dim Resp As String, sParagraph As String
    Dim Paragraph As Variant, Lines As Variant
    Dim i As Long
    Dim ws As Worksheet

    Http.Open "GET", URL, False
    Http.Send

    Resp = Http.ResponseText

    Debug.Print Len(Resp)

    Paragraph = Split(Resp, "date")

    '~~> Change as applicable
    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        '~~> Loop through the array
        For i = LBound(Paragraph) To UBound(Paragraph)
            .Range("A1:E1").Insert Shift:=xlDown, _
            CopyOrigin:=xlFormatFromLeftOrAbove

            sParagraph = Paragraph(i)

            Lines = Split(sParagraph, ",")

            '~~> I haven't changed this part. Please do not
            '~~> use Activecell. Work with actual range.
            '.Range("B1").Formula = Lines(0)
            '.Range("C1").Formula = Lines(9)
            '.Range("D1").Formula = Lines(27)
            '.Range("E1").Formula = Lines(29)

            ActiveCell.Offset(0, 1).Formula = Lines(0)
            ActiveCell.Offset(0, 2).Formula = Lines(9)
            ActiveCell.Offset(0, 3).Formula = Lines(27)
            ActiveCell.Offset(0, 4).Formula = Lines(29)

            Erase Paragraph
        Next i
    End With
End Sub
于 2013-10-15T13:31:57.950 回答