1

I'm new to vba in excel, infact its the first time for me. Along the way i've managed to work most things out and find answers in the net. However the latest issue im having i cant find an answer, solution to my problem.

this function, downloads a json string, and is usually 5+mb big. and this loop finds the opening and closing {} for each component and adds it to a collection. All is well until I get to around the 1600/35k entry and the runtime error happens.

There is a 2nd error, with a out of string message, but assigning vbNullString to the string stops this from happening

Dim cnt As Long
Dim json As String, idKey As String
Dim strStart As Long, strFinish As Long

cnt = 0
strStart = 1
strFinish = 0
On Error GoTo HandleIt
Do While strFinish <= Len(dataStr) - 5
    json = vbNullString
    strStart = InStr(strStart, dataStr, "{""Id"":", vbBinaryCompare)
    strFinish = InStr(strStart, dataStr, "}", vbBinaryCompare) - 1
    json = Mid$(dataStr, strStart, strFinish)

    ' Get the itemID for the key
    'strStart = InStr(6, json, ",", vbBinaryCompare)
    'idKey = Mid$(json, 7, strStart - 7)

    TSMDataBulk.add json
    strStart = strFinish
    cnt = cnt + 1
Loop

tltd: Everything runs fine until TSMDataBulk.add causes a out of memory error. Can anyone help?

1
  • Could you add them to an array instead? Commented Mar 9, 2016 at 22:34

2 Answers 2

2

It looks like you are using Mid$ as though the parameters were: Mid$(string, start, end) In reality, the parameters are: Mid$(string, start, length)

So if strStart was 5000 and strFinish was 5050, you would be returning all of the characters from 5000 to 10549 instead of returning those from 5000 to 5050. This may well explain the out of memory errors as you get further into the string.

Change:

json = Mid$(dataStr, strStart, strFinish)

to:

json = Mid$(dataStr, strStart, (strFinish - strStart) + 1)
Sign up to request clarification or add additional context in comments.

2 Comments

Good catch! Completely missed that myself.
Thankyou barrwc. That was spot on. I'de give you some rep but my rep is too low to do that!
1

TSMDataBulk looks like it is a Scripting.Dictionary object. You are adding a key but no item.

TSMDataBulk.add key:=json, item:=json
'alternate no. 1
TSMDataBulk.add key:=json, item:=""
'alternate no. 2 (overwrite method)
TSMDataBulk.item(json) = ""

Judging by your sample code there is a session id in the first position. I might try to use that as the key and put the remainder in as the item.

1 Comment

It is Jeeped. and the add() command is item:=, key:=, before:=, after:= with item: being the only bit of info required. I've tried using the dictionary but i'm still getting the same answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.