将工作表导出为工作簿(仅值)

时间:2019-02-14 11:56:38

标签: excel vba

我正在尝试将几张工作表从现有工作簿导出到新工作簿,但是尽管我已经包含了以下说明:

.ActiveSheet.UsedRange.Value = ActiveSheet.UsedRange.Value

输出仍包含公式。

这是整个代码:

Sub Sheet_SaveAs()

Dim wb As Workbook
Dim Path As String

'Defining Strings

Path = "%MYPATH%"

'XLS Generator

Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Copy
Set wb = ActiveWorkbook
With wb
 .ActiveSheet.UsedRange.Value = ActiveSheet.UsedRange.Value
 .SaveAs Path & "Example" & ".xlsx"
 .Close False
End With
End Sub

我想念什么?

1 个答案:

答案 0 :(得分:1)

您需要按工作簿的名称而不是活动的名称进行引用:

wb.Worksheets("Sheet1").UsedRange.Value = wb.Worksheets("Sheet1").UsedRange.Value 

要遍历您复制的所有图纸:

Dim varSheetNames As Variant: varSheetNames = Array("Sheet1", "Sheet2", "Sheet3")
' ...
Sheets(varSheetNames).Copy ' Note: ActiveWorkbook.Worksheets or ThisWorkbook.Worksheets (depending on where your code is) would be better
' ...
Dim varName As Variant: For Each varName In varSheetNames
    With wb.Worksheets(varName).UsedRange
        .Value = .Value
    End With
Next varName