VBA, how to reference several cells in one line [ tidy code ]

  • How can i make this tidier, the part where i change the colour of the cells towards the bottom, i am just repeating code, can I just select the desired ranges in one line?


  • Re: VBA, how to reference several cells in one line [ tidy code ]


    Yes you can tidy this up a fair bit. Try to avoid using 'select' on ranges. It is unncessary and slows down the code. It is better to refer to the range directly or use a 'with' statement.


    Try this:

    Code
    If List.Exists(.Cells(Rw, "A").Value) Then
                .Cells(Rw, 2).Resize(, 3).ClearContents
                With .Cells(Rw, 3).Resize(, 3).Interior
                    .Pattern = xlSolid
                    .PatternThemeColor = xlThemeColorDark1
                    .ThemeColor = xlThemeColorDark1
                    .TintAndShade = -0.149998474074526
                    .PatternTintAndShade = -0.149998474074526
                End With
            End If

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!