Copying desired information from one workbook to another workbook with the same worksheet names

K0st4din

Well-known Member
Joined
Feb 8, 2012
Messages
501
Office Version
  1. 2016
  2. 2013
  3. 2011
  4. 2010
  5. 2007
Platform
  1. Windows
Hello, everyone.
Please for your help, I just do not know how to do it with a macro and right now I do it by hand.
I have two working books.
The first: is "Base"
The second - I transfer information about what I want from me (usually last month).
At the base I have worksheets with names (London, Paris, New York, China, etc.) and many more worksheets.
In the second workbook - I have the same worksheets: (London, Paris, New York, China, etc.).
In the workbook "Base" in the above mentioned worksheets I report information every month. Once I have applied it, she -> the information accumulates for each month back and the file becomes very large.
For each worksheet (London, Paris, New York, China, etc.), I filter the past month, for example 4.2018 (in the Base) and put the information in the second workbook only the selected month.
Please for your help, do it somehow with a macro.
The date is in column A2:to the end. Once I've chosen the date, I copy all the rows to the end. In each of the worksheets the rows are different.
A button that Somehow asked me: "What month do you want to copy and apply from Base in the other Workbook?"
Please, if there is any ambiguity, ask to help
 
Last edited:
Try this:
Add more sheet names to array
Run this script from your Workbook name Base

Code:
Sub Test()
'Modified 5/8/2018 5:15 PM  EDT
Application.ScreenUpdating = False
Dim i As Long
Dim b As Long
Dim Lastrow As Long
Dim Lastrowa As Long
'Dim s As Long
Dim ans As Long
Dim mon As Long
Dim WN As String
Dim s As Variant
Dim x As Long
s = Array("London", "Paris", "New York")
x = UBound(s)
WN = "FinalResult" & ".xlsm" ' If you want to modify workbook named just change Master to what ever you want here
mon = Month(InputBox("Enter Date for this Month", "Hello", Date))
    For b = 1 To x + 1
        
        Lastrow = Workbooks("Base.xlsm").Sheets(s(b - 1)).Cells(Rows.Count, "A").End(xlUp).Row
        Lastrowa = Workbooks(WN).Sheets(Sheets(s(b - 1)).Name).Cells(Rows.Count, "A").End(xlUp).Row + 1
            For i = 2 To Lastrow
                ans = Month(Workbooks("Base.xlsm").Sheets(s(b - 1)).Cells(i, 1).Value)
                If ans = mon Then Workbooks("Base.xlsm").Sheets(s(b - 1)).Rows(i).Copy Workbooks(WN).Sheets(s(b - 1)).Rows(Lastrowa): Lastrowa = Lastrowa + 1
            Next
    Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
First I want to thank you for your help.
Please believe me that I do not have another macro I would like to be like your last macros with Array (this way I can determine which worksheets are)
I show you two workbooks, in the Base I have many, many, many worksheets, but I want the named ones to be copied and placed in the Final result.
On the second photo I showed you, which line gives me a mistake and for the moment I can not test it.



 
Upvote 0
In post 5 you said:
The second workbook is called "Final Month Result"
In Post 8 you said In the "Final result" - I have separated specific worksheets
In you image it looks like you have "final results"
You must have the exact proper name entered here.
Other then that I do not know what is wrong
You must have the exact proper name entered here:
Code:
WN = "FinalResult" & ".xlsm"

Mine says FinalResult but maybe it should be
Final Results
Or
Final Result
 
Last edited:
Upvote 0
The name of the second workbook


The error it gives me



I think I have correctly changed the name of the workbook

 
Upvote 0
No, no, I changed everything, but it gives me a mistake in my previous comment, and now I share with you that I can not test it yet.
Here is the line that gives me a mistake, although the names of the workbooks are correct
Code:
[COLOR=#333333]Lastrowa = Workbooks(WN).Sheets(Sheets(s(b - 1)).Name).Cells(Rows.Count, "A").End(xlUp).Row + 1[/COLOR]
 
Upvote 0
Code:
Sub Test()
'Modified 5/8/2018 5:15 PM  EDT ' how i can change format to be only 9.5.2018
Application.ScreenUpdating = False
Dim i As Long
Dim b As Long
Dim Lastrow As Long
Dim Lastrowa As Long
'Dim s As Long
Dim ans As Long
Dim mon As Long
Dim WN As String
Dim s As Variant
Dim x As Long
s = Array("city1", "city2", "city3", "city4", "ect to city28")
x = UBound(s)
WN = "final results" & ".xlsm" ' If you want to modify workbook named just change Master to what ever you want here
mon = Month(InputBox("Enter Date for this Month", "Hello", Date))
    For b = 1 To x + 1
        
        Lastrow = Workbooks("base workbook.xlsm").Sheets(s(b - 1)).Cells(Rows.Count, "A").End(xlUp).Row
        Lastrowa = Workbooks(WN).Sheets(Sheets(s(b - 1)).Name).Cells(Rows.Count, "A").End(xlUp).Row + 1
            For i = 2 To Lastrow
                ans = Month(Workbooks("base workbook.xlsm").Sheets(s(b - 1)).Cells(i, 1).Value)
                If ans = mon Then Workbooks("Base.xlsm").Sheets(s(b - 1)).Rows(i).Copy Workbooks(WN).Sheets(s(b - 1)).Rows(Lastrowa): Lastrowa = Lastrowa + 1
            Next
    Next
Application.ScreenUpdating = True
End Sub

Somehow, in the window where I write a date and a month, I always take this day is today, and I want to determine which month to copy.
all dates are in the format: 1.1.2018, 1.2.2018, 1.12.2018, always the day is 1st, the month and the year are changed. Your format is 9.5.2018 y. ->>> (year)
 
Last edited:
Upvote 0
You cannot do this:
s = Array("city1", "city2", "city3", "city4", "ect to city28")

You have to enter every single sheet name.
Keep going "city5","city6"

on and on.

And you changed this line of code:
You have:
ans = Month(Workbooks("base workbook.xlsm").Sheets(s(b - 1)).Cells(i, 1).Value)
My code was like this:
ans = Month(Workbooks("Base.xlsm").Sheets(s(b - 1)).Cells(i, 1).Value)

Unless your workbook is named:

base workbook.xlsm​

I don't believe that is correct.
Your workbook is named Base or base
not baseWookbook


And for testing purposes I would use a about 3 and see how things work before putting in all 28
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,209
Members
453,022
Latest member
RobertV1609

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