我的VBA功能停止使用另外一行代码。工作函数中的最后一行代码是:returnString = returnString + "some text from some cell"
。如果我复制粘贴那行代码,它会中断。
我能看到的唯一原因是字符串长度。当我在工作函数中输出returnString的长度时,它是32347. 编辑:所以32347工作,但是再添加10个字符左右开始失败。这看起来很可疑接近某种最大长度。这有什么办法吗?
目前我正在使用此函数(不是Sub)将字符串返回到Excel单元格。然后,一个单独的子例程获取单元格的内容,将其与其他数据集成并输出JSON文件。
这个问题似乎问了同样的问题,但最大值是255,所以答案对我没有帮助:Getting around the Max String size in a vba function?。但请注意,当我保存32347长度的字符串时,它是32k。所以我不能达到2GB的限制!
任何帮助表示赞赏!
Function getData(str)
Dim lArray() As String
Dim pData As String
Dim ctr As Integer
Dim ctr2 As Integer
Dim ctr3 As Integer
Dim returnString As String
Set rangetoexport = Worksheets("mysheet").Range("c2:m2728")
Dim id As Long
Dim rowRef As Long
Dim colRef As Integer
Dim var(47) As String
Const idColumn = 1
Const webColumn = 2
Const nameColumn = 3
Const revColumn = 4
Const empColumn = 5
Const ctryColumn = 10
Const indyColumn = 11
Const numFactors = 47
Const labelRow = 1
Const apost = """"
lArray = Split(str, ",")
returnString = "["
If UBound(lArray) <> 0 Then
pData = ""
For ctr = 0 To UBound(lArray)
'Open the object
returnString = returnString + "{"
'Find the row
id = lArray(ctr)
rowRef = -1
For ctr2 = 0 To rangetoexport.Rows.Count
If rangetoexport.Cells(ctr2, idColumn) = id Then
rowRef = ctr2
Exit For
End If
Next
'add the parts
'add the ID and new object
returnString = returnString & """company"": {"
returnString = returnString & """ID"":" & rangetoexport.Cells(rowRef, idColumn) & ","
returnString = returnString & """Website"":""" & rangetoexport.Cells(rowRef, webColumn) & ""","
returnString = returnString & """Company Name"":""" & rangetoexport.Cells(rowRef, nameColumn) & ""","
returnString = returnString & """Revenue"":""" & rangetoexport.Cells(rowRef, revColumn) & ""","
returnString = returnString & """Employee Count"":" & rangetoexport.Cells(rowRef, empColumn) & ","
returnString = returnString & """Country"":""" & rangetoexport.Cells(rowRef, ctryColumn) & ""","
returnString = returnString & """Industry Classification (SIC4)"":""" & rangetoexport.Cells(rowRef, indyColumn) & """}},"
'returnString = returnString & """factors"": {"
'For ctr3 = 1 To 10
'Next
'returnString = returnString & apost & rangetoexport.Cells(labelRow, 12) & apost & ":" & rangetoexport.Cells(rowRef, 12) & ","
'returnString = returnString & apost & rangetoexport.Cells(labelRow, 12) & apost & ":" & rangetoexport.Cells(rowRef, 12) & ","
'returnString = returnString & apost & rangetoexport.Cells(labelRow, 12) & apost & ":" & rangetoexport.Cells(rowRef, 12) & ","
'returnString = returnString & apost & rangetoexport.Cells(labelRow, 12) & apost & ":" & rangetoexport.Cells(rowRef, 12) & ","
'returnString = returnString & rangetoexport.Cells(rowRef, webColumn) & "},"
Next
Else
returnString = ""
End If
returnString = Left(returnString, Len(returnString) - 1)
getData = returnString & "]"
答案 0 :(得分:-2)
我建议使用数组代替。您可以创建一个动态大小的数组,最后,它更容易操作它。