I'm trying to make an excel ui/macros which allows the user to select multiple excel workbooks(wb1,wb2,wb3...) and transfer certain values from them to another workbook(wb_template). Afterwards, saving each one of them as new workbooks (wb1_new,wb2_new,wb3_new...).
Meaning: The template workbook can be used over and over again, and is being saved as a new workbook every time - should be named after the original workbook(wb1) + "_new"):
> Wb1 + wb_template = wb1_new
> Wb2 + wb_template = wb2_new
> Wb3 + wb_template = wb3_new
To summarize the scenario:
- Selection of multiple workbooks via dialog box
- Displaying the selection in a list box
- Transferring certain values from those workbooks into a workbook template
- Saving the workbook template as a new workbook for each excel workbook from the list box
- Result: a couple of new excel workbooks, named after the original excel workbooks from the list box
How can I achieve something like this? Here is a screenshot of the current UI: https://imgur.com/a/ynnhbm0
I have this code for the data transfer:
Sub Button1_Click()
Dim wb1 As Workbook
Dim wb_template As Workbook
Set wb1 = Application.Workbooks.Open("C:\Users\PlutoX\Desktop\Folder\wb1")
Set wb_template = Application.Workbooks.Open("C:\Users\PlutoX\Desktop\Folder\wb_template")
wb_template.Sheets("Sheet1").Range("A1").Value = wb1.Sheets("Sheet1").Range("A1").Value
wb_template.Sheets("Sheet1").Range("A2").Value = wb1.Sheets("Sheet1").Range("A2").Value
wb_template.Sheets("Sheet1").Range("A3").Value = wb1.Sheets("Sheet1").Range("A3").Value
wb1.Close False
wb_template.Close True
End Sub
Problems:
- Origin files(wb1) are static. Need a variable that refers to the selected files from the list box - adding the filepaths of the selected files to the code
I have this code for the dialog window/selection of files:
Sub openDialog()
Dim fd As Office.FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.AllowMultiSelect = True
' Set the title of the dialog box.
.Title = "Please select the file."
' Clear out the current filters, and add our own.
.Filters.Clear
.Filters.Add "Excel 2003", "*.xls"
.Filters.Add "All Files", "*.*"
' Show the dialog box. If the .Show method returns True, the
' user picked at least one file. If the .Show method returns
' False, the user clicked Cancel.
If .Show = True Then
txtFileName = .SelectedItems(1) 'replace txtFileName with your textbox
End If
End With
End Sub
Problems:
- How do I display the file names in the list box? Can't figure it out...
- How do I make sure that the filepaths are handed over to the variable from the "data transfer" code?
Would be very grateful for some help!