将值和颜色从一个工作簿复制到另一个工作簿而不复制条件格式
Copy values and colors from one workbook to another without copying conditional formatting
我使用这行代码将一个工作簿的内容复制到另一个工作簿,但它只复制值(并避免出现 #VALUE
错误),但我希望复制单元格的颜色还有:
Workbooks("Payroll Data.xlsm").Worksheets("Pay-MR Compiled").Range("A" & Total_rows_PayMRCompiled + 1 & ":BD" & Total_rows_PayMRCompiled + Total_rows_PayMR - 1).Value = ThisWorkbook.Worksheets("Pay-MR").Range("A2:BD" & Total_rows_PayMR).Value
我的问题是,当我复制 Values & Source Formatting
时,它还会复制条件格式规则,因为我的条件是使用类似 NOT(ISFORMULA(A2))
的函数来检查单元格是否为公式,并且粘贴只是值,所有都会被着色。
例如实际数据中只有1个单元格高亮显示如下:
但是用Values and Source Formatting
粘贴时会发生什么:
您可以使用 .DisplayFormat
方法来实现。检查 here
这样试试
Set trgtCell = Workbooks("Payroll Data.xlsm").Worksheets("Pay-MR").Range("A2")
Set srcCell = ThisWorkbook.Worksheets("Pay-MR").Range("A2")
trgtCell.Value = srcCell.Value
trgtCell.Font.FontStyle = srcCell.DisplayFormat.Font.FontStyle
trgtCell.Interior.Color = srcCell.DisplayFormat.Interior.Color
trgtCell.Font.Strikethrough = srcCell.DisplayFormat.Font.Strikethrough
trgtCell.Font.Color = srcCell.DisplayFormat.Font.Color
我认为没有条件格式就无法复制格式。
但是,您可以反其道而行之:首先复制值和格式,然后删除条件格式。
您的代码可能如下所示
Dim destRange as Range
Set destRange = Workbooks("Payroll Data.xlsm").Worksheets("Pay-MR Compiled").Range("A" & Total_rows_PayMRCompiled + 1 & ":BD" & Total_rows_PayMRCompiled + Total_rows_PayMR - 1)
ThisWorkbook.Worksheets("Pay-MR").Range("A2:BD" & Total_rows_PayMR).Copy
destRange.PasteSpecial Paste:=xlPasteValues
destRange.PasteSpecial Paste:=xlPasteFormats
destRange.FormatConditions.Delete
如果目标位置上的颜色不同,可能是两个工作簿的配色方案不同。查看如何复制颜色设置。
根据你说清楚的条件格式公式,你可以试试这个
Set sourceRange = ThisWorkbook.Worksheets("Pay-MR").Range("A2:BD" & Total_rows_PayMR)
With Workbooks("Payroll Data.xlsm").Worksheets("Pay-MR Compiled").Range(sourceRange.Address).Offset(Total_rows_PayMRCompiled - 1) ' reference "target" range
.Value = sourceRange.Value ' paste "source" range values to referenced (i.e. "target") range
.Interior.Color = sourceRange.FormatConditions(1).Interior.Color ' color referenced (i.e. "target") range with conditional formatting color
.Parent.Range(sourceRange.SpecialCells(xlCellTypeFormulas).Offset(Total_rows_PayMRCompiled - 1).Address).Interior.Pattern = xlNone 'clear the color of "target" sheet range corresponding to "source" range cells with formulas
End With
如果您的 "source" 范围条件格式有多个公式,则只需将 FormatConditions(1).
中的 1
更改为正确的格式条件项编号。
编辑以在目标范围内添加 Total_rows_PayMRCompiled - 1
行偏移量
我使用这行代码将一个工作簿的内容复制到另一个工作簿,但它只复制值(并避免出现 #VALUE
错误),但我希望复制单元格的颜色还有:
Workbooks("Payroll Data.xlsm").Worksheets("Pay-MR Compiled").Range("A" & Total_rows_PayMRCompiled + 1 & ":BD" & Total_rows_PayMRCompiled + Total_rows_PayMR - 1).Value = ThisWorkbook.Worksheets("Pay-MR").Range("A2:BD" & Total_rows_PayMR).Value
我的问题是,当我复制 Values & Source Formatting
时,它还会复制条件格式规则,因为我的条件是使用类似 NOT(ISFORMULA(A2))
的函数来检查单元格是否为公式,并且粘贴只是值,所有都会被着色。
例如实际数据中只有1个单元格高亮显示如下:
但是用Values and Source Formatting
粘贴时会发生什么:
您可以使用 .DisplayFormat
方法来实现。检查 here
这样试试
Set trgtCell = Workbooks("Payroll Data.xlsm").Worksheets("Pay-MR").Range("A2")
Set srcCell = ThisWorkbook.Worksheets("Pay-MR").Range("A2")
trgtCell.Value = srcCell.Value
trgtCell.Font.FontStyle = srcCell.DisplayFormat.Font.FontStyle
trgtCell.Interior.Color = srcCell.DisplayFormat.Interior.Color
trgtCell.Font.Strikethrough = srcCell.DisplayFormat.Font.Strikethrough
trgtCell.Font.Color = srcCell.DisplayFormat.Font.Color
我认为没有条件格式就无法复制格式。
但是,您可以反其道而行之:首先复制值和格式,然后删除条件格式。
您的代码可能如下所示
Dim destRange as Range
Set destRange = Workbooks("Payroll Data.xlsm").Worksheets("Pay-MR Compiled").Range("A" & Total_rows_PayMRCompiled + 1 & ":BD" & Total_rows_PayMRCompiled + Total_rows_PayMR - 1)
ThisWorkbook.Worksheets("Pay-MR").Range("A2:BD" & Total_rows_PayMR).Copy
destRange.PasteSpecial Paste:=xlPasteValues
destRange.PasteSpecial Paste:=xlPasteFormats
destRange.FormatConditions.Delete
如果目标位置上的颜色不同,可能是两个工作簿的配色方案不同。查看如何复制颜色设置。
根据你说清楚的条件格式公式,你可以试试这个
Set sourceRange = ThisWorkbook.Worksheets("Pay-MR").Range("A2:BD" & Total_rows_PayMR)
With Workbooks("Payroll Data.xlsm").Worksheets("Pay-MR Compiled").Range(sourceRange.Address).Offset(Total_rows_PayMRCompiled - 1) ' reference "target" range
.Value = sourceRange.Value ' paste "source" range values to referenced (i.e. "target") range
.Interior.Color = sourceRange.FormatConditions(1).Interior.Color ' color referenced (i.e. "target") range with conditional formatting color
.Parent.Range(sourceRange.SpecialCells(xlCellTypeFormulas).Offset(Total_rows_PayMRCompiled - 1).Address).Interior.Pattern = xlNone 'clear the color of "target" sheet range corresponding to "source" range cells with formulas
End With
如果您的 "source" 范围条件格式有多个公式,则只需将 FormatConditions(1).
中的 1
更改为正确的格式条件项编号。
编辑以在目标范围内添加 Total_rows_PayMRCompiled - 1
行偏移量