First of all, I am a beginner in programming VBA. That said, here is the deal: I have a code that can find and highlight all occurrences of multiple keywords (if you want to see my code, it is here). At the end of this code there is the following If
statement:
If Not rng Is Nothing Then
rng.EntireRow.Interior.ColorIndex = 3
End If
And now I would like to hide the lines that are not highlighted. After a lot of reasearch (References: 1, 2, 3, 4, 5.) on how to hide Rows I had this idea:
If Not rng Is Nothing Then
rng.EntireRow.Interior.ColorIndex=3
Else
rng.EntireRow.Hidden = True
End if
But it doesn't work at all. Frustrated, because I thought that it was really a good idea, I created a new Workbook and tried to create a simple Macro to achieve my objective and afterwards I would add it in the searching and highlight Macro described above. Right now it looks like this:
Sub Test()
Dim i As Integer
Dim ws As Worksheet
Set ws = Tabelle1
For i = 1 To 10
If ws.Rows(i).Interior.ColorIndex = 3 Then
MsgBox "Super"
Else
ws.Rows(i).Hidden = True
End If
Next i
End Sub
This Macro has a problem, when I run it once it finds a Row that is not highlighted it will hide all the other Rows independent if they are highlighted or not. Can some one explain me what is wrong with my code that makes the Macro run this away. If someone could also point out how can I make it run with the Else
statement from my previous idea it would be great!