Forum Discussion

hrh_dash's avatar
hrh_dash
Iron Contributor
Sep 03, 2022
Solved

VBA to select one column to another column

How do i write a macro to select one column to another one column with values?   For example; i have values from cell A1 to N1, would like to select column from A to N instead of just column N   ...
  • Harun24HR's avatar
    Sep 03, 2022

    hrh_dash To improve performance of macro we must try to avoid too many loops. Give a try on below codes.

     

    Sub HighlightCells()
    Dim ws              As Worksheet
    Dim lastRow         As Long
    Dim i               As Long
    Dim lastColumn      As Integer
    Dim j               As Long
    
        Set ws = ThisWorkbook.Sheets("TCA_consumer")
        lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
        lastColumn = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
        
            For i = 2 To lastRow
                If Cells(i, 14) = "Retain" Then
                    Range("A" & i, Cells(i, lastColumn)).Interior.ColorIndex = 4
                      Else
                    Range("A" & i, Cells(i, lastColumn)).Interior.ColorIndex = 5
                End If
            Next i
    End Sub

     

     

Resources