我当前正在尝试创建一个宏,该宏在单元格包含“ apple”或“ orange”一词时隐藏行。我似乎无法解决的问题是,其中可能出现“苹果”和“橙色”的单元格周围有其他单词,例如“香蕉”,“草莓”,“菠萝”等。只有“ apple”或“ orange”出现在一个单元格中,当其中一个单词与其他单词组合时,它不起作用。有谁可以帮助我吗?我目前正在使用Office 365 for Mac / Excel版本16.16.2,请参阅下面正在使用的宏:
`BeginRow = 4
EndRow = 2844
ChkCol = 5
For RowCnt = BeginRow To EndRow
If Cells(RowCnt, ChkCol).Value = "Apple" Or Cells(RowCnt, ChkCol).Value = "Orange"
Then Cells(RowCnt, ChkCol).EntireRow.Hidden = True
Else
Cells(RowCnt, ChkCol).EntireRow.Hidden = False
End If
Next RowCnt`
答案 0 :(得分:1)
您应使用Like:
而不是=BeginRow = 4 EndRow = 2844 ChkCol = 5
For RowCnt = BeginRow To EndRow
If UCase(Cells(RowCnt, ChkCol).Value) Like "*APPLE*" Or UCase(Cells(RowCnt, ChkCol).Value) Like "*ORANGE*" Then
Cells(RowCnt, ChkCol).EntireRow.Hidden = True
Else Cells(RowCnt, ChkCol).EntireRow.Hidden = False
End If
Next RowCnt
这将隐藏包含“ apple”和/或“ orange”(不区分大小写)的行。
答案 1 :(得分:0)
我会走这条路,因为它可以轻松地缩放要隐藏的单词数-通过添加到words数组。尽管我仍然按照@ emma-clarke的建议使用LIKE
。
Dim beginrow As Integer, endrow As Integer, chkcol As Integer, rowcnt As Integer, i As Integer
Dim rng As Range, r As Range
Dim words() As String
words = Split("Apple,Taco", ",")
beginrow = 1
endrow = 22
chkcol = 1
Set rng = Range(Cells(beginrow, chkcol), Cells(endrow, chkcol))
For Each r In rng
For i = 0 To UBound(words)
If UCase(r.Value) Like UCase("*" & words(i) & "*") Then
r.EntireRow.Hidden = True
Else
r.EntireRow.Hidden = False
End If
If r.EntireRow.Hidden Then Exit For
Next i
Next r
Set r = Nothing
Set rng = nothing