Use VBA to open file dialog and copy paste into current worksheet

cgrulk

New Member
Joined
Jun 16, 2020
Messages
10
Office Version
  1. 2016
Platform
  1. Windows
Hello all,

I am attempting to write a VBA to input data into a sheet I am working on. I am trying to input data into a spreadsheet that is currently set up so I need to paste data into the range D2:D195 in the current sheet. I would like to create a button that would allow me to pick a file that would open up a file finder, I could select a file to import data from, and it would copy the correct range (coincidentally also D2:D195) and paste it into the current worksheet. Note: all the files I would be copying data from should be .csv files and will all have the same layout. I am trying to make this in a workbook that contains another sheet in which I would like to create a similar function, so if this macro could be easily applied on multiple sheets that would be ideal. I have tried recording a macro and working in other code to try to make it work, but I am a VBA novice and would really appreciate some help! Here is what I have so far.

VBA Code:
Dim strFile As String
'Open the File Dialog

With Application.FileDialog(msoFileDialogFilePicker)
    .AllowMultiSelect = False
    'Show the dialog box
    .Show
    'Store in fullpath variable
    fullpath = .SelectedItems.Item(1)
    
    
    'copy range from selected item into current worksheet
    Workbook (.SelectedItems.Item(1)), Range("D2:D195").Copy
    Windows("ThisSheet").Activate
    ActiveSheet.Range("D2:D195").Paste
End With

End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Try this

VBA Code:
Dim strFile As String, wb As Workbook
'Open the File Dialog
    With Application.FileDialog(msoFileDialogFilePicker)
         .AllowMultiSelect = False
         'Show the dialog box
         .Show
         'Store in fullpath variable
         fullpath = .SelectedItems.Item(1)
         'open the file
         Set wb = Workbooks(fullpath).Open
        'copy range from selected item into current worksheet
        wb.Sheets(1).Range("D2:D195").Copy
        Windows("ThisSheet").Activate
        ActiveSheet.Range("D2:D195").Paste
        wb.Close False
    End With
End Sub
 
Upvote 0
I tried this and a subscript out of range error pops up and highlights this line
VBA Code:
Set wb = Workbooks(fullpath).Open
 
Upvote 0
How about
VBA Code:
Dim strFile As String, wb As Workbook
'Open the File Dialog
    With Application.FileDialog(3)
         .AllowMultiSelect = False
         'Show the dialog box
         If .Show Then
            'Store in fullpath variable
            fullpath = .SelectedItems.Item(1)
            
            'open the file
            Set wb = Workbooks.Open(fullpath)
         End If
         If wb Is Nothing Then Exit Sub
        'copy range from selected item into current worksheet
        wb.Sheets(1).Range("D2:D195").Copy
        Windows("ThisSheet").Activate
        ActiveSheet.Range("D2:D195").Paste
        wb.Close False
    End With
 
Upvote 0
This resulted in the same error, but for a different line this time..
VBA Code:
Windows("ThisSheet").Activate
I tried changing ("ThisSheet") to the name of the actual sheet, but this still yielded the same error. Thank you for your help!
 
Upvote 0
Change it to
VBA Code:
ThisWorkbook.Worksheet("Sheetname").Activate
 
Upvote 0
Oops it should be worksheets plural
 
Upvote 0
VBA Code:
Set wb = Workbooks(fullpath).Open

I don't believe I did that! Talk about senility,, wow! :confused:
 
Upvote 0
I get a 438 run time error that the debugger highlights this line of code.
VBA Code:
ActiveSheet.Range("D2:D195").Paste
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,155
Members
453,021
Latest member
Justyna P

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