Solved

set ranges in Excel using VBA

Posted on 2015-02-19
9
81 Views
Last Modified: 2016-02-10
I have a spreadsheet that has numerous set named ranges, so in Name Manager for instance I would like to call row AB, AB and the range I need is   ='Raw Data'!$AB1:$AB64000 [I have 17 Ranges I need to set]

I have tried to make this work through VBA but failed miserably. I tried

ActiveWorkbook.Names("AB").RefersToR1C1 = "='Raw Data'!R[1]C28:R[64000]C28"

but when I ran this it returned an error "Application defined or object defined error"

Is there a way to get VBA to put these ranges in and make sure that when the new data is entered in the Raw Data sheet, the ranges do not change.

I say that because when I set up the ranges manually in Name Manager every time I put data in the Raw Data sheet the ranges did change. i.e. when I run the macro the first time the Raw data tab may have 1000 items next time 20000 items next time 50000 items. So that is why the ranges must remain the same every time.

here's hoping
0
Comment
Question by:Jagwarman
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 29

Expert Comment

by:gowflow
ID: 40618405
must remain the same like AB1:AB64000 ? or adapt to maximum in the workbook ?

If the same range you do
$AB$1:$AB$64000

if you want it to be floating you do
AB1:AB64000

Now as far as VBA I can help you with that but better if you post a workbook that have these names and will take it from there.

gowflow
0
 
LVL 49

Accepted Solution

by:
Rgonzo1971 earned 250 total points
ID: 40618408
Hi,

pls try

ActiveWorkbook.Names.Add Name:="AB", RefersTo:="='Raw Data'!$AB1:$AB64000"

Regards
0
 
LVL 29

Assisted Solution

by:gowflow
gowflow earned 250 total points
ID: 40618410
as far as your example pls try
ActiveWorkbook.Names.Add Name:="AB", RefersTo:="='Raw Data'!R[1]C28:R[64000]C28"

or

 ActiveWorkbook.Names.Add Name:="AB", RefersTo:="='Raw Data'!$AB$1:$AB$64000"

gowflow
0
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 
LVL 12

Expert Comment

by:FarWest
ID: 40618424
I think the best way is to have a sheet that defines all ranges needed as stings
for example in Z1 = put the values "$AB$1:$AB$64000" in Z2 you put the values "$AC$1:$AC$64000" and so on

then you define names using Names Manager by putting "=INDIRECT($Z$1)" in Refers to
this way you have named that can be dynamically modified using other cells values
0
 

Author Comment

by:Jagwarman
ID: 40618431
Hi Rgonzo thought you had left EE?

When I run your the first time it works fine. When I run it the second tome it moves the range to

='Raw Data'!$AB14:$AB64013 and the next time to

='Raw Data'!$AB20:$AB64019

bizzare eh!
0
 
LVL 29

Expert Comment

by:gowflow
ID: 40618450
for it not to change it need to be absolute values again
ActiveWorkbook.Names.Add Name:="AB", RefersTo:="='Raw Data'!$AB$1:$AB$64000"

gowflow
0
 

Author Comment

by:Jagwarman
ID: 40618454
Hi Rgonzo I just notices the $ was missing so it does work
0
 

Author Comment

by:Jagwarman
ID: 40618455
Far West thanks for your reply but I prefer the others
0
 
LVL 12

Expert Comment

by:FarWest
ID: 40618461
You are welcome, and glad your problem is solved
see you in next question  :)
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

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 tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

773 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