[英]Format merged and unmerged cells in excel vba
我的 excel 表中有一個范圍來自 A1:Q50。
我用黃色填充突出了一些單元格(以便用戶可以將其識別為輸入單元格)。 其中一些單元格被合並。
我正在嘗試設置一個宏,該宏在觸發時應將所有帶有黃色填充(合並或未合並)的單元格清除為 A1:Q50 范圍內的無填充
此代碼不起作用
'---START REMOVE YELLOW COLOR----
'Step 1 : Loop through all selected sheets
Dim ws As Worksheet
For Each ws In ActiveWindow.SelectedSheets
'Select all cells in the selected worksheet
Range("A1:Q50").Select
'Finds all cells filled with yellow color and removes it
If cell.Interior.Color = vbYellow Then
cell.Interior.Color = xlNone
End If
Next ws
'---END REMOVE YELLOW COLOR----
正如Raymond Wu上面所建議的,最好有一個命名范圍。 它提供了額外的靈活性
'---START REMOVE YELLOW COLOR----
'Step 1 : Loop through all selected sheets
Dim ws As Worksheet
For Each ws In ActiveWindow.SelectedSheets
'Select all cells in the selected worksheet
Set selectedRange = Range("A1:Q50")
' if using a range named MyRange then it'll become
' Set selectedRange = Range("MyRange")
' Loop over all cells in range
For Each cell In selectedRange.Cells
'Finds all cells filled with yellow color and removes it
If cell.Interior.Color = vbYellow Then
cell.Interior.Color = xlNone
End If
Next
Next ws
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.