vba any way to make macro smaller

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
984
Office Version
  1. 2010
Platform
  1. Windows
Hi.
I did this macro, I would like to know if there are a way to write this with less lines

VBA Code:
sub highL_Dup()
Range("R2:R37").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=COUNTIF($B$26:$G$26,R2)"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.Color = 65535
End With
Selection.FormatConditions(1).StopIfTrue = False
End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Like this
VBA Code:
sub highL_Dup()
With Range("R2:R37")
    .FormatConditions.Add Type:=xlExpression, Formula1:="=COUNTIF($B$26:$G$26,R2)"
    .FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    .FormatConditions(1).Interior.Color = 65535
    .FormatConditions(1).StopIfTrue = False
End With
End Sub
You could remove 2 more lines, but it would mean making the rest longer so would be completely pointless.
 
Upvote 0
How about
VBA Code:
Sub highL_Dup()
   With Range("R2:R37")
      .FormatConditions.Add Type:=xlExpression, Formula1:="=COUNTIF($B$26:$G$26,R2)"
      .FormatConditions(.FormatConditions.Count).SetFirstPriority
      .FormatConditions(1).Interior.Color = 65535
      .FormatConditions(1).StopIfTrue = False
   End With
End Sub
 
Upvote 0
and just for fun

VBA Code:
Sub highL_Dup()
    Dim FormatCond As FormatCondition
    Dim rng As Range
    Set rng = Range("R2:R37")
    rng.FormatConditions.Delete
    Set FormatCond = rng.FormatConditions.Add(Type:=xlExpression, Formula1:="=COUNTIF($B$26:$G$26,R2)")
    FormatCond.Interior.Color = 65535: FormatCond.StopIfTrue = False: FormatCond.SetFirstPriority
End Sub

Dave
 
Upvote 0
Like this
VBA Code:
sub highL_Dup()
With Range("R2:R37")
    .FormatConditions.Add Type:=xlExpression, Formula1:="=COUNTIF($B$26:$G$26,R2)"
    .FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    .FormatConditions(1).Interior.Color = 65535
    .FormatConditions(1).StopIfTrue = False
End With
End Sub
You could remove 2 more lines, but it would mean making the rest longer so would be completely pointless.
Thank you Sir.
 
Upvote 0
How about
VBA Code:
Sub highL_Dup()
   With Range("R2:R37")
      .FormatConditions.Add Type:=xlExpression, Formula1:="=COUNTIF($B$26:$G$26,R2)"
      .FormatConditions(.FormatConditions.Count).SetFirstPriority
      .FormatConditions(1).Interior.Color = 65535
      .FormatConditions(1).StopIfTrue = False
   End With
End Sub
Thank you Sir, I really appreciate you take time for me.
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0
thanks to your ideas I did this and work. Team is always better.
VBA Code:
sub
Range("T2:T37").FormatConditions.Add Type:=xlExpression, Formula1:="=COUNTIF($B$25:$G$25,T2)"
With Selection.FormatConditions(1).Interior
.Color = 65535
End With
end sub
 
Upvote 0
A word of warning, if you already have a CF rules for T2:T37 when you run that code, it will not work properly.
 
Upvote 0

Forum statistics

Threads
1,214,430
Messages
6,119,447
Members
448,898
Latest member
drewmorgan128

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