Houston Seminar in Excel VBA Macros
Excel Filter Rows with Cells Having
Specific Strings
Sergio Perez Rodriguez
EG, PDG, MSc
August 2020
Sub Expandiso()
Dim he, cam, rngFound, cnd, cnd1, car As String
cnd = "EXPANDIDO"
cnd1 = "Expandido"
rngFound = "BACHAQ"
Dim v, ch, ct As Integer
Dim pc, pco As Long
pc = 0
ct = 0
Set wks = Worksheets("Hoja1"): wks.Activate
cam = "Campo"
Set Match5 = wks.Cells.Find(cam)
Match5.Offset(0, 0).Select
'The onset of the autofilter looking for rows with cells containing the word "EXPANDIDO"
Cells(ActiveCell.Row, ActiveCell.Column).AutoFilter Field:=2, Criteria1:="EXPANDIDO"
With Worksheets("Hoja1").Cells
Set Match1 = wks.Cells.Find(rngFound)
Match1.Offset(0, 0).Select
Range(Selection, Selection.End(xlDown)).Select
Set he = Selection
ch = he.Rows.Count
MsgBox "Total # of fields " & ch
End With
For v = 1 To ch
dnc = Match1.Offset(v, 1).Value
'MsgBox dnc
Select Case dnc
Case cnd, cnd1
'MsgBox Match1.Offset(v, 6).Value
pc = pc + Match1.Offset(v, 6).Value
ct = ct + 1
car = Match1.Offset(v, 0).Value
Select Case car
Case Is = "CARDON"
Range("K2") = Round(Match1.Offset(v, 6).Value, 0)
Case Else
'Do nothing
End Select
Case Else
'do nothing
End Select
Next v
End Sub
Bibliography
Jelen, B., Syrstad, T. 2016. Excel 2016 VBA and Macros. Pearson Education.
Roman, S. 2002. Writing Excel Macros with VBA. O’Reilly
Alexander, M. & Walkenbach, J. 2018. Excel VBA Programming For Dummies. Wiley