简体   繁体   中英

Dynamic cell formatting in excel

I have a VBA that will pull rows from another spreadsheet. The problem is I am trying to dynamically format the cells that are populated with the rows information. At the moment I have formatted row 11 however, how can I set it so that any the rows are populated will be formatted in a white bordered cell. and when they are not populated they will just be greyed out.

Sub Searchdata()
Dim Lastrow As Long
Dim count As Integer
Lastrow = Sheets("Data").Cells(Rows.count, 1).End(xlUp).Row
Y = 11
For X = 2 To Lastrow
If Sheets("Data").Cells(X, 1) = Sheet3.Range("B3") Then
   Sheet3.Cells(Y, "A") = Sheets("Data").Cells(X, 1)
   Sheet3.Cells(Y, "B") = Sheets("Data").Cells(X, 2)
   Sheet3.Cells(Y, "C") = Sheets("Data").Cells(X, 3) & " " & Sheets("data").Cells(X, 4) _
                            & " " & Sheets("data").Cells(X, 5) & " " & Sheets("Data").Cells(X, 6)
   Sheet3.Cells(Y, "D") = Sheets("Data").Cells(X, 7)
   Y = Y + 1
End If
Next X
End Sub

在此处输入图片说明

Right after your if statement add the following lines:

    With range(Cells(Y, 1), Cells(Y, "D"))
        .Interior.ColorIndex = xlNone
        With .Borders
            .LineStyle = xlContinuous
            .Color = vbBlack
            .Weight = xlThin
        End With
    End With

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM