能否定义保留原有数字格式的自定义Excel样式?
解决自定义样式覆盖单元格原有数字格式的问题
问题出在你代码里的.IncludeNumber = True——这个属性会让自定义样式强制应用默认数字格式,直接覆盖单元格原本设置的ID和日期格式。
要保留原有格式,只需要把这个属性改成False,这样样式只会应用你定义的字体和填充样式,不会改动数字格式。修改后的代码如下:
Sub DefineAlreadyCompleted() Dim wb As Workbook Dim theNewStyle As Style Set wb = ThisWorkbook ' Check to see if Style already exists On Error Resume Next Set theNewStyle = wb.Styles("AlreadyCompleted") On Error GoTo 0 ' If Style Already Exists, Do Nothing If Not theNewStyle Is Nothing Then Exit Sub End If ' Add New Style Set theNewStyle = wb.Styles.Add("AlreadyCompleted") ' Define the Formatting Properties With theNewStyle .IncludeNumber = False ' 关键修改:不覆盖原有数字格式 .Font.Strikethrough = True .Interior.Color = wb.Styles("Good").Interior.Color End With End Sub
这样修改后,应用AlreadyCompleted样式时,单元格的ID格式(0-00000-00000)和日期格式(m/dd/yyyy)都会保留,同时会加上删除线和对应填充色,完全不需要额外子程序恢复格式。
内容的提问来源于stack exchange,提问作者Cedon
相关产品推荐
相关产品推荐

