Search multiple headings(Columns) in other sheet, copy data and paste in main file

Zahid0111

New Member
Joined
Mar 8, 2020
Messages
23
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I need a VBA code for a button which when clicked browse for other excel file, search for specific sheet named “Farmer History” in it. In this sheet it looks for A1 complete row and search heading “Crop Area” and copy this column data to main file(where button embedded) in sheet named “Berkhund” at F Column below last cell is used.

The same to be done for other 2 columns too i.e

Looks for “Target Qty” in first row at same sheet “Farmer History” and paste in main file sheet “Berkhund” at R Column below last cell is used

Looks for “Commulative Sold” in first row at same sheet “Farmer History” and paste in main file sheet “Berkhund” at S Column below last cell is used.Code which i tried is given below but it cannot BROWSE for file, search and paste back in main file:
 

Attachments

  • data to paste here.PNG
    data to paste here.PNG
    27.8 KB · Views: 12
  • headers to search in this.PNG
    headers to search in this.PNG
    109.2 KB · Views: 14

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
Will the file you want to find always be the same file? If so, what is the full path to the folder containing the file?
 
Upvote 0
Will the file you want to find always be the same file? If so, what is the full path to the folder containing the file?
Yes it will look for same file...
File can be anywhere, user will select the file by using Application.GetOpenFilename method..
 
Upvote 0
Try:
VBA Code:
Sub CopySheet()
    Application.ScreenUpdating = False
    Dim flder As FileDialog, FileName As String, FileChosen As Integer, wkbSource As Workbook, desWS As Worksheet, header As Range, LastRow As Long
    Set desWS = ThisWorkbook.Sheets("Berkhund")
    Set flder = Application.FileDialog(msoFileDialogFilePicker)
    flder.Filters.Add "Excel Macros Files", "*.xlsx"
    FileChosen = flder.Show
    FileName = flder.SelectedItems(1)
    Set wkbSource = Workbooks.Open(FileName)
    With wkbSource.Sheets("Farmer History")
        LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        Set header = .Rows(1).Find("Crop Area", LookIn = xlValues, lookat:=xlWhole)
        If Not header Is Nothing Then
            Cells(2, header.Column).Resize(LastRow - 1).Copy desWS.Cells(desWS.Rows.Count, "F").End(xlUp).Offset(1)
        End If
        Set header = .Rows(1).Find("Target Qty", LookIn = xlValues, lookat:=xlWhole)
        If Not header Is Nothing Then
            Cells(2, header.Column).Resize(LastRow - 1).Copy desWS.Cells(desWS.Rows.Count, "R").End(xlUp).Offset(1)
        End If
        Set header = .Rows(1).Find("Commulative Sold", LookIn = xlValues, lookat:=xlWhole)
        If Not header Is Nothing Then
            Cells(2, header.Column).Resize(LastRow - 1).Copy desWS.Cells(desWS.Rows.Count, "S").End(xlUp).Offset(1)
        End If
    End With
    wkbSource.Close savechanges:=False
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi thank you for reply...but i m getting the attached error, can you please check this
 

Attachments

  • error 1.PNG
    error 1.PNG
    65.9 KB · Views: 10
  • eror2.PNG
    eror2.PNG
    57.1 KB · Views: 10
Upvote 0
In the second picture you posted it looks like the following 2 lines of code have a space to the left of the sheet name.
VBA Code:
Set header = .Rows(1).Find(" Crop Area", LookIn = xlValues, lookat:=xlWhole)
Set header = .Rows(1).Find(" Target City", LookIn = xlValues, lookat:=xlWhole)
The macro that I posted does not have those spaces. Check the macro and make sure that those spaces are removed.
 
Upvote 0
Yes the spaces are also in source sheet as attached in picture that,s why i copied the header from source sheet to the code
snapshot.PNG
 
Upvote 0
My apologies. I somehow missed putting in a colon after "Lookin". Try the following:
Rich (BB code):
Sub CopySheet()
    Application.ScreenUpdating = False
    Dim flder As FileDialog, FileName As String, FileChosen As Integer, wkbSource As Workbook, desWS As Worksheet, header As Range, LastRow As Long
    Set desWS = ThisWorkbook.Sheets("Berkhund")
    Set flder = Application.FileDialog(msoFileDialogFilePicker)
    flder.Filters.Add "Excel Macros Files", "*.xlsx"
    FileChosen = flder.Show
    FileName = flder.SelectedItems(1)
    Set wkbSource = Workbooks.Open(FileName)
    With wkbSource.Sheets("Farmer History")
        LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        Set header = .Rows(1).Find("Crop Area", LookIn:=xlValues, lookat:=xlWhole)
        If Not header Is Nothing Then
            Cells(2, header.Column).Resize(LastRow - 1).Copy desWS.Cells(desWS.Rows.Count, "F").End(xlUp).Offset(1)
        End If
        Set header = .Rows(1).Find("Target Qty", LookIn:=xlValues, lookat:=xlWhole)
        If Not header Is Nothing Then
            Cells(2, header.Column).Resize(LastRow - 1).Copy desWS.Cells(desWS.Rows.Count, "R").End(xlUp).Offset(1)
        End If
        Set header = .Rows(1).Find("Commulative Sold", LookIn:=xlValues, lookat:=xlWhole)
        If Not header Is Nothing Then
            Cells(2, header.Column).Resize(LastRow - 1).Copy desWS.Cells(desWS.Rows.Count, "S").End(xlUp).Offset(1)
        End If
    End With
    wkbSource.Close savechanges:=False
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Great, now it,s working very fine....Thank you so much for your time and work... :)
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,154
Members
453,021
Latest member
Justyna P

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