Sure! Here's a script that uses VBA with a GUI to fill a primary Excel table with values from multiple other tables:
Sub FillPrimaryTable()
' Prompt user to select primary table file
Dim primaryTableFile As Variant
primaryTableFile = Application.GetOpenFilename(FileFilter:="Excel Files (*.xlsx*), *.xlsx*", Title:="Select Primary Table File")
' Exit if user cancels the file selection
If primaryTableFile = False Then
Exit Sub
End If
' Open primary table workbook
Dim primaryWorkbook As Workbook
Set primaryWorkbook = Workbooks.Open(primaryTableFile)
' Prompt user to select other tables files
Dim otherTablesFiles As FileDialog
Set otherTablesFiles = Application.FileDialog(msoFileDialogOpen)
otherTablesFiles.AllowMultiSelect = True
otherTablesFiles.Title = "Select Other Tables Files"
otherTablesFiles.Filters.Add "Excel Files", "*.xlsx*"
' Exit if user cancels the file selection
If otherTablesFiles.Show = False Then
Exit Sub
End If
' Loop through selected other tables files
Dim otherTableFile As Variant
For Each otherTableFile In otherTablesFiles.SelectedItems
' Open other table workbook
Dim otherWorkbook As Workbook
Set otherWorkbook = Workbooks.Open(otherTableFile)
' Loop through sheets in other table workbook
Dim otherSheet As Worksheet
For Each otherSheet In otherWorkbook.Worksheets
' Get last used row and column in the other sheet
Dim lastRow As Long
Dim lastColumn As Long
lastRow = otherSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
lastColumn = otherSheet.Cells.SpecialCells(xlCellTypeLastCell).Column
' Loop through each cell in other sheet and copy to primary table
Dim otherRow As Long
Dim otherColumn As Long
For otherRow = 1 To lastRow
For otherColumn = 1 To lastColumn
' Copy cell value from other sheet to corresponding cell in primary table
primaryWorkbook.Sheets(otherSheet.Name).Cells(otherRow, otherColumn).Value = otherSheet.Cells(otherRow, otherColumn).Value
Next otherColumn
Next otherRow
Next otherSheet
' Close other table workbook without saving changes
otherWorkbook.Close SaveChanges:=False
Next otherTableFile
' Save and close primary table workbook
primaryWorkbook.Save
primaryWorkbook.Close
' Notify user of completion
MsgBox "Primary table filled successfully!"
End Sub
To use this script, follow these steps:
- Press
ALT + F11 to open the VBA Editor in Excel.
- Insert a new module by clicking
Insert > Module in the menu.
- Copy and paste the script into the module.
- Close the VBA Editor.
- Run the script from Excel by pressing
ALT + F8 to open the macros dialog, selecting FillPrimaryTable, and clicking Run.
This script will prompt the user to select the primary table file and other tables files. It will then loop through each selected other table file, copy the values from each sheet, and paste them into the corresponding sheet in the primary table file. Finally, it will save and close the primary table file and display a completion message to the user.