Mar*_*tin 9 excel vba excel-2003 excel-vba
有没有办法将条件格式转换为Excel中的静态格式?
我正在尝试将一系列Excel工作表导出到一个新的工作簿,外观相同但没有公式,链接等.这里的问题是我有条件格式依赖于导出范围之外的计算.
我已经尝试将工作簿保存到.html,奇怪的是IE格式化显示,但在Excel中重新打开时却没有.
虽然修改了以适应一些新的条件格式结构和您的需求,但是从这里获取了以下想法.
它的工作原理如下:给定一个带有一些条件格式的工作簿(制作你的副本),你可以在Sub a()中放入你想要从条件格式转换为直接格式化的单元格范围,然后运行宏.之后,只需手动删除条件格式,然后!
抱歉代码长度...生活有时像这样:(
Option Explicit
Sub a()
Dim iconditionno As Integer
Dim rng, rgeCell As Range
Set rng = Range("A1:A10")
For Each rgeCell In rng
If rgeCell.FormatConditions.Count <> 0 Then
iconditionno = ConditionNo(rgeCell)
If iconditionno <> 0 Then
rgeCell.Interior.ColorIndex = rgeCell.FormatConditions(iconditionno).Interior.ColorIndex
rgeCell.Font.ColorIndex = rgeCell.FormatConditions(iconditionno).Font.ColorIndex
End If
End If
Next rgeCell
End Sub
Private Function ConditionNo(ByVal rgeCell As Range) As Integer
Dim iconditionscount As Integer
Dim objFormatCondition As FormatCondition
For iconditionscount = 1 To rgeCell.FormatConditions.Count
Set objFormatCondition = rgeCell.FormatConditions(iconditionscount)
Select Case objFormatCondition.Type
Case xlCellValue
Select Case objFormatCondition.Operator
Case xlBetween: If Compare(rgeCell.Value, ">=", objFormatCondition.Formula1) = True And _
Compare(rgeCell.Value, "<=", objFormatCondition.Formula2) = True Then _
ConditionNo = iconditionscount
Case xlNotBetween: If Compare(rgeCell.Value, "<=", objFormatCondition.Formula1) = True And _
Compare(rgeCell.Value, ">=", objFormatCondition.Formula2) = True Then _
ConditionNo = iconditionscount
Case xlGreater: If Compare(rgeCell.Value, ">", objFormatCondition.Formula1) = True Then _
ConditionNo = iconditionscount
Case xlEqual: If Compare(rgeCell.Value, "=", objFormatCondition.Formula1) = True Then _
ConditionNo = iconditionscount
Case xlGreaterEqual: If Compare(rgeCell.Value, ">=", objFormatCondition.Formula1) = True Then _
ConditionNo = iconditionscount
Case xlLess: If Compare(rgeCell.Value, "<", objFormatCondition.Formula1) = True Then _
ConditionNo = iconditionscount
Case xlLessEqual: If Compare(rgeCell.Value, "<=", objFormatCondition.Formula1) = True Then _
ConditionNo = iconditionscount
Case xlNotEqual: If Compare(rgeCell.Value, "<>", objFormatCondition.Formula1) = True Then _
ConditionNo = iconditionscount
If ConditionNo > 0 Then Exit Function
End Select
Case xlExpression
If Application.Evaluate(objFormatCondition.Formula1) Then
ConditionNo = iconditionscount
Exit Function
End If
End Select
Next iconditionscount
End Function
Private Function Compare(ByVal vValue1 As Variant, _
ByVal sOperator As String, _
ByVal vValue2 As Variant) As Boolean
If Left(CStr(vValue1), 1) = "=" Then vValue1 = Application.Evaluate(vValue1)
If Left(CStr(vValue2), 1) = "=" Then vValue2 = Application.Evaluate(vValue2)
If IsNumeric(vValue1) = True Then vValue1 = CDbl(vValue1)
If IsNumeric(vValue2) = True Then vValue2 = CDbl(vValue2)
Select Case sOperator
Case "=": Compare = (vValue1 = vValue2)
Case "<": Compare = (vValue1 < vValue2)
Case "<=": Compare = (vValue1 <= vValue2)
Case ">": Compare = (vValue1 > vValue2)
Case ">=": Compare = (vValue1 >= vValue2)
Case "<>": Compare = (vValue1 <> vValue2)
End Select
End Function
Run Code Online (Sandbox Code Playgroud)
这种方法似乎效果很好。我只将其实现为背景颜色。
Sub FixColor()
Dim r
For Each r In Selection
r.Interior.Color = r.DisplayFormat.Interior.Color
Next r
Selection.FormatConditions.Delete
End Sub
Run Code Online (Sandbox Code Playgroud)
我建议您采用一种更简单且始终有效的方法。我也很努力地尝试过VBA,但是太难了,所以我中途就放弃了。
要将条件格式转换为静态,我们首先将 Excel 转换为 Html(网页),然后再转换回 Excel。请遵循以下方法。
1. Load the workbook that contains your conditional formatting.
2. Save the workbook as an HTML file(as webpage).
(Press F12, specify the HTML format, and give the workbook a different name.)
3. Restart Excel.
4. Load into Excel the HTML file you saved in step 2.
5. Save the workbook as an Excel workbook.
(Press F12, specify an Excel workbook format, and give the workbook a different name.)
Run Code Online (Sandbox Code Playgroud)
在以 HTML 格式保存 Excel 工作簿的过程中,程序“剥离”所有条件格式并使其显式(绝对)。但是,您应该意识到,此过程也会对您的公式执行相同的操作,而是将所有内容保存为值。