Extract workbook select in Col A after last item

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,570
Office Version
  1. 2021
Platform
  1. Windows
I have written the following code to select files to be copied


once I select a file it appears in the message box


I would also like the file name selected to be copied into Col A from row 2 onwards on sheet "Macro"

or e.g. if BR1 sales May 2019.csv is selected first, BR2 sales May 2019.csv is then selected, BR1 sales May 2019.csv must be copied to A2, BR2 sales May 2019.csv must be copied to A3 etc


Code:
 Sub Open_Workbook()


A:
Dim A     As Variant
    
    ChDir "C:\download"
    
    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
        .Sheets(1).UsedRange.Copy Destination:=ThisWorkbook.Sheets("Imported Data").Range("A" & ThisWorkbook.Sheets(1).Rows.Count).End(xlUp).Offset(1)
        .Close 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



I would be appreciated if someone could kindly amend my code accordingly
 
Last edited:

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try:
Code:
Sub Open_Workbook()
A:
    Dim A As Variant, desWS1 As Worksheet, desWS2 As Worksheet
    Set desWS1 = ThisWorkbook.Sheets("Imported Data")
    Set desWS2 = Sheets("Macro")
    ChDir "C:\download"
    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 & Trim(Right(WorksheetFunction.Substitute(A, "\", WorksheetFunction.Rept(" ", Len(A))), Len(A)))
        .Sheets(1).UsedRange.Copy desWS1.Range("A" & desWS1.Rows.Count).End(xlUp).Offset(1, 0)
        desWS2.Cells(desWS2.Rows.Count, "A").End(xlUp).Offset(1, 0) = Trim(Right(WorksheetFunction.Substitute(A, "\", WorksheetFunction.Rept(" ", Len(A))), Len(A)))
        .Close 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
 
Last edited:
Upvote 0
Thanks for the help, code works perfectly
 
Upvote 0
You are very welcome. :)
 
Upvote 0

Forum statistics

Threads
1,216,309
Messages
6,130,002
Members
449,552
Latest member
8073662045

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