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

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Got it to work! Last question.. When I run this a window pops up asking if I would like to share this info to the clipboard. Is there a way to automatically select yes or no for this so it does not pop up each time? BTW this is the version of code that finally got working.
VBA Code:
Sub InData()
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("C2:C195").Copy
        ThisWorkbook.Worksheets("SheetName").Activate
        ActiveSheet.Range("D2:D195").PasteSpecial
        wb.Close False
    End With
End Sub
 
Upvote 0
Just add this line
VBA Code:
       ActiveSheet.Range("D2:D195").PasteSpecial
      application.cutcopymode=False 
      wb.Close False
 
Upvote 0
Glad it's sorted & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,905
Messages
6,175,297
Members
452,633
Latest member
DougMo

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