Take data from another close excel workbook with macro

serdarvergili

New Member
Joined
Apr 15, 2019
Messages
7
Hi everyone , firstly english is not my main language sorry for that.I have a main file which is working with 15 different source file.In the main file on A columns datas should be taken from source files on TOTAL sheets on A column.But the problem is a lot of data in TOTAL sheets and they are repeating, so how can I take these data without repeat ? Howewer A column in the main file should be automatic updatable.I tried to make that with excels formulas but it didn't work when the source files close.I think only way to make this situation is macro and I dont know anything about macro.Thanks a lot.

Main File: https://drive.google.com/file/d/1zvF_l4EJhHihd1LkQ02CWr-QY0BB6JaR/view?usp=sharing
One of the Source File: https://drive.google.com/file/d/1UwbrDzco11dKa1RaG0JUu56jri4A7cDY/view?usp=sharing
 
Place this macro in a standard module in your Main workbook and run it from there. Change the source workbook name (in red) to suit your needs. The macro assumes that the destination sheet in Main will always be the first sheet. You can change the number (in blue) to suit your needs. Make sure both workbooks are open.
Code:
Sub CopyData()
    Application.ScreenUpdating = False
    Dim LastRow As Long, srcWS As Worksheet, desWS As Worksheet
    Set srcWS = Workbooks([COLOR="#FF0000"]"Source File 1.xlsx[/COLOR]").Sheets("TOTAL")
    Set desWS = ThisWorkbook.Sheets([COLOR="#0000FF"]1[/COLOR])
    LastRow = srcWS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    srcWS.Range("A3:A" & LastRow).Copy
    desWS.Range("A2").PasteSpecial xlPasteValues
    With desWS
        .Range("A1:A" & .Range("A" & .Rows.Count).End(xlUp).Row).RemoveDuplicates Columns:=1, Header:=xlYes
    End With
    LastRow = desWS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Sheets([COLOR="#0000FF"]1[/COLOR]).Sort.SortFields.Clear
    Sheets([COLOR="#0000FF"]1[/COLOR]).Sort.SortFields.Add Key _
        :=Range("A2:A" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
    With Sheets([COLOR="#0000FF"]1[/COLOR]).Sort
        .SetRange Range("A1:H" & LastRow)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

Forum statistics

Threads
1,223,896
Messages
6,175,265
Members
452,627
Latest member
KitkatToby

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