Redefining a named range in Excel VBA

bisel

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

I have a workbook with some named ranges. Depending on cell's value, I want to change the range values of the named range. Here is the code that I have tried without success ...


VBA Code:
:
Select Case Sheet23.Range("allocation_type").Value
    Case Is = "Do Not Include"
            ActiveWorkbook.Names("allocation_unit_table").RefersTo = Sheet23.Range("AG43")                   ' Set range to single blank cell 
            ActiveWorkbook.Names("allocation_summary_table").RefersTo = Sheet23.Range("AG44")           ' Set range to single blank cell 
    Case Is = "Uniform Rate"
            ActiveWorkbook.Names("allocation_unit_table").RefersTo = Sheet23.Range("AG43")                    ' Set range to single blank cell 
            ActiveWorkbook.Names("allocation_summary_table").RefersTo = Sheet23.Range("AG44")            ' Set range to single blank cell 
    Case Is = "Variable Rate"
            ActiveWorkbook.Names("allocation_unit_table").RefersTo = Sheet23.Range("AE7:AK22")              ' Set range to the unit table for variable rate 
            ActiveWorkbook.Names("allocation_summary_table").Refers = Sheet23.Range("B50:O83")           ' Set range to the summary table for variable rate 
    Case Is = "Hybrid/Blended Rate"
            ActiveWorkbook.Names("allocation_unit_table").RefersTo = Sheet23.Range("AE25:AK40")            ' Set range to the unit table for hybrid rate 
            ActiveWorkbook.Names("allocation_summary_table").Refers = Sheet23.Range("B91:Q124")         ' Set range to the summary table for hybrid rate
    Case Else
            ActiveWorkbook.Names("allocation_unit_table").RefersTo = Sheet23.Range("AG43")                     ' Set range to single blank cell 
            ActiveWorkbook.Names("allocation_summary_table").RefersTo = Sheet23.Range("AG44")             ' Set range to single blank cell 
End Select
:

I have tried variations of the above without success. I notice that with the "ActiveWorkbook.Names ... " there are several variations such as .Names.Item. And with .RefersTo there are variations such as .RefersToRange. I have tried several things but nothing is working as I would like. Any help is appreciated.

Regards,

Steve
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Something like this...maybe.
VBA Code:
Dim allocationType As String
allocationType = Sheet23.Range("allocation_type").Value

Dim unitTableRange As Range
Dim summaryTableRange As Range

Select Case allocationType
    Case "Variable Rate"
        Set unitTableRange = Sheet23.Range("AE7:AK22")
        Set summaryTableRange = Sheet23.Range("B50:O83")
    Case "Hybrid/Blended Rate"
        Set unitTableRange = Sheet23.Range("AE25:AK40")
        Set summaryTableRange = Sheet23.Range("B91:Q124")
    Case "Do Not Include", "Uniform Rate", Else
        Set unitTableRange = Sheet23.Range("AG43")
        Set summaryTableRange = Sheet23.Range("AG44")
End Select

ActiveWorkbook.Names("allocation_unit_table").RefersTo = unitTableRange
ActiveWorkbook.Names("allocation_summary_table").RefersTo = summaryTableRange
 
Upvote 0
Solution
Something like this...maybe.
VBA Code:
Dim allocationType As String
allocationType = Sheet23.Range("allocation_type").Value

Dim unitTableRange As Range
Dim summaryTableRange As Range

Select Case allocationType
    Case "Variable Rate"
        Set unitTableRange = Sheet23.Range("AE7:AK22")
        Set summaryTableRange = Sheet23.Range("B50:O83")
    Case "Hybrid/Blended Rate"
        Set unitTableRange = Sheet23.Range("AE25:AK40")
        Set summaryTableRange = Sheet23.Range("B91:Q124")
    Case "Do Not Include", "Uniform Rate", Else
        Set unitTableRange = Sheet23.Range("AG43")
        Set summaryTableRange = Sheet23.Range("AG44")
End Select

ActiveWorkbook.Names("allocation_unit_table").RefersTo = unitTableRange
ActiveWorkbook.Names("allocation_summary_table").RefersTo = summaryTableRange

Thank you very much. Your solution did the trick. And so simple too!

Best regards,

Steve
 
Upvote 0

Forum statistics

Threads
1,217,389
Messages
6,136,316
Members
450,003
Latest member
AnnetteP

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