[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 161
  • Last Modified:

Trying to name a range of cells as "database" in a macro

When I try to name a range of cells as "database" in a marco sequence, Visual Basic gives me the following result:    

    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    ActiveWorkbook.Names.Add Name:="database", RefersToR1C1:= _
        "='2521'!R31C3:R6651C6"

My problem is I want to use the sequence with various different workbooks. This macro was created with an imported document named '2521' It seems if I want to use the macro with another document, Visual Basic still returns the information based on the earlier document namd.

I did select "relative reference" when I used the macro but that doesn't seem to solve the problem.

I would very much appreciate any help you could provide.

Thank you

Barry
0
BarryOTO
Asked:
BarryOTO
  • 2
1 Solution
 
DennisBorgCommented:
We can use the name of the active worksheet instead of that specific worksheet. Modify the last line as follows:

   ActiveWorkbook.Names.Add Name:="database", RefersToR1C1:= _
       "=" & ActiveWorksheet.Name & "!R31C3:R6651C6"


That should do the trick for you.


-Dennis Borg
0
 
BarryOTOAuthor Commented:
The answer gave me all the information I needed to solve my problem.
0
 
DennisBorgCommented:
Thank you, Barry. I'm glad I could help.


-Dennis Borg
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now