Named Ranges through VBA

Hanz77

Board Regular
Joined
Sep 21, 2006
Messages
87
I’m hoping someone can see where I’m messing up with my code. I’m trying to name several columns based on their heading name. The headings are in row three and the actual data starts in row 4 and goes down from there.

The problem I’m having is that the named ranges are changing based on the actively selected column. For example, if the column containing Countries is located in column H, my code will identify the column and properly name it “Country”. However, as soon as I select column K, the named references will then refer to column K, then if I select column M the named references will refer to column M.

Following is an example of my code to name two ranges, the actual code is attempting to name about 70 columns. The columns are not always in the same order, which is why I have a find function built in.

Code:
LastRow01 = [A65536].End(xlUp).Row
MyArea01 = "='" & ActiveSheet.Name & "'!R2:R" & LastRow01 & "C" & LastCol01
LastCol01 = Cells.Find(What:="Country", After:=[A3], LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext).Select
ActiveWorkbook.Names.Add Name:="Country", RefersToR1C1:=MyArea01

LastRow02 = [A65536].End(xlUp).Row
MyArea02 = "='" & ActiveSheet.Name & "'!R2:R" & LastRow02 & "C" & LastCol02
LastCol02 = Cells.Find(What:="Sales Person", After:=[A3], LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext).Select
ActiveWorkbook.Names.Add Name:="SalesPerson", RefersToR1C1:=MyArea02

Why is the column reference changing after this script has ran? Any thoughts on how to fix are appreciated.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Shouldn't you be setting/finding the value for LastColX before you use it when creating MyAreaX?
 
Upvote 0
How about a little different approach?

Code:
    Dim wks As Worksheet
    Dim r As Range
    
    Set wks = ActiveSheet
    With wks
        Set r = .Range(.Rows(3), .Rows(.Cells(.Rows.Count, "A").End(xlUp).Row))
        Application.DisplayAlerts = False
        r.CreateNames Top:=True
        Application.DisplayAlerts = True
    End With
 
Upvote 0

Forum statistics

Threads
1,216,050
Messages
6,128,498
Members
449,455
Latest member
jesski

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