My VBA function stops working with one additional line of code. The final line of code in the working function is: returnString = returnString + "some text from some cell"
. If I copy paste that line of code, it breaks.
The only cause I can see is string length. When I output the length of returnString in the working function it is 32347. EDIT: So 32347 works, but adding a further 10 characters or so it starts to fail. This looks suspiciously close to a maximum length of some sort. Is there any way round this?
At the moment I am using this Function (not Sub) to return the string to an Excel cell. A separate Sub-routine then picks up the contents of the cell, to integrate it with other data and output a JSON file.
This question appears to ask the same thing, but the max was 255, and so the answers don't help me: Getting around the Max String size in a vba function?. Please note, though, that when I save the 32347-length string, it is 32k. So I can't have hit the 2GB limit!
Any help appreciated!
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 & "]"