Explicit Range vs. Range variable?

jvanbonn

Board Regular
Joined
Mar 11, 2011
Messages
71
Will someone please tell me why these two codes are NOT equivalent?

Code 1 works fine, but does not allow for a dynamic range selection.
Code:
Sub CreateConditionalFormat(senior As String)

Sheets(senior).Cells.FormatConditions.Delete
  [COLOR=Blue]With[/COLOR] Sheets(senior).[COLOR=Red][B][B5:J46][/B][/COLOR].FormatConditions _
    .Add(xlExpression, xlExpression, "=$A5=True")
    [COLOR=Blue]With[/COLOR] .Font
      .Color = RGB(90, 90, 90)
      .Italic = [COLOR=Blue]True[/COLOR]
      .Strikethrough = [COLOR=Blue]True[/COLOR]
[COLOR=Blue]    End With
  End With
End Sub[/COLOR]
Code 2, I get an Object doesn't support this property or method run-time error.
Code:
Sub CreateConditionalFormat(senior As String)
'
' CreateStrikeGreyCondition Macro
'
[B][COLOR=Blue]Dim[/COLOR] [COLOR=Red]myRng [/COLOR][COLOR=Blue]As[/COLOR] [COLOR=Red]Range[/COLOR]

[COLOR=Blue]Set[/COLOR][/B] [COLOR=Red][B]myRng = Sheets(senior).[B5].CurrentRegion[/B][/COLOR]

Sheets(senior).[COLOR=Red][B]myRng[/B][/COLOR].FormatConditions.Delete
  [COLOR=Blue]With[/COLOR] Sheets(senior).myRng.FormatConditions _
    .Add(xlExpression, xlExpression, "=$A5=True")
    [COLOR=Blue]With[/COLOR] .Font
      .Color = RGB(90, 90, 90)
      .Italic = [COLOR=Blue]True[/COLOR]
      .Strikethrough = [COLOR=Blue]True[/COLOR]
[COLOR=Blue]    End With
  End With
End Sub[/COLOR]
As always, with deep respect & gratitude...
Thank you!
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Leave out:

Sheets(senior).

in:

Sheets(senior).myRng.FormatConditions.Delete

in code 2 and it will work too.
 
Upvote 0
Thank you, thank you, thank you!

It seems like it's always the obvious that stumps me. The difficult stuff, I can usually figure out.

It makes complete sense why I should delete "Sheets(senior)." when I "Set myRng = Sheets(senior).[B5].CurrentRegion"

Thanks again!:biggrin:
 
Upvote 0

Forum statistics

Threads
1,224,591
Messages
6,179,767
Members
452,940
Latest member
rootytrip

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