vba any way to make macro smaller

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
732
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

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.

jasonb75

Well-known Member
Joined
Dec 30, 2008
Messages
12,959
Office Version
  1. 365
Platform
  1. Windows
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.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
58,493
Office Version
  1. 365
Platform
  1. Windows
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
 

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
6,602
Office Version
  1. 2019
Platform
  1. Windows
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
 

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
732
Office Version
  1. 2010
Platform
  1. Windows

ADVERTISEMENT

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.
 

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
732
Office Version
  1. 2010
Platform
  1. Windows
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.
 

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
732
Office Version
  1. 2010
Platform
  1. Windows

ADVERTISEMENT

Thanks DMT, nice molecule. 32
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
58,493
Office Version
  1. 365
Platform
  1. Windows
Glad we could help & thanks for the feedback.
 

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
732
Office Version
  1. 2010
Platform
  1. Windows
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
58,493
Office Version
  1. 365
Platform
  1. Windows
A word of warning, if you already have a CF rules for T2:T37 when you run that code, it will not work properly.
 

Watch MrExcel Video

Forum statistics

Threads
1,133,428
Messages
5,658,736
Members
418,467
Latest member
sc356448

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
Top