Link to home
Start Free TrialLog in
Avatar of Jagwarman
Jagwarman

asked on

set ranges in Excel using VBA

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
Avatar of Jacques Geday
Jacques Geday
Flag of Canada image

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
ASKER CERTIFIED SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of FarWest
FarWest

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
Avatar of Jagwarman

ASKER

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!
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
Hi Rgonzo I just notices the $ was missing so it does work
Far West thanks for your reply but I prefer the others
You are welcome, and glad your problem is solved
see you in next question  :)