Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

vba add range name on demand

Posted on 2014-04-15
5
Medium Priority
?
424 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 46

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 46

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 46

Accepted Solution

by:
aikimark earned 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

715 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