Redefining a named range in Excel using VBA

bisel

Board Regular
Joined
Jan 4, 2010
Messages
249
Office Version
  1. 365
Platform
  1. Windows
Hello All,

I tried searching and cannot find an answer ... hoping someone can help.

I have a macro where I define named ranges. The VBA code works fine ...

VBA Code:
:
Select Case Range("allocation_type").Value
    Case Is = "Do Not Include"
            ActiveWorkbook.Names("allocation_unit_table").RefersToR1C1 = "='Assessment Allocation'!R46C47"              ' Set range to single blank cell to suppress unit table in report
            ActiveWorkbook.Names("allocation_summary_table").RefersToR1C1 = "='Assessment Allocation'!R47C47"           ' Set range to single blank cell to suppress summary table in report
    Case Is = "Uniform Rate"
            ActiveWorkbook.Names("allocation_unit_table").RefersToR1C1 = "='Assessment Allocation'!R46C47"              ' Set range to single blank cell to suppress unit table in report
            ActiveWorkbook.Names("allocation_summary_table").RefersToR1C1 = "='Assessment Allocation'!R10C34:R44C35"    ' Set range to the summary table for uniform rate to print in the report
    Case Is = "Variable Rate"
            ActiveWorkbook.Names("allocation_unit_table").RefersToR1C1 = "='Assessment Allocation'!R50C32:R65C38"       ' Set range to the unit table for variable rate to print in the report
            ActiveWorkbook.Names("allocation_summary_table").RefersToR1C1 = "='Assessment Allocation'!R10C34:R44C36"    ' Set range to the summary table for variable rate to print in the report
    Case Is = "Hybrid/Blended Rate"
            ActiveWorkbook.Names("allocation_unit_table").RefersToR1C1 = "='Assessment Allocation'!R71C32:R87C38"       ' Set range to the unit table for hybrid rate to print in the report
            ActiveWorkbook.Names("allocation_summary_table").RefersToR1C1 = "='Assessment Allocation'!R10C39:R44C43"    ' Set range to the summary table to print in the report
    Case Else
            ActiveWorkbook.Names("allocation_unit_table").RefersToR1C1 = "='Assessment Allocation'!R46C47"              ' Set range to single blank cell to suppress space in report
            ActiveWorkbook.Names("allocation_summary_table").RefersToR1C1 = "='Assessment Allocation'!R47C47"           ' Set range to single blank cell to suppress space in report
End Select
:

What I want to do is to define the named ranges using a variable. I will define the row number and column number using a variable. For example ...

instead of ...

VBA Code:
:
ActiveWorkbook.Names("allocation_unit_table").RefersToR1C1 = "='Assessment Allocation'!R50C32:R65C38" 
:

I want to do something like this ...
VBA Code:
:
ActiveWorkbook.Names("allocation_unit_table").RefersToR1C1 = "='Assessment Allocation'!R50C32:rowandcolumnnumber" 
:

where "rowandcolumnnumber" is the row and column number that I would define depending on user input that would change the value of a target cell.

Hope someone can assist me.

Regards,

Steve
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Literal text goes inside quotes, variables go outside, and sewn together with &.

So if you had variables named rownum and column, your code should look like this:
VBA Code:
ActiveWorkbook.Names("allocation_unit_table").RefersToR1C1 = "='Assessment Allocation'!R50C32:R" & rownum & "C" & colnum
 
Upvote 0

Forum statistics

Threads
1,224,815
Messages
6,181,135
Members
453,021
Latest member
Justyna P

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