我仍然是VBA的新手但是决定使用VBa来简化我正在为我们的工资单工作的月度报告将是最有效的方式,我希望将范围I3:U2270导出到一个以单元格命名的新工作表中摘要表。 除格式化外,一切正常;
如果我使用xlPasteAll
格式是完美的,但它会将值显示为公式。
如果我使用:
.PasteSpecial Paste:=xlPasteValuesAndNumberFormats
.PasteSpecial Paste:=xlPasteColumnWidths
.PasteSpecial Paste:=xlPasteFormats
数据和单元格宽度是完美的,但由于某些数据在表格中,因此不会带来格式。
任何想法都将不胜感激!
Sub MonthlySummaryExport()
Dim NewSheetName As String
Dim Newsheet As Object
On Error Resume Next
NewSheetName = Worksheets("Monthly Summary").Range("T1")
If NewSheetName = "" Then Exit Sub
Set Newsheet = Sheets(NewSheetName)
If Not Newsheet Is Nothing Then
MsgBox "Sheet cannot be created as there is already a worksheet with the same name" & " " & (NewSheetName)
Exit Sub
End If
Sheets.Add(, Sheets(Sheets.Count)).Name = NewSheetName
'Copy and PasteSpecial a between worksheets
Worksheets("Monthly Summary").Range("I3:U2270").Copy
With Worksheets(NewSheetName).Range("A1")
.PasteSpecial Paste:=xlPasteValuesAndNumberFormats
.PasteSpecial Paste:=xlPasteColumnWidths
.PasteSpecial Paste:=xlPasteFormats
End With
'Disable marching ants around copied range
Application.CutCopyMode = False
End Sub
任何建议都会很棒!感谢
答案 0 :(得分:0)
复制包括公式在内的所有内容,然后将公式更改为副本中的值
'Copy and PasteSpecial a between worksheets
dim dest as range
set dest = Worksheets(NewSheetName).Range("A1")
Worksheets("Monthly Summary").Range("I3:U2270").Copy dest
dest.currentregion.copy
dest.pastespecial xlPasteValues