简体   繁体   中英

VBA script to loop through files in its current folder and edit

I'm writing a vba script to a. copy the contents of one current excel file (with formatting), b. loop through all other excel files in its current folder, c. add the new copied worksheet to excel files

For some reason my code is not working. can anyone please help I'm new in vba scripting

    Sub Macro7()

    Dim wbf As Workbook
    Dim myPath As String
    Dim myFile As String
    Dim currentFile As String
    Dim mtExtension As String

    myPath = Application.ActiveWorkbook.Path
    currentFile = ActiveWorkbook.Name


    MsgBox (myPath)
    Dim objFSO As Object
    Dim objFolder As Object


    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(myPath)
    Count = 0


        For Each objFile In objFolder.Files
            If currentFile <> objFile.Name Then
                Set wbt = Workbooks.Open(Filename:=myPath & "\" & objFile.Name)

                MsgBox (objFile.Name)
                .Sheets("Action Descriptions").Select After:=Workbooks(objFile.Name).Sheets(1)
                ActiveWorkbook.Save
                ActiveWindow.Close

            End If


'
        Next
   ' Loop

End Sub

Here you have copied once and pasting multiple times opening multiple files. Try copying every time inside the loop just before pasting in the other file. In loop { Open new file Add tab Activate this workbook Copy Go to other file Paste Save Close }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM