how to transfer data from one workbook to another workbook

amo

Board Regular
Joined
Apr 14, 2020
Messages
141
Office Version
  1. 2010
Platform
  1. Windows
good afternoon

how to transfer data from one workbook to another workbook based on header names

thank you
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
I understand you have two tables which have the columns in different sequence

In this case you might use Application.Match to determine the index of a header in the target table, then use it to determine the target destination area

If your data are organized in canonical Tables then it might be simpler, because you can use the header directly to identify the destination position, example:
VBA Code:
    ThisWorkbook.Sheets("MASTRO").Range("Table1[[#All],[Description]]").Copy _
       Destination:=Workbooks("MyOldCopy.xlsm").Sheets("MASTRO-OLD").Range("Table1[[#Headers],[Description]]")

Bye
 
Upvote 0
Hi
As I understand
Give this a try
VBA Code:
Sub test()
    Dim rng As Range
    Dim a As Variant
    Dim i, ii, c, r, x
    Application.ScreenUpdating = False
    a = Sheets("sheet1").UsedRange
    With CreateObject("scripting.dictionary")
        For i = 1 To UBound(a, 2)
            If Not .exists(a(1, i)) Then
                x = ""
                For ii = 2 To UBound(a)
                    x = x & a(ii, i) & Chr(2)
                Next
                .Add a(1, i), x
            End If
        Next
        Sheets("sheet2").Select
        For Each rng In Range("g1:j1")    '<<< to be changed as yours
            c = rng.Column: r = rng.Row
            x = .Item(rng.Value)
            x = Split(x, Chr(2))
            Cells(r, c).Offset(1, 0).Resize(UBound(x)) = x
        Next
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
@mohadin

Thank you so much for your help

which are expected

Transfer to another workbook by first selecting the file :)?
 
Upvote 0
Now I don't understand if your problem is selecting a file or selecting a column or what.
I think you should spend some of your time to tell us about your environment, your needs, and for which part of the job you are asking for some help

In the meantime I wasted my time to prepare the following demo workbook:

It contains two tables in two different sheets and the Sub AmoDemo that is intended to demonstrate how using Application.Match to get the index of the Source and the Destination column based on the column header
Code:
Sub AmoDemo()
Dim SoTab As Range, DeTab As Range, Msg As String
Dim ColToCopy, mySInd, myDInd, I As Long
'
Set SoTab = Workbooks("4AMO-Mr_Demo_C01011.xlsm").Sheets("Foglio1").Range("A2") '<<< Source Table Starting address
Set DeTab = Workbooks("4AMO-Mr_Demo_C01011.xlsm").Sheets("Foglio2").Range("B2") '<<< Destination Table Starting address
ColToCopy = Array("B", "D", "E")                                                    '<<< List of Headers to copy
'
For I = 0 To UBound(ColToCopy)
    mySInd = Application.Match(ColToCopy(I), SoTab.Resize(1, 100), False)       'Get Source Column Index
    myDInd = Application.Match(ColToCopy(I), DeTab.Resize(1, 100), False)       'Get Destination Col Index
    If Not (IsError(mySInd) Or IsError(myDInd)) Then                            'if both valid, then...
        Range(SoTab.Cells(2, mySInd), SoTab.Cells(1, mySInd).End(xlDown)).Copy _
          Destination:=DeTab.Cells(1, myDInd).End(xlDown).Offset(1, 0)          '.... copy from.. to..
    Else
        Msg = Msg & ", " & ColToCopy(I)                                         '...else log the missed header
    End If
Next I
'Completion message:
If Len(Msg) > 0 Then
    MsgBox ("Completed, EXCEPT:" & vbCrLf & Mid(Msg, 3))
Else
    MsgBox ("Completed...")
End If
End Sub
Copy the code in a standard module of your vba project; the lines marked <<< need to be customized on your situation. To be more specific, SoTab and DeTab need to point to the topleft cell of your table headers; you will specify the workbook, the sheet and the cell address.
ColToCopy is the list of your headers of your columns to copy.

A final msgbox will list any column that was not found either in the source table or the destination one.

Bye
 
Upvote 0
Hi,
Try this one hope It's Ok with you
VBA Code:
Sub OMA2()
    Dim fDialog As FileDialog
    Dim wbk, Mywbk As Workbook
    Dim rng As Range
    Dim a As Variant
    Dim i, ii, c, r, x, y, z
    Set Mywbk = ActiveWorkbook
    Application.ScreenUpdating = False
    On Error Resume Next
    Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
    With fDialog
        .AllowMultiSelect = False
        .Title = "PICK FILE"
        .InitialFileName = "C:\"
        .Filters.Clear
        .Filters.Add "All supported files", "*.xlsm", "*.xlsx"
        .SelectedItems.Application.Sort
        If .Show = True Then
            Dim fPath As Variant
            fPath = .SelectedItems.Item(1)
            Set wbk = Workbooks.Open(Filename:=fPath)
        End If
    End With
    Mywbk.Activate
    a = Mywbk.Sheets("sheet1").UsedRange
    With CreateObject("scripting.dictionary")
        For i = 1 To UBound(a, 2)
            If Not .exists(a(1, i)) Then
                x = ""
                For ii = 2 To UBound(a)
                    x = x & a(ii, i) & Chr(2)
                Next
                .Add a(1, i), x
            End If
        Next
        For Each rng In wbk.Sheets("sheet2").Range("G1:J1")    '<<< to be changed as yours
            c = rng.Column: r = rng.Row
            y = rng.Value
            x = .Item(y)
            x = Split(x, Chr(2))
            wbk.Sheets("sheet2").Cells(r, c).Offset(1, 0).Resize(UBound(x)) = x
        Next
        End With
    Sheets("sheet2").Select
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
thank you @mohadin
as I expected

But for the copied ones, why only the first line
why not all

photo attached


Screenshot_5.png
Screenshot_6.png
 
Upvote 0
OOOps
Sorry for this
Just Replace this line
Code:
 wbk.Sheets("sheet2").Cells(r, c).Offset(1, 0).Resize(UBound(x)) = x

With
VBA Code:
wbk.Sheets("sheet2").Cells(r, c).Offset(1, 0).Resize(UBound(x)) = Application.Transpose(x)
 
Upvote 0

Forum statistics

Threads
1,215,086
Messages
6,123,040
Members
449,092
Latest member
ikke

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