我想在 C4:Z104 中創建一個包含字母并且顏色索引為白色或灰色(2 或 15 或 16)的所有單元格的范圍。有其他顏色的單元格不應包含在該范圍內。請注意,單元格是非數字的。
下面是我正在嘗試除錯的代碼。它拋出一個錯誤。我試過使用 IsEmpty(),但在其他執行緒上讀到這可能不是字串值的最佳選擇。任何幫助將不勝感激。
Sub SelectByColorAndString()
Dim cell As Range, u As Boolean
For Each cell In Range("C4:Z104")
If cell.Interior.ColorIndex = 2 or cell.Interior.ColorIndex = 15 or cell.Interior.ColorIndex = 16 or & Not (IsNumeric(cell.Value) then
If u = False Then cell.Select: u = True
Range(Selection.Address & "," & cell.Address).Select
End If
Next
End Sub
此外,如果包括這 100 行中任何一行中的任何單元格,則范圍應包括該行第 1 列中的字串值。
謝謝你,山頂
我嘗試了以下代碼,但收到錯誤訊息。我希望選擇所有帶有字母的白色或灰色單元格。
Sub SelectByColorAndString()
Dim cell As Range, u As Boolean
For Each cell In Range("C4:Z104")
If cell.Interior.ColorIndex = 2 or cell.Interior.ColorIndex = 15 or cell.Interior.ColorIndex = 16 or & Not (IsNumeric(cell.Value) then
If u = False Then cell.Select: u = True
Range(Selection.Address & "," & cell.Address).Select
End If
Next
End Sub
uj5u.com熱心網友回復:
VBAAnd
不&
使用布爾邏輯,您需要在填充顏色測驗周圍加上一些括號。
嘗試這個:
Sub SelectByColorAndString()
Dim cell As Range, u As Boolean, ci As Long, rng As Range
For Each cell In ActiveSheet.Range("C4:Z104").Cells
ci = cell.Interior.ColorIndex
If (ci = 2 Or ci = 15 Or ci = 16) And Not IsNumeric(cell.Value) Then
If rng Is Nothing Then
Set rng = cell
Else
Set rng = Application.Union(rng, cell)
End If
End If
Next cell
If Not rng Is Nothing Then rng.Select
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/536513.html
標籤:擅长VBA模块