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

kbishop94

Active Member
Joined
Dec 5, 2016
Messages
458
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
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

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Try:
Code:
Dim lCol As Long
    lCol = ActiveSheet.UsedRange.Columns.Count
    For Each cell In Range(Cells(4, 3), Cells(4, lCol))
 
Upvote 0
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
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,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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