简体   繁体   中英

Excel VBA code to highlight rows for a specific cell

I have created an spreadsheet with 3 sheets:

Sheet1 - Search Sheet2 - Add Data Sheet3 - Database

I want to create a button (Update) to highlight rows (green) for value of cell D4 Sheet1 and highlight rows in Sheet3 (in column 1 i have data which i need to insert in D4 to highlight). I have created one button but everytime just highlight the last row from Sheet3

Private Sub MDAno_Click()
currentrow = Sheet3.Cells(1, 1).CurrentRegion.Rows.Count

If Len(Range("D4")) <> 0 Then

Sheet3.Cells(currentrow, 498) = "Investigating"

End If

End Sub

I have a condition in Sheet3 (if i have "investigating" in column 498 highlight row). Can somebody help me with this ? Thanks

Though it can be achieved with a simple conditional formatting, here is what you are trying to achieve...

Private Sub MDAno_Click()
Dim lr As Long, i As Long
Dim ID
lr = Sheet3.Cells(1, 1).CurrentRegion.Rows.Count
ID = Sheet1.Range("D4").Value
If ID <> "" Then
    For i = 2 To lr
        If Sheet3.Range("A" & i) = ID Then
            Sheet3.Cells(i, 498) = "Investigating"
        Else
            Sheet3.Cells(i, 498) = ""
        End If
    Next i
End If
End Sub

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