SOLVED

Excel Makros code seems right but doesn't work as expected

%3CLINGO-SUB%20id%3D%22lingo-sub-2636664%22%20slang%3D%22de-DE%22%3EExcel%20Makros%20code%20seems%20right%20but%20doesn't%20work%20as%20expected%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2636664%22%20slang%3D%22de-DE%22%3E%3CP%3EHello%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20I'd%20love%20to%20get%20some%20Data%20from%20a%20few%20sheets%2C%20listed%20on%20a%20single%20sheet.%20I%20wrote%20this%20code%3A%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CP%3ESub%20Tabelle_zusammanfassen()%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20Dim%20i%20As%20Integer%20Dim%20%3CBR%20%2F%3E%20Summary%20As%20Worksheet%3C%2FP%3E%3CP%3ESet%20Summary%20%3D%20Worksheets(%22Summary%22)%20%3CBR%20%2F%3E%20For%20i%20%3D%202%20To%20Worksheets.Count%3C%2FP%3E%3CP%3ESet%20RangeTargetTab%20%3D%20Worksheets(i).Range(%22b5%22)%20%3CBR%20%2F%3E%20Set%20LastlineSummary%20%3D%20Worksheets(1).Cells(Rows.Count%2C%20%22A%22).End(xlUp)%20%3CBR%20%2F%3E%20RangeTargetTab.Copy%20Destination%3A%3DLastLineSummary%20%3CBR%20%2F%3E%20Next%20i%3C%2FP%3E%3CP%3EEnd%20Sub%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20Instead%20of%20listing%20the%20Data%2C%20it%20pops%20up%20in%20the%20A1%20cell%20one%20by%20one%20and%20when%20it's%20over%20there's%20nothing%20left.%20Any%20idea%20what%20i'm%20doing%20wrong%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2636664%22%20slang%3D%22de-DE%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2636842%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20Makros%20code%20seems%20right%20but%20doesn't%20work%20as%20expected%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2636842%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1126030%22%20target%3D%22_blank%22%3E%40Bixente%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETry%20it%20like%20this...%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3ESub%20Tabelle_zusammanfassen()%0ADim%20i%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20As%20Integer%0ADim%20Summary%20%20%20%20%20%20%20%20%20As%20Worksheet%0ADim%20RangeTargetTab%20%20As%20Range%0ADim%20LastLineSummary%20As%20Long%0A%0ASet%20Summary%20%3D%20Worksheets(%22Summary%22)%0AFor%20i%20%3D%202%20To%20Worksheets.Count%0A%0ASet%20RangeTargetTab%20%3D%20Worksheets(i).Range(%22b5%22)%0ALastLineSummary%20%3D%20Worksheets(1).Cells(Rows.Count%2C%20%22A%22).End(xlUp).Row%20%2B%201%0ARangeTargetTab.Copy%20Summary.Range(%22A%22%20%26amp%3B%20LastLineSummary)%0ANext%20i%0A%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOr%20you%20may%20try%20it%20like%20this...%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-visual-basic%22%3E%3CCODE%3ESub%20Tabelle_zusammanfassen()%0ADim%20Summary%20%20%20%20%20%20%20%20%20As%20Worksheet%0ADim%20ws%20%20%20%20%20%20%20%20%20%20%20%20%20%20As%20Worksheet%0ADim%20RangeTargetTab%20%20As%20Range%0ADim%20i%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20As%20Integer%0ADim%20dlr%20%20%20%20%20%20%20%20%20%20%20%20%20As%20Long%0A%0AApplication.ScreenUpdating%20%3D%20False%0A%0ASet%20Summary%20%3D%20Worksheets(%22Summary%22)%0A%0AFor%20Each%20ws%20In%20ThisWorkbook.Worksheets%0A%20%20%20%20If%20Not%20ws%20Is%20Summary%20Then%0A%20%20%20%20%20%20%20%20dlr%20%3D%20Summary.Cells(Rows.Count%2C%20%22A%22).End(xlUp).Row%20%2B%201%0A%20%20%20%20%20%20%20%20ws.Range(%22B5%22).Copy%20Summary.Range(%22A%22%20%26amp%3B%20dlr)%0A%20%20%20%20End%20If%0ANext%20ws%0A%0AApplication.ScreenUpdating%20%3D%20True%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2641082%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20Makros%20code%20seems%20right%20but%20doesn't%20work%20as%20expected%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2641082%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F394231%22%20target%3D%22_blank%22%3E%40Subodh_Tiwari_sktneer%3C%2FA%3E%26nbsp%3BThank%20you%20so%20much.%20This%20is%20working.%20You're%20already%20a%20hero%20to%20me.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20still%20would%20have%20a%20question%3A%20For%20the%20next%20column%20i%20don't%20need%20the%20data%20from%20the%20same%20cell%20in%20every%20sheet%20but%20always%20from%20the%20last%20line%20of%20a%20table.%20But%20it%20isn't%20always%20the%20last%20line%20from%20the%20sheet.%20The%20table%20always%20starts%20at%20the%20line%208.%20Let's%20say%20i%20have%20a%20table%20with%205%20lines%20and%20i%20need%20the%20data%20from%20the%20last%20one%20in%20column%20A.%20Ist%20this%20possible%20somehow%3F%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EBest%20wishes%3CBR%20%2F%3EBixente%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2641116%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20Makros%20code%20seems%20right%20but%20doesn't%20work%20as%20expected%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2641116%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1126030%22%20target%3D%22_blank%22%3E%40Bixente%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou're%20welcome!%20Yes%2C%20it%20seems%20possible%20but%20to%20discard%20any%20confusion%2C%20I%20suggest%20you%20to%20upload%20a%20sample%20file%20along%20with%20some%20dummy%20data%20on%20data%20sheets%20and%20expected%20output%20on%20Summary%20Sheet%20to%20let%20me%20know%20what%20exactly%20you%20are%20trying%20to%20achieve.%20Add%20some%20comments%20on%20the%20Summary%20Sheet%20to%20describe%20the%20logic%20behind%20the%20output%20if%20necessary.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2642255%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20Makros%20code%20seems%20right%20but%20doesn't%20work%20as%20expected%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2642255%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1126030%22%20target%3D%22_blank%22%3E%40Bixente%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDoes%20the%20following%20code%20work%20for%20you%3F%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3ESub%20Tabelle_zusammanfassen()%0ADim%20Summary%20%20%20%20%20%20%20%20%20As%20Worksheet%0ADim%20ws%20%20%20%20%20%20%20%20%20%20%20%20%20%20As%20Worksheet%0ADim%20RangeTargetTab%20%20As%20Range%0ADim%20i%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20As%20Integer%0ADim%20dlr%20%20%20%20%20%20%20%20%20%20%20%20%20As%20Long%0ADim%20n%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20As%20Variant%0A%0AApplication.ScreenUpdating%20%3D%20False%0A%0ASet%20Summary%20%3D%20Worksheets(%22Summary%22)%0A%0AFor%20Each%20ws%20In%20ThisWorkbook.Worksheets%0A%20%20%20%20If%20Not%20ws%20Is%20Summary%20Then%0A%20%20%20%20%20%20%20%20dlr%20%3D%20Summary.Cells(Rows.Count%2C%20%22E%22).End(xlUp).Row%20%2B%201%0A%20%20%20%20%20%20%20%20n%20%3D%20Application.Match(1%2C%20ws.Columns(1)%2C%200)%0A%20%20%20%20%20%20%20%20If%20Not%20IsError(n)%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20ws.Range(%22A%22%20%26amp%3B%20n).Copy%20Summary.Range(%22E%22%20%26amp%3B%20dlr)%0A%20%20%20%20%20%20%20%20%20%20%20%20ws.Range(%22D%22%20%26amp%3B%20n).Copy%20Summary.Range(%22F%22%20%26amp%3B%20dlr)%0A%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20End%20If%0ANext%20ws%0A%0AApplication.ScreenUpdating%20%3D%20True%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

