pulling one coumn & related data from one file to other in excel

ssbhatban

Board Regular
Joined
Oct 20, 2015
Messages
81
Office Version
  1. 2010
Platform
  1. Windows
Dear friends

I have two excel files
1. master
2. marks

There are multiple columns in each file . . now i am creating one more file called combine. my reference column is barcode column in both master & marks file. i want to bring all the data in master file to combine file as well as data from marks file to combine file . data from master file should come as it is , but the barcode in marks file will not be arranged as in master file. i want it to get arranged as in master file when it is imported to combine file and same way marks & bundle columns should also should get arranged in combine file

i am attaching image files & excel sample file for reference. i will be having hundreds of data. i will be having all the file in the same folder
what is the best & simple way to do it

thanks in advance
 

Attachments

  • marks.JPG
    marks.JPG
    153.5 KB · Views: 27
  • master.JPG
    master.JPG
    216.1 KB · Views: 27
  • combined-min.JPG
    combined-min.JPG
    88.1 KB · Views: 27

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Make sure that the combine file has a blank sheet named "Combined". Place this macro in the combine file. Change the sheet names (in red) and the file names (in blue) to suit your needs. Make sure that the master and marks workbooks are open before you run the macro.
Rich (BB code):
Sub CopyData()
    Application.ScreenUpdating = False
    Dim master As Workbook, marks As Workbook, combined As Worksheet
    Dim Arr As Variant, i As Long, srcRng As Range, x As Long
    Set master = Workbooks("Master.xlsx")
    Set marks = Workbooks("Marks.xlsx")
    Set combined = ThisWorkbook.Sheets("Combined")
    With marks.Sheets("Sheet1")
        Arr = .Range("B2", .Range("B" & .Rows.Count).End(xlUp)).Resize(, 3).Value
        .Range("B1:D1").Copy combined.Range("J1")
    End With
    With combined
        master.Sheets("Sheet1").UsedRange.Cells.Copy .Range("A1")
        Set srcRng = .Range("H2", .Range("H" & .Rows.Count).End(xlUp))
        For i = LBound(Arr) To UBound(Arr)
            x = Application.Match(Arr(i, 1), srcRng, 0)
            If Not IsError(x) Then
                .Range("J" & x).Resize(, 3).Value = Array(Arr(i, 1), Arr(i, 2), Arr(i, 3))
            End If
        Next i
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Dear friend

Thank you very much. working like a charm. you have saved my day.
I am facing only one small problem. the values pulled from sheet 'marks' is getting arranged properly as per requirement but going one row up & header is not getting copied. any solutions to that. i am attaching the image
i have placed the code n a module as shown i another image. is it ok
can u just explain me exactly how this is functioning. i am getting a bit confused
thanks
 

Attachments

  • mis-match-min.JPG
    mis-match-min.JPG
    80.4 KB · Views: 15
  • code-min.JPG
    code-min.JPG
    43.3 KB · Views: 14
Upvote 0
Dear Friend

in addition to the above problem. one more small problem i encountered is when there are blank cell or rows or some other characters which are not matching with another file in any file of the two files. it is giving the following error message.
find the attached images
 

Attachments

  • blank-error.JPG
    blank-error.JPG
    76.2 KB · Views: 8
  • blank-error-1.JPG
    blank-error-1.JPG
    73.1 KB · Views: 10
Upvote 0
This should take care of the blank cells problem.
VBA Code:
Sub CopyData()
    Application.ScreenUpdating = False
    Dim master As Workbook, marks As Workbook, combined As Worksheet
    Dim Arr As Variant, i As Long, srcRng As Range, x As Long
    Set master = Workbooks("Master.xlsx")
    Set marks = Workbooks("Marks.xlsx")
    Set combined = ThisWorkbook.Sheets("Combined")
    With marks.Sheets("Sheet1")
        Arr = .Range("B2", .Range("B" & .Rows.Count).End(xlUp)).Resize(, 3).Value
        .Range("B1:D1").Copy combined.Range("J1")
    End With
    With combined
        master.Sheets("Sheet1").UsedRange.Cells.Copy .Range("A1")
        Set srcRng = .Range("H2", .Range("H" & .Rows.Count).End(xlUp))
        For i = LBound(Arr) To UBound(Arr)
            If Arr(i, 1) <> "" Then
                x = Application.Match(Arr(i, 1), srcRng, 0)
                If Not IsError(x) Then
                    .Range("J" & x).Resize(, 3).Value = Array(Arr(i, 1), Arr(i, 2), Arr(i, 3))
                End If
            End If
        Next i
    End With
    Application.ScreenUpdating = True
End Sub
Do you have headers in the marks workbook in cells B1, C1 and D1?
 
Upvote 0
This line of code should be copying the headers in B1, C1, D1 from the marks to the combined:
VBA Code:
.Range("B1:D1").Copy combined.Range("J1")
Is this not happening?
 
Upvote 0
No i. Checked that it is not happening when data is pulled from marks file . But it is working when data is pulled from master file
 
Upvote 0
Try this version:
VBA Code:
Sub CopyData()
    Application.ScreenUpdating = False
    Dim master As Workbook, marks As Workbook, combined As Worksheet
    Dim Arr As Variant, i As Long, srcRng As Range, x As Long
    Set master = Workbooks("Master.xlsx")
    Set marks = Workbooks("Marks.xlsx")
    Set combined = ThisWorkbook.Sheets("Combined")
    With marks.Sheets("Sheet1")
        Arr = .Range("B2", .Range("B" & .Rows.Count).End(xlUp)).Resize(, 3).Value
    End With
    With combined
        master.Sheets("Sheet1").UsedRange.Cells.Copy .Range("A1")
        marks.Sheets("Sheet1").Range("B1:D1").Copy .Range("J1")
        Set srcRng = .Range("H2", .Range("H" & .Rows.Count).End(xlUp))
        For i = LBound(Arr) To UBound(Arr)
            If Arr(i, 1) <> "" Then
                x = Application.Match(Arr(i, 1), srcRng, 0)
                If Not IsError(x) Then
                    .Range("J" & x).Resize(, 3).Value = Array(Arr(i, 1), Arr(i, 2), Arr(i, 3))
                End If
            End If
        Next i
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Dear friend
That blank cell problem is solved, thank you very much , but the row shifting up problem still persists, I tried all combinations but no use
 
Upvote 0

Forum statistics

Threads
1,223,734
Messages
6,174,186
Members
452,550
Latest member
southernsquid2

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