Solved

vba add range name on demand

Posted on 2014-04-15
5
419 Views
Last Modified: 2014-04-15
Dear Experts,

I have an issue whereby I am trying to specify a range name on the fly but I cannot seem to get it to work the way I want.

To make this clear, I do not want to use formula's or anything to do with CurrentRegion as my range name will commence from row 2 as the worksheet has headers.

Enclosed is my existing code:

 With shtFrom.Range("tbl_import")

'collect last row
                lastrow = shtFrom.Cells(2).CurrentRegion.Columns(1).SpecialCells(xlCellTypeVisible).Count - 1
                
                'Range("A2 " & L & lastrow).Select
                csb_import = Range("A2:L & lastrow)
                ActiveWorkbook.Names.Add Name:="csb_import", RefersTo:=csb_import
         
                'allocate from range
                Set rngFrom = Range("csb_import")

Open in new window


Any ideas to help appreciated guys?
0
Comment
Question by:discogs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 45

Expert Comment

by:aikimark
ID: 40002847
assuming that csb_import is a range, all you lacked was a SET keyword:
With shtFrom.Range("tbl_import")

'collect last row
                lastrow = shtFrom.Cells(2).CurrentRegion.Columns(1).SpecialCells(xlCellTypeVisible).Count - 1
                
                'Range("A2 " & L & lastrow).Select
                Set csb_import = Range("A2:L & lastrow)
                ActiveWorkbook.Names.Add Name:="csb_import", RefersTo:=csb_import
         
                'allocate from range
                Set rngFrom = Range("csb_import")

Open in new window

0
 
LVL 45

Expert Comment

by:aikimark
ID: 40002848
You might simplify your code as follows, unless you need to use the csb_import range.
With shtFrom.Range("tbl_import")

'collect last row
                lastrow = shtFrom.Cells(2).CurrentRegion.Columns(1).SpecialCells(xlCellTypeVisible).Count - 1
                
                ActiveWorkbook.Names.Add Name:="csb_import", RefersTo:=Range("A2:L & lastrow)
         
                'allocate from range
                Set rngFrom = Range("csb_import")

Open in new window

0
 

Author Comment

by:discogs
ID: 40002851
Hi Mark.

Thanks for the help.

Just to clarify, the line:

ActiveWorkbook.Names.Add Name:="csb_import", RefersTo:=Range("A2:L & lastrow)

Open in new window


I get an error. Should it not be:


Open in new window

ActiveWorkbook.Names.Add Name:="csb_import", RefersTo:=Range("A2:L & lastrow")

TA
0
 
LVL 45

Accepted Solution

by:
aikimark earned 500 total points
ID: 40002852
misplaced quote marks
ActiveWorkbook.Names.Add Name:="csb_import", RefersTo:=Range("A2:L" & lastrow)

Open in new window

0
 

Author Closing Comment

by:discogs
ID: 40002855
Legend! Thanks heaps champ.
TA
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question