Hello

I'd love to get  some Data from a few sheets, listed on a single sheet. I wrote this code:

Sub Tabelle_zusammanfassen()

Dim i As Integer
Dim Zusammenfassung As Worksheet

Set Zusammenfassung = Worksheets("Zusammenfassung")
For i = 2 To Worksheets.Count

Set BereichZielTab = Worksheets(i).Range("b5")
Set LetzteZeileZusammenfassung = Worksheets(1).Cells(Rows.Count, "A").End(xlUp)
BereichZielTab.Copy Destination:=LetzteZeileZusammenfassung
Next i

End Sub

Instead of listing the Data, it pops up in the A1 cell one by one and when it's over there's nothing left. Any idea what i'm doing wrong?

12 Replies

@Bixente 

 

Try it like this...

Sub Tabelle_zusammanfassen()
Dim i               As Integer
Dim Summary         As Worksheet
Dim RangeTargetTab  As Range
Dim LastLineSummary As Long

Set Summary = Worksheets("Summary")
For i = 2 To Worksheets.Count

Set RangeTargetTab = Worksheets(i).Range("b5")
LastLineSummary = Worksheets(1).Cells(Rows.Count, "A").End(xlUp).Row + 1
RangeTargetTab.Copy Summary.Range("A" & LastLineSummary)
Next i

End Sub

 

Or you may try it like this...

Sub Tabelle_zusammanfassen()
Dim Summary         As Worksheet
Dim ws              As Worksheet
Dim RangeTargetTab  As Range
Dim i               As Integer
Dim dlr             As Long

Application.ScreenUpdating = False

Set Summary = Worksheets("Summary")

For Each ws In ThisWorkbook.Worksheets
    If Not ws Is Summary Then
        dlr = Summary.Cells(Rows.Count, "A").End(xlUp).Row + 1
        ws.Range("B5").Copy Summary.Range("A" & dlr)
    End If
Next ws

Application.ScreenUpdating = True
End Sub

 

 

 

@Subodh_Tiwari_sktneer Thank you so much. This is working. You're already a hero to me.

 

