Naming a range without selecting it

belle_the_cat

New Member
Joined
Jun 23, 2021
Messages
7
Office Version
  1. 365
  2. 2013
Platform
  1. Windows
  2. MacOS
Hi guys,

I want to name two ranges without using select, but I'm totally stuck. This is part of a longer macro so I'm just pasting the relevant bits.
This is what I'm trying to do, but without selecting:

VBA Code:
Range("A:B").Select
Range(Selection, Selection.End(xlDown))
Selection.Name = "importLookup"

Range("A:A").Select
Range(Selection,Selection.End(xlDown))
Selection.Name = "importNames"

The length of the list will vary, and I've already got a variable for the last row, I tried doing this:

Code:
Dim lrImport as Long
lrImport = Worksheets("Import").Range("A" & Rows.Count).End(xlUp).Row

Range("A:A" & lrImport).Name = importLookup
Range("A:B" & lrImport).Name = importNames

Which didn't work, presumably because I'm a huge idiot.

I keep looking at this thread, and I feel like it contains the answer, but I can't seem to figure out how to put it into practice.
Any help would be extremely appreciated!
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
The second bit should look like this:
Rich (BB code):
Dim lrImport as Long
lrImport = Worksheets("Import").Range("A" & Rows.Count).End(xlUp).Row

Range("A1:A" & lrImport).Name = importLookup
Range("A1:B" & lrImport).Name = importNames
 
Upvote 0
Try this:
VBA Code:
Dim lrImport as Long
lrImport = Worksheets("Import").Range("A" & Rows.Count).End(xlUp).Row

Range("A1:A" & lrImport).Name = "importLookup"
Range("A1:B" & lrImport).Name = "importNames"

What I changed:
  • Added "1" to the start of your range "A1:A..." - You were missing the start cell row value
  • I put your range name in quotes
I hope this works for you! Good luck!
 
Upvote 0
Try this:
VBA Code:
Dim lrImport as Long
lrImport = Worksheets("Import").Range("A" & Rows.Count).End(xlUp).Row

Range("A1:A" & lrImport).Name = "importLookup"
Range("A1:B" & lrImport).Name = "importNames"

What I changed:
  • Added "1" to the start of your range "A1:A..." - You were missing the start cell row value
  • I put your range name in quotes
I hope this works for you! Good luck!
Thank you so much!! I really appreciate your help. I've seriously been at this for hours, trying to figure out what I was doing wrong. :P
 
Upvote 0

Forum statistics

Threads
1,214,391
Messages
6,119,249
Members
448,879
Latest member
oksanana

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