VBA Copy Multiple Non-Contiguous Table Columns from One Workbook to the same place in another Workbok

bbjok

New Member
Joined
Apr 12, 2016
Messages
3
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.

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.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

Forum statistics

Threads
1,223,276
Messages
6,171,138
Members
452,381
Latest member
Nova88

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