Can you uses a string variable as the name of a new variable?

DropTables

New Member
Joined
Sep 14, 2022
Messages
10
Office Version
  1. 365
Platform
  1. Windows
Hello, long time lurker first time poster. Still fairly new to VBA. I am wondering if it is possible to put a string variable in a dimension statement.

I am trying to write a for loop that will store the column letter for each column in a sheet based on the text in the header row ("C_" & [header text]. For example, the ID column will be a string viable named "C_ID" which will store "A". I am currently doing this manually for each column, so I don't have to rely on the columns positions when I want to reference a column. The purpose is so the code doesn't break when I add or remove columns. I am mostly using these variable in in range statements (eg WS.range(C_ID & [row]). Open to alternative ways to accomplish the same.

My first go at try this is below. It thinks I am trying to declare ColumnVarName a second time instead of using the string value.


Dim WS As Worksheet
Dim CurrentColumn As Long ' Current Column Number
Dim ColumnLetter As String ' Current Column Letter
Dim ColumnTitle As String
Dim NumberofColumns As Long
NumberofColumns = WS.Cells(1, Columns.Count).End(xlToLeft).Column
Dim VariableName As String


For CurrentColumn = 1 To NumberofColumns

ColumnLetter = Split(WS.Cells(1, CurrentColumn).Address, "$")(1) 'Convert current iteration to column letter
ColumnTitle = WS.Range(ColumnLetter & 1).Value 'Sets Column title as header text
VariableName = "C_" & ColumnTitle 'Sets VariableName to what I want my variable to be named
Dim VariableName As String ' 'Declare new variable named after sting value ; Duplicate declaration error
VariableName = COLLetter(WS_BuySell, ColumnTitle, BS_LCOL, 1)
Next
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
whoops just realized I could post it this way.

VBA Code:
Dim CurrentColumn As Long ' Current Column Number
Dim ColumnLetter As String ' Current Column Letter
Dim ColumnTitle As String
Dim NumberofColumns As Long
    NumberofColumns = WS.Cells(1, Columns.Count).End(xlToLeft).Column
Dim VariableName As String


For CurrentColumn = 1 To NumberofColumns
    'Convert current iteration to column letter
    ColumnLetter = Split(Cells(1, CurrentColumn).Address, "$")(1)
    'Sets Column title as header text
    ColumnTitle = WS.Range(ColumnLetter & 1).Value
    'Sets VariableName to what I want my variable to be named
    VariableName = "C_" & ColumnTitle
    'Declare new variable from sting variable
    Dim VariableName As String
        VariableName = COLLetter(WS_BuySell, ColumnTitle, BS_LCOL, 1)
Next
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,387
Members
449,080
Latest member
Armadillos

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