1

I'm a beginner in vba and I would like a script for add 1 rule on all line(201 lines).

My counter is i and I want insert this i for an automatic increment in my worksheet

I don't know why I can't insert a variable?

Sub test()
    Dim i As Byte
    i = 2

    While (i <= 202)
        Range("Hi,Hi:Ji,Mi:Pi").Select      '<--- insert my "i" here
        Range("Mi").Activate '<---here

        Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlNotEqual, _
        Formula1:="=$G$i" '< ---here

        Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority

        With Selection.FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .Color = 192
            .TintAndShade = 0
        End With

        Selection.FormatConditions(1).StopIfTrue = False

        i = i + 1
    Wend
End Sub
Community
  • 1
  • 1
Skunk
  • 85
  • 1
  • 11

1 Answers1

1

Range("Hi,Hi:Ji,Mi:Pi") with variable can be written as

Range("H" & i & ",H" & i & ":J" & i & ",M" & i & ":P" & i)

Similarly for the rest.

Basically Range("A1") can be written as Range("A" & i)

Also you do not need to use .Select to perform an action. In most of the cases, you can directly work with the object. You may want to see THIS

Community
  • 1
  • 1
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250