代码之家  ›  专栏  ›  技术社区  ›  Mikz

在特定文件夹中保存xlsx文件

  •  0
  • Mikz  · 技术社区  · 8 年前

    我正在尝试将源工作簿中的内容复制到新工作簿,并将其以xlsx格式保存在指定文件夹中。

    我正在尝试下面的代码,代码的最后一行出现了应用程序定义的错误,我正在尝试将新工作簿另存为。xlsx

    此外,这段小代码大约需要5分钟的时间。

    Sub newWB()
    Dim myWksht As String
    Dim newWB As Workbook
    Dim MyBook As Workbook
    Dim i As Integer, j As Integer
    Dim LastRow As Long, totalrows As Long
    Dim path1, path2  As String
    
    path1 = ThisWorkbook.Path
    path2 = path1 & "\Tru\Sq\"
    Set newWB = Workbooks.Add
    
    
    With ThisWorkbook.Worksheets("Pivottabelle")
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With
    
    
    With newWB.Sheets("Sheet1")
        .Name = "PivotTable"
        j = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
    End With
    
    With Worksheets("Pivottabelle")
        For i = 1 To LastRow
          ThisWorkbook.Sheets("Pivottabelle").Range("A1:Y400").Copy: newWB.Sheets("PivotTable").PasteSpecial
        Next i
    End With
    
    With newWB.Worksheets("PivotTable")
        totalrows = .Cells(.Rows.Count, "A").End(xlUp).Row
        For i = totalrows To 2 Step -1
            If .Cells(i, 8).Value <> "TRU" Then
            Cells(i, 8).EntireRow.Delete
            End If
    Next
    newWB.SaveAs Filename:=path2 & ".xlsx"
    End With
    End Sub
    
    1 回复  |  直到 8 年前
        1
  •  2
  •   Pᴇʜ    8 年前

    这应该显示从注释中得到的所有改进(以及更多的改进)

    保存时可能会遇到问题,因为

    DestinationPath = ThisWorkbook.Path & "\Tru\Sq\"
    

    仅当包含工作簿的宏已保存时有效。否则 ThisWorkbook.Path 为空。您可能需要确保这些子文件夹已经存在。

    Option Explicit 'force variable declare
    
    Public Sub AddNewWorkbook() 'sub and newWB had the same name (no good practice)
        'Dim myWksht As String 'not used therefore can be removed
        Dim newWB As Workbook
        'Dim MyBook As Workbook 'not used therefore can be removed
        'Dim i As Integer, j As Integer
        Dim i As Long, j As Long 'use long instead of integer whenever possible
                                 'see https://stackoverflow.com/a/26409520/3219613
        Dim LastRow As Long, totalrows As Long
        'Dim path1, path2 As String 'always specify a type for every variable
        Dim DestinationPath As String 'we only need one path
    
        DestinationPath = ThisWorkbook.Path & "\Tru\Sq\"
        'path2 = path1 & "\Tru\Sq\" ' can be reduced to one path
    
        Set newWB = Workbooks.Add
    
        With ThisWorkbook.Worksheets("Pivottabelle")
            LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        End With
    
        With newWB.Sheets("Sheet1")
            .Name = "PivotTable"
            j = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
        End With
    
        'With Worksheets("Pivottabelle") 'unecessary with (not used at all)
            'For i = 1 To LastRow 'unecessary loop
        ThisWorkbook.Sheets("Pivottabelle").Range("A1:Y400").Copy
        newWB.Sheets("PivotTable").PasteSpecial
            'Next i
        'End With
    
        With newWB.Worksheets("PivotTable")
            totalrows = .Cells(.Rows.Count, "A").End(xlUp).Row
            For i = totalrows To 2 Step -1
                If .Cells(i, 8).Value <> "TRU" Then
                    .Cells(i, 8).EntireRow.Delete 'was missing a . before Cells(i, 8).EntireRow.Delete
                End If
            Next
    
            newWB.SaveAs Filename:=DestinationPath & "FILENAME" & ".xlsx" 'was missing a filename
        End With
    End Sub
    
    推荐文章