Need to Alter a Couple Parts of Macro

asalazar

New Member
Joined
Feb 15, 2018
Messages
12
Hello,


The current Macro code works and it basically copies data from closed workbooks stored in folder ("C:\Users\jsmith\Desktop\reports")
into Sheet1 of the Active Workbook:


Code:
Sub CopyDataClosedWorkbooks()
    'Turn Screen Refresh Off
        Application.ScreenUpdating = False
    
    'Variables Defined
        Dim wkbDest As Workbook
        Dim wkbSource As Workbook
        Dim LastRow As Long
        Dim LastRuw As Long
        Dim i As Long


    'Set Reference to Active Workbook
        Set wkbDest = ThisWorkbook
    
    'Delete Rows from Sheet1 & Sheet2 of Active Workbook
        For i = 1 To 2
           With Sheets(i)
              .Rows("2:" & .Rows.Count).Delete
           End With
        Next i


    'Copy and Paste Data from Closed Workbooks
        Const strPath As String = "C:\Users\jsmith\Desktop\reports\"
        ChDir strPath
        strExtension = Dir("*.xls*")
        Do While strExtension <> ""
           Set wkbSource = Workbooks.Open(strPath & strExtension)
            With wkbSource
                LastRow = .Sheets(1).Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                .Sheets(1).Range("A2:F" & LastRow).Copy wkbDest.Sheets(1).Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
                .Close savechanges:=False
            End With
            strExtension = Dir
        Loop
    
    'Turn Screen Refresh On
        Application.ScreenUpdating = True
End Sub

I now want to alter parts of the code. The first thing I want to do is instead of
hardcoding the folder path, "C:\Users\jsmith\Desktop\reports", the user can choose
whatever folder he stores the closed workbooks. I think I have accomplished this with
the function I built below, ChooseFolder.


The problem I am having now is that the rest of the code won't execute after this line item: Call ChooseFolder.
The Sub creates an error and stops executing. I can't seem to get this work.


Code:
Sub CopyDataClosedWorkbooks()
    'Turn Screen Refresh Off
        Application.ScreenUpdating = False
    
    'Variables Defined
        Dim wkbDest As Workbook
        Dim wkbSource As Workbook
        Dim LastRow As Long
        Dim i As Long


    'Set Reference to Active Workbook
        Set wkbDest = ThisWorkbook
    
    'Delete Rows from Sheet1 & Sheet2 of Active Workbook
        For i = 1 To 2
           With Sheets(i)
              .Rows("2:" & .Rows.Count).Delete
           End With
        Next i


    'Execute 'ChooseFolder' Function
        Call ChooseFolder
        
    'Copy and Paste Data from Closed Workbooks
        'Const strPath As String = "C:\Users\jsmith\Desktop\reports\"
        'ChDir strPath
        
	strExtension = Dir("*.xls*")
        Do While strExtension <> ""
           Set wkbSource = Workbooks.Open(strPath & strExtension)
            With wkbSource
                LastRow = .Sheets(1).Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                .Sheets(1).Range("A2:F" & LastRow).Copy wkbDest.Sheets(1).Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
                .Close savechanges:=False
            End With
            strExtension = Dir
        Loop
    
    'Turn Screen Refresh On
        Application.ScreenUpdating = True
End Sub


Function ChooseFolder() As String
    Dim fldr As FileDialog
    Dim sItem As String
    Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
    With fldr
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        .InitialFileName = Application.DefaultFilePath
        If .Show <> -1 Then GoTo NextCode
        sItem = .SelectedItems(1)
    End With
NextCode:
    ChooseFolder = sItem
    Set fldr = Nothing
End Function



Another part of the code I want to alter is the section below to also include worksheet Sheet2 from the closed workbooks that the same rows are deleted
as well:
Code:
LastRow = .Sheets(1).Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                .Sheets(1).Range("A2:F" & LastRow).Copy wkbDest.Sheets(1).Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
                .Close savechanges:=False

Thanks,

- rs
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try this. Not sure what you mean with your other issue.

Code:
Sub CopyDataClosedWorkbooks()
    'Turn Screen Refresh Off
        Application.ScreenUpdating = False
    
    'Variables Defined
        Dim wkbDest As Workbook
        Dim wkbSource As Workbook
        Dim LastRow As Long
        Dim i As Long

    'Set Reference to Active Workbook
        Set wkbDest = ThisWorkbook
    
    'Delete Rows from Sheet1 & Sheet2 of Active Workbook
        For i = 1 To 2
           With Sheets(i)
              .Rows("2:" & .Rows.Count).Delete
           End With
        Next i
     
    'Copy and Paste Data from Closed Workbooks
        Dim strPath As String
        strPath = ChooseFolder
                
        strExtension = Dir(strPath & "\*.xls*")
        Do While strExtension <> ""
           Set wkbSource = Workbooks.Open(strPath & strExtension)
            With wkbSource
                LastRow = .Sheets(1).Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                .Sheets(1).Range("A2:F" & LastRow).Copy wkbDest.Sheets(1).Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
                .Close savechanges:=False
            End With
            strExtension = Dir
        Loop
    
    'Turn Screen Refresh On
        Application.ScreenUpdating = True
End Sub


Function ChooseFolder() As String
    Dim fldr As FileDialog
    Dim sItem As String
    Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
    With fldr
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        .InitialFileName = Application.DefaultFilePath
        If .Show <> -1 Then GoTo NextCode
        sItem = .SelectedItems(1)
    End With
NextCode:
    ChooseFolder = sItem
    Set fldr = Nothing
End Function
 
Upvote 0
I tried your code but no data is being copied to the Active Workbook. The code executes but no data is copied over into Sheet1 of the Active Workbook.

In regards to the second issue, I want to change the code so that it not only copies data from Sheet1 from closed workbooks but it also copies data from Sheet2 from the closed workbooks. Essentially, I want to copy data (Range("A2:F" & LastRow) from Sheet1 from all closed workbooks into the Sheet1 from the Active Workbook and then do the same for Sheet 2 from all closed workbooks into Sheet2 of the Active Workbook. Let me know if this makes more sense.
 
Upvote 0
Try this macro. You won't need the "ChooseFolder" function.
Code:
Sub CopyDataClosedWorkbooks()
    Application.ScreenUpdating = False
    Dim wkbDest As Workbook
    Dim wkbSource As Workbook
    Dim LastRow As Long
    Dim LastRuw As Long
    Dim i As Long
    Dim FolderName As String
    Set wkbDest = ThisWorkbook
    For i = 1 To 2
       With Sheets(i)
          .Rows("2:" & .Rows.Count).Delete
       End With
    Next i
    With Application.FileDialog(msoFileDialogFolderPicker)
       .AllowMultiSelect = False
       .Show
       FolderName = .SelectedItems(1) & "\"
    End With
    ChDir FolderName
    strExtension = Dir("*.xls*")
    Do While strExtension <> ""
       Set wkbSource = Workbooks.Open(FolderName & strExtension)
        With wkbSource
            For i = 1 To 2
                LastRow = .Sheets(i).Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                .Sheets(i).Range("A2:F" & LastRow).Copy wkbDest.Sheets(1).Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
            Next i
            .Close SaveChanges:=False
        End With
        strExtension = Dir
    Loop
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,265
Members
452,627
Latest member
KitkatToby

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top