I have created a Template I share with my team. The Template gets updated with new VB and formulas periodically. I'm trying to create a way to easily migrate data to the unpopulated template from a workbook which is populated with data.
Task:
Copy from: Workbook A, Sheet "MyData", Entry[FEB COST], Entry[MAR COST], Entry[APR COST], Entry[MAY COST], Entry[JUN COST], etc
Paste To (Template): Workbook B, Sheet "MyData", Table[FEB COST], Table[MAR COST], Table[APR COST], Table[MAY COST], Table[JUN COST], etc
Template Table Columns always have 5 blank rows and populated workbooks has 20 or more rows.
The Sheet "MyData" has many columns and many with formulas so I don't want to overwrite those with paste. I only want to migrate the "Month COST" and "Month PROFIT" columns which are number fields manually entered. These 2 columns occur every 4 columns. There are Blank cells in the copy range.
The MyData sheet has a table called "Entry". Usage of named ranges and table names is mandatory, I can't maintain a sheet with references to cells.
This feature imports into the template from a workbook selected by the user. The sheet columns are identical I just need to copy many columns using VBA and not manually.
With the Code below I have to do that one column at a time. I would love to loop through the process.
I'm hoping the new code is VERY simple and not resource intensive. It has to perform well.
I was hoping for something like Union but I don't think I can do that across workbooks. I've looked everywhere but I can't piece together a solution that works. Trying to avoid a ".copy" in favor of a .value = .value solution. But I can't seem to get that to transfer the entire table column, just until the first populated cell.
Task:
Copy from: Workbook A, Sheet "MyData", Entry[FEB COST], Entry[MAR COST], Entry[APR COST], Entry[MAY COST], Entry[JUN COST], etc
Paste To (Template): Workbook B, Sheet "MyData", Table[FEB COST], Table[MAR COST], Table[APR COST], Table[MAY COST], Table[JUN COST], etc
Template Table Columns always have 5 blank rows and populated workbooks has 20 or more rows.
The Sheet "MyData" has many columns and many with formulas so I don't want to overwrite those with paste. I only want to migrate the "Month COST" and "Month PROFIT" columns which are number fields manually entered. These 2 columns occur every 4 columns. There are Blank cells in the copy range.
The MyData sheet has a table called "Entry". Usage of named ranges and table names is mandatory, I can't maintain a sheet with references to cells.
This feature imports into the template from a workbook selected by the user. The sheet columns are identical I just need to copy many columns using VBA and not manually.
With the Code below I have to do that one column at a time. I would love to loop through the process.
Code:
Sub MigrateCostData()
Dim EntryBook As String
Dim rCnt As Long, cCnt As Long
Dim FileandPathFrom As String
Dim fDialog As FileDialog, result As Integer
Dim SourceTrackerFile As String
Dim Rng As Range
'GET SOURCE TRACKER FILE
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
'Optional: FileDialog properties
.AllowMultiSelect = False
.Title = "Please Select Budget Tracker to copy"
.ButtonName = "Select"
'Optional: Add filters
.Filters.Clear
.Filters.Add "Excel files", "*.xlsx;*.xls;*.xlsm"
'Show the dialog. -1 means success!
If fDialog.Show = -1 Then
SourceTrackerFile = .SelectedItems(1)
Else
MsgBox "Stopping because you did not select a file"
Exit Sub
End If
End With
'END GET SOURCE TRACKER FILE
'COPY SOURCE COST DATA
EntryBook = ActiveWorkbook.Name
Application.Calculation = xlCalculationManual
Application.DisplayAlerts = False
Application.ScreenUpdating = False
FileandPathFrom = SourceTrackerFile
Workbooks.Open (FileandPathFrom), False, True
Application.Goto Reference:="Entry[FEB COST]"
Selection.Copy
Workbooks(EntryBook).Activate
Application.Goto Reference:="Entry[FEB COST]"
Selection.PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.Calculation = xlCalculationAutomatic
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
I'm hoping the new code is VERY simple and not resource intensive. It has to perform well.
I was hoping for something like Union but I don't think I can do that across workbooks. I've looked everywhere but I can't piece together a solution that works. Trying to avoid a ".copy" in favor of a .value = .value solution. But I can't seem to get that to transfer the entire table column, just until the first populated cell.