Defining a Variable for Columns for Use in Select Case

Fromlostdays1

New Member
Joined
Jul 18, 2022
Messages
14
Office Version
  1. 365
Platform
  1. Windows
Intro: I have a lot of data in columns in Excel. I can already use:

Select Case Range ("A1").value to give me the value of A1 and use that to do various things with. For instance, if I wanted to return a message based on the data in A1:

Code:
Select Case Range ("A1").value
Case ""
Case "Yes"
MsgBox "This is true"
Case "No"
MsgBox "This is not true!"

Problem / Question:

What I need to do is somehow define the column as a variable, I think, so that I can somehow define the column on the fly somewhere else, but leave the row number in tact. I would prefer to be able to somehow define the column as text in a defined cell I could just fill in on the fly.

This is what I have so far, but I don't think I'm even close to getting it right. In this example I'm trying to define a column in cell A2 and then have the code search A2, then run in the specificied column always on row 20:

VBA Code:
Dim ColNum As Integer
'Define Column as a variable
ColNum = Range("A2").Value
'Tell Excel where to look for which Column I want the code to run in, in this case I was hoping I could write a letter in box A2 and that letter would define which column the code would run in
 
Select Case Range(ColNum & "20").Value
'This is where I'm really lost. How do I tell the Select case to look for the variable column letter I defined in A2 but also always use row 20?

Case ""
Case "Yes"
MsgBox "This is true"
Case "No"
MsgBox "This is not true!"

Thanks for any advice!
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
I'm sorry I'm a total dumb dumb. Sometimes just typing it out, helps. I defined ColNum as an Integer above, not a String.

This works:

VBA Code:
Dim ColNum As String
'Define Column as a variable
ColNum = Range("A2").Value
'Type the Column you want to run the code on in cell A2
 
Select Case Range(ColNum & "20").Value
'Grabs the Column you specified in A2 and the info in row 20
Case ""
Case "Yes"
MsgBox "This is true"
Case "No"
MsgBox "This is not true!"
 
Upvote 0
Solution

Forum statistics

Threads
1,214,976
Messages
6,122,539
Members
449,088
Latest member
RandomExceller01

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