I have a spreadsheet that is updated regularly. Therefore the column header positions change regularly. eg. today "Username" is column K, but tomorrow "Username" might be column L. I need to add a new column to the right of "Username" but where it changes I cannot refer to as cell/column reference.
So far I have:
Dim rngUsernameHeader As Range
Dim rngHeaders As Range
Set rngHeaders = Range("1:1") 'Looks in entire first row.
Set rngUsernameHeader = rngHeaders.Find("Username")
When I go to add a new column to the right of it, I'm selecting that row but it's going back to cell/column references...
Columns("K:K").Select
Selection.Insert Shift:=xlToRight
Range("K1").Select
ActiveCell.FormulaR1C1 = "Role"
How can I perform this step with a macro?
edit: I think need to give that Column a header name and begin populating the row with data - each time I do begins the cell references which I want to avoid wherever possible.
Many thanks in advance.
Something like this should work. The idea is that you locate the column and then you insert to the right. That is why you have the
+1
in theTestMe
. The functionl_locate_value_col
returns the column, where it has found the value. If you want, you may change the optional parameterl_row
, depending on which row do you want to look for.How about:
You could name your range:
Edit: Have rewritten so it inserts a column after your named column and returns that reference:
Untested code as not at my desktop