Macro to open serveral files

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have a macro that allows me to select files in a directory. I would like to amend the macro that if I select another file after the first file, the macro will automatically take the user to the last file selected. The reason for this is that I have many files in this directory and the next file to be selected will be very close to where the first file is



Code:
 Sub Open_Workbook()
ChDir ("C:\downloads")
A:
Dim A     As Variant
Dim LR As Long
  



 With Sheets("Data Import")
 LR = Cells(Rows.Count, "A").End(xlUp).Row
      Range("A1:AJ" & LR).ClearContents
     End With

    If msg <> "" Then MsgBox msg

     A = Application.GetOpenFilename
    If A = False Or IsEmpty(A) Then Exit Sub
    
    Application.ScreenUpdating = False
     
    
  
    
   With Workbooks.Open(A)
     msg = msg & vbCr & A
     
        With Sheets(1)
            .Range("a1", .Range("J" & Rows.Count).End(xlUp)).Copy _
                Destination:=ThisWorkbook.Sheets("Data Import").Range("A" & Rows.Count).End(xlUp).Offset(1)
        End With
      
        .Close SaveChanges:=False
    End With

   answer = MsgBox("Does another file needs to be selected?", vbYesNo + vbQuestion, "Hello")
If answer = vbYes Then
GoTo A:
End If
 Application.ScreenUpdating = True
    
End Sub


Your assistance in this regard is most appreciated
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Not sure if I correctly understood you, but this is the code which shows the last file selected. Note, however, that this is not a last file selected by user, but last file in list (which is ordered).
Code:
Sub SelectAnother()
    Dim last_file
    With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = True
        If Not .Show() Then Exit Sub
        last_file = .SelectedItems(.SelectedItems.Count)
    End With
    MsgBox last_file
End Sub
 
Upvote 0
Thanks for your code. What I am looking for is that Once i have selected the f1st file in the folder and once I select yes to select another the code will take me to the file in the folder i last selected


It would be appreciated if you could amend my code to accomodate my request
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,557
Members
449,088
Latest member
davidcom

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