variants and arrays

maks25

New Member
Joined
Sep 8, 2011
Messages
30
Hello,

I am trying to loop through sheets and combine columns of data into a variant

Code:
dim sSheet(3) as string
dim mgTickers() as variant
dim x as integer

Do
Sheets(sSheet(x)).Select


'variant
With ActiveSheet
    mgTickers() = Range(.Cells(22, 1), .Cells(.Rows.Count, 1).End(xlDown)).Value
End With


x = x + 1
Loop Until x > 3

Obviously the code above would just replace the variant data with the new data from the next sheet. My question is, how can I add data to the variant without overwriting it?

Thank you.
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Hello,

I am trying to loop through sheets and combine columns of data into a variant

Code:
dim sSheet(3) as string
dim mgTickers() as variant
dim x as integer

Do
Sheets(sSheet(x)).Select


'variant
With ActiveSheet
    mgTickers() = Range(.Cells(22, 1), .Cells(.Rows.Count, 1).End(xlDown)).Value
End With


x = x + 1
Loop Until x > 3

Obviously the code above would just replace the variant data with the new data from the next sheet. My question is, how can I add data to the variant without overwriting it?

Thank you.
I would use a second variant (array) like this:
Rich (BB code):
Sub VariantArray()
Dim sSheet(3) As String
Dim vA As Variant
Dim mgTickers() As Variant
Dim x As Integer, i As integr, j As Integer

ReDim mgTickers(1 To 1)
'Need to establish values for sSheet() here
Do
Sheets(sSheet(x)).Select
'variant
With ActiveSheet
    vA = Range(.Cells(22, 1), .Cells(.Rows.Count, 1).End(xlDown)).Value
    For i = LBound(vA, 1) To UBound(vA, 1)
        ReDim Preserve mgTickers(1 To UBound(mgTickers) + UBound(vA, 1) - 1)
        j = j + 1
        mgTickers(j) = vA(i, 1)
    Next i
End With
x = x + 1
Loop Until x > 3
End Sub
 
Upvote 0
I would use a second variant (array) like this:
Rich (BB code):
Sub VariantArray()
Dim sSheet(3) As String
Dim vA As Variant
Dim mgTickers() As Variant
Dim x As Integer, i As integr, j As Integer

ReDim mgTickers(1 To 1)
'Need to establish values for sSheet() here
Do
Sheets(sSheet(x)).Select
'variant
With ActiveSheet
    vA = Range(.Cells(22, 1), .Cells(.Rows.Count, 1).End(xlDown)).Value
    For i = LBound(vA, 1) To UBound(vA, 1)
        ReDim Preserve mgTickers(1 To UBound(mgTickers) + UBound(vA, 1) - 1)
        j = j + 1
        mgTickers(j) = vA(i, 1)
    Next i
End With
x = x + 1
Loop Until x > 3
End Sub

Thanks,

I tried it however I got an overflow error, so I changed i to a long.

But now I am getting an "Out of memory error" on
ReDim Preserve mgTickers(1 To UBound(mgTickers) + UBound(vA, 1) - 1)

How can I fix it?

thanks for helping me out!

Max
 
Upvote 0
Thanks,

I tried it however I got an overflow error, so I changed i to a long.

But now I am getting an "Out of memory error" on


How can I fix it?

thanks for helping me out!

Max
Maybe too much data in the columns you are trying to combine. Try it in a reduced-size test to see if it is working first. If it is then reply with an explanation of what you are trying to achieve and maybe a better approach can be found.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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