Solved

vba add range name on demand

Posted on 2014-04-15
5
412 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now