simple (I think?) question about assigning a constant to represent a column

kbishop94

Active Member
Joined
Dec 5, 2016
Messages
429
Office Version
  1. 2010
Platform
  1. Windows
I apologize for asking such a remedial and simple question, but I couldn’t really find the exact answer using google and its only making me more confused lol

Throughout my code in the spreadsheet I am using the furthest out column that is being used on the spreadsheet (column “IR”) in my code… example:

For Each cell In Range("C4:IR4").Cells

The problem is, when a situation arises where I’ll have to insert a new column (which won’t be very often, but it will eventually happen) I’ll have to go back in to the code and manually change all the “IR4” ‘s to “IS4” and so on… Now the “C” column will not ever change, and the row (4) will not ever change… Only the “IR” column will. Now, I understand the concept(kinda) on assigning a CONSTANT to represent the farthest out column (IR in this case) that is being utilized in the spreadsheet, but what I do not know is how to actually put that into code along with exactly how the code is written every time “IR” is referenced throughout all the code. ?? Thanks
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
12,667
Office Version
  1. 2013
  2. 2010
Platform
  1. Windows
Try:
Code:
Dim lCol As Long
    lCol = ActiveSheet.UsedRange.Columns.Count
    For Each cell In Range(Cells(4, 3), Cells(4, lCol))
 
Upvote 0

kbishop94

Active Member
Joined
Dec 5, 2016
Messages
429
Office Version
  1. 2010
Platform
  1. Windows
Try:
Code:
Dim lCol As Long
    lCol = ActiveSheet.UsedRange.Columns.Count
    For Each cell In Range(Cells(4, 3), Cells(4, lCol))

I apologize but I think I am not entering it correctly. I am getting the error " Invalid Outside Procedure" (?)
 
Upvote 0

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
66,925
Office Version
  1. 365
Platform
  1. Windows
I apologize but I think I am not entering it correctly. I am getting the error " Invalid Outside Procedure" (?)
What mumps gave you was just past of code to replace the one line you had:
Code:
[COLOR=#333333]For Each cell In Range("C4:IR4").Cells[/COLOR]

Did you place his code within a Sub Procedure (between a "Sub ..." and "End Sub")?
That error message is something I would expect if you just pasted that section of code at random in the VB Editor and not within a Sub Procedure.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,190,789
Messages
5,982,924
Members
439,807
Latest member
WXM86

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
Top