Declaring a workbook with filepath/name in cell as a variable, in order to return to it and copy data multiple times

aldousjg

New Member
Joined
Jan 20, 2021
Messages
16
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I am working on a project where the end user will use a dialog box within the "Macro" workbook to choose a file that contains the data they need. There will then be an automated process that copies data from multiple sheets within this file and pastes into the "Macro" workbook.

Because the file that they select could have a different name and file location each time, I am struggling to code for the repeated copy and paste. Could anyone help?

The code for the dialog box for choosing the file is as follows:

With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
.Show

'Store in cell
Sheets("Frontsheet").Range("C8") = .SelectedItems.Item(1)
End With

I need to then access this file multiple times to copy from various sheets. The sheets will have set names.

Thanks,
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Why don't you store the filename/path in a variable?
VBA Code:
Dim strPath As String

    With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = False
        .Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
        .Show
        'Store in variable
        strPath = .SelectedItems.Item(1)
    End With
 
Upvote 0
Hi Norie,

Would this work when having the code to open/copy/paste etc in a separate module?

The selection of the file needs to be it's own module and assigned to a button, then the open/copy/paste etc will make up part of a larger macro that will be assigned to a different button.
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,691
Members
448,978
Latest member
rrauni

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