VBA Ranges - Setting and Referencing

marteb

New Member
Joined
Sep 11, 2009
Messages
22
Hello,

I am trying to adapt a macro with hard coded ranges to use flexible ranges based on user input.
The macro creates a monthly calendar that I would like to use for my daily task pages. There are
14 different areas on the document where the calendar resides.

My problem is referencing the ranges once they are set. This is a portion of the code.

Code:
' sCell = Starting Cell - Input 1A, 1B, etc (1A first page, first calendar)
    Dim sCell As String
    
' Define Ranges
    Dim mTitle As Range
    Dim mLabel As Range
    Dim dTitle As Range
    Dim dlabel As Range
    
    sCell = InputBox("Enter Starting Cell")

' Beginning Case Statement - Set Ranges
Select Case sCell
    Case Is = "1A"
        Set mTitle = Range("a1")
        Set mLabel = Range("a1:g1")
        Set dTitle = Range("a2:g2")
        Set dlabel = Range("a2")
End Select

[B]Range("mTitle").NumberFormat = "mmmm"[/B]

With Range("mLabel")
           .HorizontalAlignment = xlCenterAcrossSelection
           .VerticalAlignment = xlCenter
           .Font.Size = 11
           .Font.Bold = True
           .RowHeight = 14.25
       End With
The code hits the error trap (not shown) at Range("mTitle"). Am I referencing the ranges incorrectly
or is the setup wrong? The code works correctly with the hard coded ranges.

Thanks, Marteb
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Since you have already defined those ranges with names, you can refer to them like this (which should get rid if your error):
Code:
mTitle.NumberFormat = "mmmm"
 
With mLabel
    .HorizontalAlignment = xlCenterAcrossSelection
    .VerticalAlignment = xlCenter
    .Font.Size = 11
    .Font.Bold = True
    .RowHeight = 14.25
End With
 
Upvote 0

Forum statistics

Threads
1,224,891
Messages
6,181,614
Members
453,057
Latest member
LE102024

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