使用 Excel VBA 有没有办法将整个工作表的 NumberFormat 复制到新工作表?
With Excel VBA is there a way to copy an entire worksheet's NumberFormat to a new worksheet?
在 Excel 中,有没有办法将 NumberFormat 从具有多列的输入范围复制到具有相同列数的输出范围?
我已经尝试了此代码示例中的几种变体,但是当我尝试从多个列获取输出范围的格式时,输出范围的格式没有任何变化,或者复制值的性能太有问题。
Sub Import()
'Demonstration variables
Dim ws_input As Worksheet: Set ws_input = ThisWorkbook.Sheets(1)
Dim ws_output As Worksheet: Set ws_output = ThisWorkbook.Sheets(2)
Dim rowLast As Double, colLast As Double, i As Double
rowLast = ws_input.Cells(Rows.Count, 1).End(xlUp).Row
colLast = ws_input.Cells(1, Columns.Count).End(xlToLeft).Column
'This variant is way too much of a performance hit and Excel can misapply formatting
'E.g. Cell XFD1048576 is considered used
ws_input.Cells.Copy Destination:=ws_output.Range("A1")
'This variant can copy "###" as the value if the input cell widths are too small
ws_output.Range("A1", Cells(rowLast, colLast).Address).Value = _
ws_input.Range("A1", Cells(rowLast, colLast).Address).Value
'This variant copies values correctly but without formatting
ws_output.Range("A1", Cells(rowLast, colLast).Address).Value2 = _
ws_input.Range("A1", Cells(rowLast, colLast).Address).Value2
ws_output.Range("A1", Cells(rowLast, colLast).Address).NumberFormat = _
ws_input.Range("A1", Cells(rowLast, colLast).Address).NumberFormat
'This variant also seemingly does nothing with the NumberFormat
ws_output.Range("A1", Cells(rowLast, colLast).Address).EntireColumn.NumberFormat = _
ws_input.Range("A1", Cells(rowLast, colLast).Address).EntireColumn.NumberFormat
'This variant also seemingly does nothing with the NumberFormat
ws_output.Range("A1").CurrentRegion.NumberFormat = _
ws_input.Range("A1").CurrentRegion.NumberFormat
'Kludgy solution I've come to
For i = 1 to colLast
ws_output.Columns(i).NumberFormat = ws_input.Columns(i).NumberFormat
Next i
End Sub
我希望避免每次需要应用数字格式时都必须使用迭代器,而且似乎应该有一种更直观的方式 Excel 可以应用 NumberFormat 属性 到多个范围的多个范围。如果我只想将一个范围作为格式来源,ws_output.Range("A1:Z9999").NumberFormat = ws_input.Range("A1").NumberFormat
可以,但一旦使用多个列来源,它似乎就会崩溃。
感谢您的宝贵时间!非常感谢有关此主题的任何帮助。
来自 Range.NumberFormat
文档:
This property returns Null if all cells in the specified range don't have the same number format.
如果您的列有不同的数字格式,那么您将不得不像上一种方法一样遍历它们,假设您不想使用剪贴板。
在 Excel 中,有没有办法将 NumberFormat 从具有多列的输入范围复制到具有相同列数的输出范围?
我已经尝试了此代码示例中的几种变体,但是当我尝试从多个列获取输出范围的格式时,输出范围的格式没有任何变化,或者复制值的性能太有问题。
Sub Import()
'Demonstration variables
Dim ws_input As Worksheet: Set ws_input = ThisWorkbook.Sheets(1)
Dim ws_output As Worksheet: Set ws_output = ThisWorkbook.Sheets(2)
Dim rowLast As Double, colLast As Double, i As Double
rowLast = ws_input.Cells(Rows.Count, 1).End(xlUp).Row
colLast = ws_input.Cells(1, Columns.Count).End(xlToLeft).Column
'This variant is way too much of a performance hit and Excel can misapply formatting
'E.g. Cell XFD1048576 is considered used
ws_input.Cells.Copy Destination:=ws_output.Range("A1")
'This variant can copy "###" as the value if the input cell widths are too small
ws_output.Range("A1", Cells(rowLast, colLast).Address).Value = _
ws_input.Range("A1", Cells(rowLast, colLast).Address).Value
'This variant copies values correctly but without formatting
ws_output.Range("A1", Cells(rowLast, colLast).Address).Value2 = _
ws_input.Range("A1", Cells(rowLast, colLast).Address).Value2
ws_output.Range("A1", Cells(rowLast, colLast).Address).NumberFormat = _
ws_input.Range("A1", Cells(rowLast, colLast).Address).NumberFormat
'This variant also seemingly does nothing with the NumberFormat
ws_output.Range("A1", Cells(rowLast, colLast).Address).EntireColumn.NumberFormat = _
ws_input.Range("A1", Cells(rowLast, colLast).Address).EntireColumn.NumberFormat
'This variant also seemingly does nothing with the NumberFormat
ws_output.Range("A1").CurrentRegion.NumberFormat = _
ws_input.Range("A1").CurrentRegion.NumberFormat
'Kludgy solution I've come to
For i = 1 to colLast
ws_output.Columns(i).NumberFormat = ws_input.Columns(i).NumberFormat
Next i
End Sub
我希望避免每次需要应用数字格式时都必须使用迭代器,而且似乎应该有一种更直观的方式 Excel 可以应用 NumberFormat 属性 到多个范围的多个范围。如果我只想将一个范围作为格式来源,ws_output.Range("A1:Z9999").NumberFormat = ws_input.Range("A1").NumberFormat
可以,但一旦使用多个列来源,它似乎就会崩溃。
感谢您的宝贵时间!非常感谢有关此主题的任何帮助。
来自 Range.NumberFormat
文档:
This property returns Null if all cells in the specified range don't have the same number format.
如果您的列有不同的数字格式,那么您将不得不像上一种方法一样遍历它们,假设您不想使用剪贴板。