【问题标题】:Format merged and unmerged cells in excel vba在excel vba中格式化合并和未合并的单元格
【发布时间】:2021-11-28 08:02:58
【问题描述】:

我的 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----

【问题讨论】:

  • 您可能应该将这些单元格设为命名范围,以便您可以直接引用这些范围,而不是遍历每个单元格并检查其颜色属性。这些“输入单元格”是否已修复?

标签: excel vba formatting


【解决方案1】:

正如上面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

【讨论】:

  • 感谢格式化并使其更具可读性
猜你喜欢
  • 2021-05-10
  • 2013-11-23
  • 1970-01-01
  • 2016-06-09
  • 2021-06-14
  • 1970-01-01
  • 2010-10-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多