Using a variable inside code

Lavina

Board Regular
Joined
Dec 18, 2018
Messages
75
Hello guys,

I'm trying to minimize lines that i have in my code and i ran into a small problem I'm not sure how to approach

I have 3 arrays that i fill up depending on a variable:

Code:
If fullArray(i, 2) >= 1 And fullArray(i, 2) < 3 Then
arrayInQuestion = "sheet2"
If fullArray(i, 2) >= 3 And fullArray(i, 2) < 6 Then
arrayInQuestion = "sheet3"
If fullArray(i, 2) >= 6 And fullArray(i, 2) <= 10 Then
arrayInQuestion = "sheet4"


Then i have small cycle to work transfer data to array:

Code:
If fullArray(i, 2) >= 3 And fullArray(i, 2) < 6 Then
If (Not sheet3) = -1 Then
positionX = 1
Else: positionX = UBound(sheet3, 2) + 1
End If
ReDim Preserve sheet3(1 To 2, 1 To positionX)
For k = 1 To lastColumn - 1
sheet3(k, UBound(sheet3, 2)) = fullArray(i, k)
Next k
End If

Currently i have 3 ifs that all run the same procedure, except target different arrays. Is there a way i can use the initial arrayInQuestion setter to acquire a variable i can use in my code?

Something like:

Code:
If (Not arrayInQuestion) = -1 Then
positionX = 1
Else: positionX = UBound(arrayInQuestion, 2) + 1
End If
ReDim Preserve arrayInQuestion(1 To 2, 1 To positionX)
For k = 1 To lastColumn - 1
sheet2(k, UBound(sheet2, 2)) = fullArray(i, k)
Next k
fullArray(i, 3) = "Sheet2"
End If
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
The solution was to use a function:

Code:
If fullArray(i, 2) >= 1 And fullArray(i, 2) < 3 Then sheet2() = fillUpArray(sheet2())
If fullArray(i, 2) >= 3 And fullArray(i, 2) < 6 Then sheet3() = fillUpArray(sheet3())
If fullArray(i, 2) >= 6 And fullArray(i, 2) <= 10 Then sheet4() = fillUpArray(sheet4())

Code:
Function fillUpArray(arrayInQuestion() As Variant)
If (Not arrayInQuestion) = -1 Then
positionX = 1
Else: positionX = UBound(arrayInQuestion, 2) + 1
End If
ReDim Preserve arrayInQuestion(1 To 2, 1 To positionX)
For k = 1 To lastColumn - 1
arrayInQuestion(k, UBound(arrayInQuestion, 2)) = fullArray(i, k)
Next k
fillUpArray = arrayInQuestion()
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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