Dynamic range created vrom VBA

white_flag

Active Member
Joined
Mar 17, 2010
Messages
331
Hello

I have two dynamic ranges defined via VBA. But like this it is not going.
Code:
Option Explicit
Public Sub list_name()
    ActiveWorkbook.Names.Add Name:="name1", RefersTo:="=OFFSET(data!$B$3,0,0,COUNTA(data!$B3:$B20),1)"
    ActiveWorkbook.Names.Add Name:="name2", RefersTo:="=OFFSET(data!$B$26,0,0,COUNTA(data!$B26:$B38),1)"
       With Sheets("Calculation").Range("C6").Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
                xlBetween, Formula1:="=name1"
            .IgnoreBlank = True
            .InCellDropdown = True

       End With
       With Sheets("Calculation").Range("C7").Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
                xlBetween, Formula1:="=name2"
            .IgnoreBlank = True
            .InCellDropdown = True

       End With

End Sub
Then I put just one range "name1" and in excel the dynamic range become:
=OFFSET(data!$B$3,0,0,COUNTA(data!$B28:$B45),1) insted
=OFFSET(data!$B$3,0,0,COUNTA(data!$B3:$B20),1)

Code:
Option Explicit
Public Sub list_name()
    ActiveWorkbook.Names.Add Name:="name1", RefersTo:="=OFFSET(data!$B$3,0,0,COUNTA(data!$B3:$B20),1)"
       With Sheets("Calculation").Range("C6").Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
                xlBetween, Formula1:="=name1"
            .IgnoreBlank = True
            .InCellDropdown = True

       End With

End Sub
so what it is wrong?
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Your definition has relative references in it ... so the definition depends on what is the active cell when the definition is created. Either select an appropriate cell or change the definition.
 
Upvote 0
I add this line on VBA but it is not going:
Code:
    Worksheets("data").Activate
    ActiveWorkbook.Names.Add Name:="name1", RefersTo:="=OFFSET(data!$B$3,0,0,COUNTA(data!$B3:$B20),1)"
    ActiveWorkbook.Names.Add Name:="name2", RefersTo:="=OFFSET(data!$C$26,0,0,COUNTA(data!$C26:$C38),1)"
 
Upvote 0
You have not done either of my suggestions ... and you seem surprised that it is still not working. What would you have me suggest instead?????
 
Upvote 0
sorry. but my level of VBA is not so fantastic.

set active cell:

Code:
Worksheets("data").Range("B1").Activate

then will follow the definition ...

the offset are created correct now. but the second part it is with error


Code:
Public Sub list_name()
    Worksheets("data").Range("B1").Activate
    ActiveWorkbook.Names.Add Name:="name1", RefersTo:="=OFFSET(data!$B$3,0,0,COUNTA(data!$B3:$B20),1)"
    ActiveWorkbook.Names.Add Name:="name2", RefersTo:="=OFFSET(data!$C$26,0,0,COUNTA(data!$C26:$C38),1)"
       With Sheets("Calculation").Range("C6").Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
                xlBetween, Formula1:="=name1"
            .IgnoreBlank = True
            .InCellDropdown = True
       End With
'       With Sheets("Calculation").Range("C7").Validation
'            .Delete
'            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
'                xlBetween, Formula1:="=name2"
'            .IgnoreBlank = True
'            .InCellDropdown = True
'       End With

End Sub

so I can not insert in C7 an list
 
Upvote 0
You may have added the names correctly, but I still don't think that you realise what having relative references means. I recommend that you change your references to be absolute ( data!$B$3:$B$20 and data!$C$26:$C$38 ).
 
Upvote 0
ups ..my bad (I did not see). but for the second question:


Code:
'       With Sheets("Calculation").Range("C7").Validation
'            .Delete
'            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
'                xlBetween, Formula1:="=name2"
'            .IgnoreBlank = True
'            .InCellDropdown = True
'       End With

I recive an error 1004
 
Upvote 0
Try activating the Calculation sheet before that block of code.
 
Upvote 0

Forum statistics

Threads
1,224,506
Messages
6,179,159
Members
452,892
Latest member
yadavagiri

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top