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.
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