VBA 如果单元格具有用户定义的样式,则更改单元格的值
VBA change cell's value if it has a user defined style
我想将 Excel 电子表格中特定用户定义样式(比如 'Beauty' 样式)的所有单元格更改为值 "Beast"。
Sub BulkChangeValeOfStyle()
Dim TheCell As Range
For Each TheCell In ActiveSheet.UsedRange.Cells
If TheCell.Style = "Beauty" Then
TheCell.Value = "Beast"
End If
Next
End Sub
这段代码太慢了。我在电子表格周围散布了很多美容细胞。能不能做成这样:
ActiveSheet.AllCellsWithaStyle="Beauty".value="Beast"
更新
这只是一个想法:
ActiveSheet.Cells.SpecialCells(xlCellTypeSameFormatConditions).Activate
或者
ActiveSheet.Cells.SpecialCells(xlCellTypeSameValidation).Activate
但我不知道如何设置确定 xlCellTypeSameFormatConditions
的条件。或 xlCellTypeSameValidation
的标准。有人知道吗?
我认为不可能有比 For Each
循环更好的解决方案。但是您应该为要修改的单元格创建一个新样式,并在需要时修改该样式的格式属性,如下所示:
ThisWorkbook.Styles.Item("Good").Interior.ColorIndex=4
我想将 Excel 电子表格中特定用户定义样式(比如 'Beauty' 样式)的所有单元格更改为值 "Beast"。
Sub BulkChangeValeOfStyle()
Dim TheCell As Range
For Each TheCell In ActiveSheet.UsedRange.Cells
If TheCell.Style = "Beauty" Then
TheCell.Value = "Beast"
End If
Next
End Sub
这段代码太慢了。我在电子表格周围散布了很多美容细胞。能不能做成这样:
ActiveSheet.AllCellsWithaStyle="Beauty".value="Beast"
更新
这只是一个想法:
ActiveSheet.Cells.SpecialCells(xlCellTypeSameFormatConditions).Activate
或者
ActiveSheet.Cells.SpecialCells(xlCellTypeSameValidation).Activate
但我不知道如何设置确定 xlCellTypeSameFormatConditions
的条件。或 xlCellTypeSameValidation
的标准。有人知道吗?
我认为不可能有比 For Each
循环更好的解决方案。但是您应该为要修改的单元格创建一个新样式,并在需要时修改该样式的格式属性,如下所示:
ThisWorkbook.Styles.Item("Good").Interior.ColorIndex=4