简体   繁体   English

增加字符串VBA的允许内存

[英]Increasing allowed memory of string VBA

So I have some code to sort through some output from an API and extract information into a spreadsheet. 因此,我有一些代码可以对API的某些输出进行排序,并将信息提取到电子表格中。 The problem is the maximum amount of characters I can set some string variable seems to be around 26513 characters (found using debug.print Len(my-oversized-string). Now is there a way in Excel 2010 to extend how much data a string can hold? I would ideally need my string to hold at least 3,500,000 characters. Is this possible or should I approach this problem from a different way? 问题是我可以设置一些字符串变量的最大字符数似乎是26513个字符(使用debug.print Len(my-oversized-string)找到。)现在Excel 2010中有一种方法可以扩展一个字符串的数据量理想情况下,我的字符串需要至少包含3,500,000个字符,这是否可能?或者我应该以其他方式解决此问题?

The error message I get is subscript out of range and sParagraph = Paragraph(i) is highlighted after clicking debug. 我收到的错误消息是下标超出范围,并且单击调试后,sParagraph = Paragraph(i)被突出显示。

Thanks for your help! 谢谢你的帮助! Justin 贾斯汀

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

Are you sure it is 26513 characters I was able to create a string with 98,333,767 length which is 94,833,767 longer than your requirement of 3,500,000 . 你确定这是26513 characters ,我能够创建一个字符串98,333,767长度为94,833,767长于您的需求3,500,000

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

See this example 看这个例子

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

As you can see that we get a string of 98,333,767 in length. 如您所见,我们得到的字符串长度为98,333,767 The length is as expected 长度符合预期

32767 + (32767 X 3000) = 98333767

If my memory serves me right then a variable-length string can contain up to approx 2 billion (2^31) characters. 如果我的记忆正确,那么可变长度的字符串最多可以包含大约2 billion (2^31)字符。 Whereas a fixed-length string can contain up to approx 64K (2^16) characters. 而固定长度的字符串最多可以包含大约64K (2^16)字符。

FOLLOWUP FROM COMMENTS 意见跟进

You are hardcoding the values For i = 1 To 365 and it's not necessary that the array will have that many items. 您正在将For i = 1 To 365的值硬编码For i = 1 To 365 ,并且数组不必具有那么多的项。 Use lbound and ubound to loop through the array. 使用lboundubound遍历数组。

Also, please, please do not use Activecell/select etc. INTERESTING READ 另外,请不要使用Activecell / select等。有趣的阅读

Try this code ( UNTESTED ) 试试这个代码( 未经测试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM