I need help with the following code:
Sub highlightspecificvalue()
'highlight all cells containing a specified value`enter code here`
Dim fnd As String, firstfound As String
Dim foundcell As Range, rng As Range
Dim myrange As Range, lastcell As Range
'what value do you want to find
fnd = InputBox("i want to highlight cells containing...", "highlight")
'end if cancel button is clicked or no text is entered
If fnd = vbNullString Then Exit Sub
Set myrange = ActiveSheet.UsedRange
Set lastcell = myrange.cells(myrange.cells.Count)
Set foundcell = myrange.Find(what:=fnd, after:=lastcell)
'test to see if anything was found
If Not foundcell Is Nothing Then
firstfound = foundcell.Address
Else
GoTo nothingfound
End If
Set rng = foundcell
'loop until cycled through all unique finds
Do Until foundcell Is Nothing
'find next cell with fnd value
Set foundcell = myrange.FindNext(after:=foundcell)
'add found cell to rng range variable
Set rng = Union(rng, foundcell)
'test to see if cycled through to first found cell
If foundcell.Address = firstfound Then Exit Do
Loop
If IsEmpty(cells(1,2).Offset(1, 0)) = False Then
cells(1,2).Interior.Color = RGB(255, 255, 0)
ElseIf IsEmpty(cells(1,2).Offset(1, 0)) = True Then
cells(1,2).Interior.Color = RGB(255, 255, 255)
End If
'highlight found cells yellow
rng.Interior.Color = RGB(255, 255, 0)
'message
MsgBox rng.cells.Count & "cell(s) were found containing: " & fnd & "
found at " & rng.Address
Exit Sub
'error
nothingfound:
MsgBox "no cells containing: " & fnd & " were found in this worksheet"
End Sub
This is what the table looks like in excel:
plug | CH | CHA | CHB | CHA | CHB
-------------------------------------------------------------------------
1 | emptycell | 9 | emptycell | 5 | 4
-------------------------------------------------------------------------
2 | emptycell | 8 | emptycell | 4 | 5
-------------------------------------------------------------------------
3 | emptycell | 7 | emptycell | 3 | 6
-------------------------------------------------------------------------
4 | emptycell | 6 | emptycell | 2 | 7
-------------------------------------------------------------------------
What I would like the code to do is the following:
when searching for a term using the inputbox, only highlight the cells that have data underneath them. For example, if I wanted to see how many channels there were, I would type "ch" into the inputbox and expect to see only certain cells in the first row highlighted because there is data underneath those specific cells: CHA cell in cell(3,1) should be highlighted because there is data underneath it and so should CHA in cell (5,1), and CHB in cell (6,1).
Unfortunately, the code is highlighting all the words in the first row that contain "CH", which includes cells (2,1) and (4,1), even though there is no data in those columns.
I have tried different loops and have not been successful. I think that my main issue is coming from the second if loop near the end of the code.
I have gotten help from reading various online forums and website tutorials since I am new to VBA and learning the language, however, I am stuck on this issue.
Any help and suggestions that I receive on this would be greatly appreciated.