宏将多个Excel合并为一个,破坏了保存

时间:2017-10-31 10:36:13

标签: excel-vba vba excel

我有一个宏,它将所有excel文件合并为一个并重命名,但是我遇到了一旦保存后重新打开主文件的问题。

   Sub Combine_em_all()
   Dim wb As Workbook, sh As Worksheet, fPath As String, fName As String
   Set sh = ThisWorkbook.Sheets(1)

   fPath = ThisWorkbook.Path 
   If Right(fPath, 1) <> "\" Then fPath = fPath & "\" 
    fName = Dir(fPath & "*.xl*") 
    If fName <> ThisWorkbook.Name Then
        Set wb = Workbooks.Open(fPath & fName)
        With wb.Sheets(1)
            If ActiveSheet.Name = "CR-GB 1" Then
                Sheets(1).Copy 
              After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
                ThisWorkbook.Activate

                ActiveSheet.Name = Range("D4").Value

            End If
        End With
        wb.Close
    End If
    fName = Dir
    Loop While fName <> ""
End Sub

文件夹中有48个文件,我试图了解代码出了什么问题。

感谢,

0 个答案:

没有答案