Looping Through Worksheets

draycut

Board Regular
Joined
Sep 22, 2014
Messages
54
Hi Everyone..

I have a workbook named "Klaus", with a number of worksheets.. The first sheet it named "Ark" further I have a number of other worksheets with the same structure as shown here: (The solumns are A to C..)

IDYearWeight
134200420
123200417
543200413
247200412
364200410
35720047
85320046
24520045
86420044
246720043
54220042
258020041

<tbody>
</tbody>


What I want is a macro that loops through all the worksheets (Except the first one) and copies data from the other worksheets into "Ark". I want data from all three columns copied in BUT, I only want a certain number of rows.. The sum of the first 5 cells from the top of column C is 72.. So lets say that I have in some cell have the value 70, then I want the macro to copy the range Range("A2:C6") into "Ark", so the minimum range of cells across the three columns, where the sum from C2 and downwards is larger than 70 (in this example). Then I want the macro to go on to the next worksheet and do the same (with the same value (70)), but copy the cells in extension of the previously inserted cells in "Ark"..

Hope it makes sense and please dont hesitate the ask clarifying questions :)

So far I have this code..

Code:
Sub codesofar()    
    Application.ScreenUpdating = False
    
    Dim ark As Worksheet
    
    Set ark = Worksheets(1)
    
    For Each ws In Workbook
        If ws.Name <> "Ark" Then
            
            'HELP! ;)
            
            
        End If
    Next ws
    
End Sub

I am using Windows 7 and Excel 2013.

Any help is greatly appreciated! :)
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Hi,

This uses Cell K1 on the Ark worksheet as the marker cell that contains the value 70 or whatever value you want to assign.

I have assumed that there will always be sufficient data in each sheet to add up to this marker value.

EDIT... I forgot to mention. see this line in the code. It sums the cells until the value is greater then the marker value.

Code:
If TempSum > dst.Range("K1") Then

You may want to use >= instead of >

Code:
Sub codesofar()
Dim ws As Worksheet, dst As Worksheet
Dim x As Long, LastRow As Long
Dim TempSum As Long
Set dst = Sheets("ark")
Application.ScreenUpdating = False
For Each ws In ThisWorkbook.Worksheets
    If ws.Name <> dst.Name Then
        LastRow = ws.Cells(Rows.Count, "C").End(xlUp).Row
            For x = 2 To LastRow
                TempSum = TempSum + ws.Cells(x, "C")
                If TempSum > dst.Range("K1") Then
                    LastRow = dst.Cells(Rows.Count, "A").End(xlUp).Row + 1
                    ws.Range("A2").Resize(x - 1, 3).Copy dst.Range("A" & LastRow)
                    TempSum = 0
                    LastRow = 0
                    GoTo getmeout
                End If
            Next
    End If
getmeout:
Next ws
    
End Sub
 
Last edited:
Upvote 0
Hi Mike LH, first of all Thank yor very much, I see your idea! :)

Second.. Where do you put the line that you edited in?
 
Upvote 0
Hi,

The way the code is written it will sum down column c until the sum of those cells is greater than the maker value when this line will evaluate as TRUE:-


Code:
If TempSum > dst.Range("K1") Then

It occurred to me after I'd written the code that you might want to terminate if the sum of column c was greater than or equal to the marker value. If that's what you want you replace the above line of code with this one.

Code:
If TempSum >= dst.Range("K1") Then
 
Upvote 0
Hi Mike LH, first of all, thank you for taking the time, I see your idea! :)

But.. I dont get the "goto getmeout" line? :) This would immediately throw me on to the next sheet after the first iteration, regardless of whether
Tempsum > dst.Range("K1") rigtht? :/

Again,thank you! ;)
 
Upvote 0
Ah, cool, this works like a charm now, and I understand what's going on :)

Thank You so much for your help and explanation! :)
 
Upvote 0
Hi Mike LH, first of all, thank you for taking the time, I see your idea! :)

But.. I dont get the "goto getmeout" line? :) This would immediately throw me on to the next sheet after the first iteration, regardless of whether
Tempsum > dst.Range("K1") rigtht? :/

Again,thank you! ;)

No it won't do that. The code will iterate down column C adding up the values until TEMPSUM exceeds the value of the marker cell. Then and only then will the IF statement evaluate as TRUE and the statements inside the IF statement evaluate. Note that the Goto GetMeOut statement is inside the if --- End if statement. The purpose of that getmeout bit is so that the code stops adding more lines after the marker value has been reached.
 
Upvote 0

Forum statistics

Threads
1,215,372
Messages
6,124,541
Members
449,169
Latest member
mm424

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