I still would have a question: For the next column i don't need the data from the same cell in every sheet but always from the last line of a table. But it isn't always the last line from the sheet. The table always starts at the line 8. Let's say i have a table with 5 lines and i need the data from the last one in column A. Ist this possible somehow?


Best wishes
Bixente

@Bixente 

You're welcome! Yes, it seems possible but to discard any confusion, I suggest you to upload a sample file along with some dummy data on data sheets and expected output on Summary Sheet to let me know what exactly you are trying to achieve. Add some comments on the Summary Sheet to describe the logic behind the output if necessary.

@Subodh_Tiwari_sktneer Thank you so much for your reply. I hope the file is clear to you. Let's say i'll always need 1 and 12.08.2021. 

@Bixente 

 

Does the following code work for you?

Sub Tabelle_zusammanfassen()
Dim Summary         As Worksheet
Dim ws              As Worksheet
Dim RangeTargetTab  As Range
Dim i               As Integer
Dim dlr             As Long
Dim n               As Variant

Application.ScreenUpdating = False

Set Summary = Worksheets("Summary")

For Each ws In ThisWorkbook.Worksheets
    If Not ws Is Summary Then
        dlr = Summary.Cells(Rows.Count, "E").End(xlUp).Row + 1
        n = Application.Match(1, ws.Columns(1), 0)
        If Not IsError(n) Then
            ws.Range("A" & n).Copy Summary.Range("E" & dlr)
            ws.Range("D" & n).Copy Summary.Range("F" & dlr)
        End If
    End If
Next ws

Application.ScreenUpdating = True
End Sub

@Subodh_Tiwari_sktneer First of all, i want to thank you for your time. I really apriciate it a lot.

Of course the code works in the Dummy File. But it doesn't work on my file. I think in spite of the file i sent you, we still missunderstood each other. If i get that right, this code only works if i am looking for "1". But of course the Data can be different.

I took the time to rewrite the file with some Dummy Data as i have it 1:1 (but with less sheets of course). While i was preparing this file, i saw a major mistake: For the column G, i don't need the data from the last row of the table in Q but the very last line. I'm so sorry.  I think you should get it with this file.

Again, thank you very much for your help.

@Subodh_Tiwari_sktneer P.S. This second file now has the same amount of rows on every sheet. Of course the original one doesn't. Otherwise the first code you sent me would work again.


Best wihes
Bixente

@Bixente 

To discard any confusion and considering the fact that your latest sample file may not still represent your actual data, could you please let me know what logic you would apply manually to pick the data to be copied to the Master Sheet? How do you identify the last line to be copied? The line which appears before a cell in column A which contains a sub-string "Total" in it on each Sheet? And if this is correct, there is only one instance of Total in column A?

e.g. on the first data sheet, cell A12 contains text "Total 111111 NAME" so the code will identify this cell and then go above and find row#10 as the last line of your data and pick the data from A12, J12, and Q12 and copy it to Master Sheet. Is this logic correct?

Okay so... Manually i would do this:

For Column E in the Summarysheet: Look for the last data in column A before "Total 11111121 NAME"
For Column F in the Summarysheet: Look for the last data in column J
For Column G in the Summarysheet: Look for the last data in column Q

But the issue is that the amount of Rows with Data starting from Row 8 is variating. Otherwise i would just link it with the cell as in your first code.

I am sorry for my bad communication. I try my best to explain the issue. Thank you very much.
best response confirmed by Bixente (Occasional Contributor)
Solution

@Bixente 

Please try this...

Sub CopyDataToMasterSheet()
Dim wsSummary       As Worksheet
Dim ws              As Worksheet
Dim dlr             As Long
Dim RngTotal        As Range
Dim r               As Long

Application.ScreenUpdating = False

Set wsSummary = Worksheets("Summary")

For Each ws In ThisWorkbook.Worksheets
    If Not ws Is wsSummary Then
        Set RngTotal = ws.Columns(1).Find(what:="Total", lookat:=xlPart)
        If Not RngTotal Is Nothing Then
            r = RngTotal.End(xlUp).Row
            If wsSummary.Range("H1").Value = "" Then
                dlr = 1
            Else
                dlr = wsSummary.Cells(Rows.Count, "H").End(xlUp).Row + 1
            End If
            ws.Range("A" & r).Copy wsSummary.Range("H" & dlr)
            ws.Range("J" & r).Copy wsSummary.Range("I" & dlr)
            ws.Range("Q" & RngTotal.Row).Copy wsSummary.Range("J" & dlr)
        End If
    End If
Next ws
Application.ScreenUpdating = True
End Sub

 

In the attached, you may click the button called "Copy Data" on Summary Sheet to run the code.

 

 

@Subodh_Tiwari_sktneer 

I could adapt the code for my file. What an amazing feeling of satisfaction. I thank you very much for your patience, time and precious help. I wish you all the best.

You're welcome @Bixente! Glad it worked as desired.

 

Please take a minute to accept the post with the proposed solution as a Best Response to mark your question as Solved.