Problems transfering Array between functions and subs

Spurious

Active Member
Joined
Dec 14, 2010
Messages
439
I got a problem. The last sub doesnt work, I always get subscript out of range, whats my mistake?

Code:
Sub ECBFX(jCrcy As String)
[...]
Dim jData() As Variant
[...]
jData() = QTextFormat(jQText)
Call WriteData(jData())
[...]
End Sub

Function QTextFormat(jQText As String) As Variant
[...]
Dim jData() As Variant
[...]
Array is filled
[...]
QTextFormat = jData()
End Function

Sub WriteData(jData() As Variant)
Dim jCount As Long
Open "c:\test.txt" For Output As #1
Do Until jCount = UBound(jData)
    Print #1, jData(jCount, 1), jData(jCount, 2)
Loop
Close #1
End Sub

Second question: Is there a way to just copy the array into the text file without using a while loop?

Help as usual appreciated !
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Suggestion: declare jData at the module level - above the first sub - so that it's available to all subs and functions, and don't pass it as a parameter.

(I suspect you should be declaring it and passing it as just jData rather than jData().)
 
Upvote 0
Please don't make jdata global. It *might* solve this problem -- though I don't believe so -- but it is a prescription for problems down the road.

It's easy to find your problem. Step through your code and see what you get in the last sub!

My guess is that you have a problem with jcount. It's not initialized and probably not within lbound(jdata) and ubound(jdata).

Also, once you solve this problem, you should expect an infinite loop since you are not updating jcount.

In a case like this a For jcount=...loop is probably a better idea.

I got a problem. The last sub doesnt work, I always get subscript out of range, whats my mistake?

Code:
Sub ECBFX(jCrcy As String)
[...]
Dim jData() As Variant
[...]
jData() = QTextFormat(jQText)
Call WriteData(jData())
[...]
End Sub

Function QTextFormat(jQText As String) As Variant
[...]
Dim jData() As Variant
[...]
Array is filled
[...]
QTextFormat = jData()
End Function

Sub WriteData(jData() As Variant)
Dim jCount As Long
Open "c:\test.txt" For Output As #1
Do Until jCount = UBound(jData)
    Print #1, jData(jCount, 1), jData(jCount, 2)
Loop
Close #1
End Sub

Second question: Is there a way to just copy the array into the text file without using a while loop?

Help as usual appreciated !
 
Upvote 0

Forum statistics

Threads
1,224,532
Messages
6,179,388
Members
452,908
Latest member
MTDelphis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top