Solved

vba add range name on demand

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

747 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

12 Experts available now in Live!

Get 1:1 Help Now