将启用宏的工作簿另存为.xlxs

时间:2018-09-07 09:59:16

标签: excel vba excel-vba

我建立了一个带有宏的工作表,该宏检查各种单元格的格式,最后将其另存为用户C:驱动器中的普通.xlxs电子表格。

我一直在尝试的代码是

Dim sheetName As String
Dim tempwb As Workbook
Dim currentworkbook As String
Dim currentformat As Long

    currentworkbook = ThisWorkbook.FullName
    currentformat = ThisWorkbook.FileFormat
    sheetname = "Risk Info"

    ThisWorkbook.Sheets(sheetName).Copy before:=tempwb.Sheets(1)
    ThisWorkbook.Sheets(sheetName).SaveAs Filename:="C:\xstreamv1\inbox\xstream_data_sheet.xlsx", FileFormat :=51
    tempwb.Close savechanges:=False

    ThisWorkbook.SaveAs Filename:=currentworkbook, FileFormat:=currentformat
    ActiveWorkbook.Close savechanges:=False

    ActiveWorkbook.Close False

当我运行宏时,它确实会创建我想要的xlxs副本,但是它也会创建并打开我正在处理的工作表的副本,然后崩溃一段时间!

任何建议将不胜感激!

1 个答案:

答案 0 :(得分:0)

很抱歉问题的布局不好。现在,我已经根据Cyboashu和我发现的其他论坛的建议设法解决了这个问题。固定代码为:

Sub DataCheck()
'
' To check that the data entered in XStream is correct
'

Dim sheetName As String
Dim startRow As Integer, startCol As Integer
Dim endRow As Long, endCol As Integer
Dim row As Integer, col As Integer
Dim RC As Range
Dim c As Integer
Dim x As Integer
Dim a As Double
Dim tempwb As Workbook
Dim currentworkbook As String
Dim currentformat As Long

Set tempwb = Workbooks.Add

currentworkbook = ThisWorkbook.FullName
cuurentformat = ThisWorkbook.FileFormat


sheetName = "Risk Info" 'Your sheetname

    startRow = 3 'start row for the loop
    endRow = 20

    c = 0

    ...

    If c = 0 Then

        ThisWorkbook.Sheets(sheetName).Copy before:=tempwb.Sheets(1)
        ThisWorkbook.Sheets(sheetName).SaveAs Filename:="C:\xstreamv1\inbox\xstream_data_sheet.xlsx", FileFormat:=51
        tempwb.Close savechanges:=False

    Else
        MsgBox "There were issues with " & c & " entries. See red cells"
    End If





End Sub