Create a Variable Number of Variables reliant on count in vba

dellehurley

Board Regular
Joined
Sep 26, 2009
Messages
171
Office Version
  1. 365
Platform
  1. Windows
Hi
I was wondering if it is possible to create a variable number of variables. If I count the number of times something happens that I can create a variable that many times. ie, count =10 and variable the named variable in this case y is created 10 times = y1,y2,y3,y4 etc up to 10, or a 1000 or as many times as needed. Something like the control function.
VBA Code:
Sub VariableVariables()
Dim x As Integer
Dim Cnt As Integer
Dim y As String

Cnt = Application.WorksheetFunction.CountIf(Ws.Range("A:A"), x)

    Do Until cnt = 0
        y& Cnt = something
        'create variable that looks something like this... y1, y2, y3 etc
        'do something
        Cnt = Cnt - 1
    Loop
End Sub

I hope this makes some sort of sense.
Dannielle
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hi,
to do what you want use an array which can be sized dynamically to have as many elements as needed

VBA Code:
Sub VariableVariables()
    Dim y() As Variant
    Dim Cnt As Long, x As Long, i  As Long
    Dim ws As Worksheet
   
    Set ws = ActiveSheet
   
    x = 10
  
    Cnt = Application.WorksheetFunction.CountIf(ws.Range("A:A"), x)
   
    ReDim y(1 To Cnt)
   
    For i = 1 To UBound(y)
        y(i) = i ' add value to elements
        'y(1), y(2), y(3) etc
        'do something
        MsgBox y(i)
    Next i
   
End Sub

you can read more here: Using arrays (VBA)

Dave
 
Last edited:
Upvote 0
Like this perhaps:

VBA Code:
Dim y() As String

cnt = Application.WorksheetFunction.CountIf(ws.Range("A:A"), x)

If cnt > 1 Then
    ReDim y(1 To cnt)
    For i = 1 To UBound(y)
        'do things with y(i)
    Next i
Else
    '??
End If
 
Upvote 0
Hi,
to do what you want use an array which can be sized dynamically to have as many elements as needed

VBA Code:
Sub VariableVariables()
    Dim y() As Variant
    Dim Cnt As Long, x As Long, i  As Long
    Dim ws As Worksheet
  
    Set ws = ActiveSheet
  
    x = 10
 
    Cnt = Application.WorksheetFunction.CountIf(ws.Range("A:A"), x)
  
    ReDim y(1 To Cnt)
  
    For i = 1 To UBound(y)
        y(i) = i ' add value to elements
        'y(1), y(2), y(3) etc
        'do something
        MsgBox y(i)
    Next i
  
End Sub

you can read more here: Using arrays (VBA)

Dave
Thanks for your help again Dave. I knew there had to be away to get around writing to a cell or the like. Stephen's post looks like it will do the job too.
I will go and play now. Enjoy your day.
Dannielle
 
Upvote 0
Like this perhaps:

VBA Code:
Dim y() As String

cnt = Application.WorksheetFunction.CountIf(ws.Range("A:A"), x)

If cnt > 1 Then
    ReDim y(1 To cnt)
    For i = 1 To UBound(y)
        'do things with y(i)
    Next i
Else
    '??
End If
Hi Stephen
Thanks for your help, as I said to Dave I knew there had to be away to do this. Both of your option and Dave's look great, I will go fiddle and find which works for me best.
Enjoy your day.
Dannielle
 
Upvote 0

Forum statistics

Threads
1,223,970
Messages
6,175,707
Members
452,667
Latest member
vanessavalentino83

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