LOOP with non blank cell

pedrojoin

New Member
Joined
Apr 5, 2013
Messages
5
Hi everyone, Im learning VBA and I have this code
Code:
Sub [CODE]SmartRunningTotals()    Dim rng As Range
    Dim cell As Range
    Dim lastRow As Long
    Dim totalTime As Double
 
    ' I'm assuming your time column is in column A
    lastRow = Range("A" & ActiveSheet.Rows.Count).End(xlUp).Row
 
    Set rng = Range("A2:A" & lastRow)
 
    For Each cell In rng
        totalTime = totalTime + cell.Value
        If cell.Offset(, 2).Value <> "" Then
            cell.Offset(, 3).Value = totalTime
            ' reset total after we write it to column D
            totalTime = 0
        End If
    Next
 
End Sub


And the result is
Time Date Result
00:21.6 10/1/2012 1:43 FALSE 00:21.6
01:47.7 10/1/2012 2:13 FALSE 01:47.7
00:56.7 10/1/2012 2:49 FALSE 00:56.7
00:54.9 10/1/2012 3:43
00:11.8 10/1/2012 3:43
02:10.9 10/1/2012 3:46 FALSE 3:17.6(ie.,00:54.9+..+2:10.9)
01:05.4 10/1/2012 3:58 FALSE 01:05.4
00:55.8 10/1/2012 4:53
04:41.8 10/1/2012 4:52
00:26.3 10/1/2012 4:58
00:04.2 10/1/2012 4:58
00:15.3 10/1/2012 4:59
00:06.4 10/1/2012 4:57
00:10.7 10/1/2012 4:56
00:04.4 10/1/2012 4:56
00:04.2 10/1/2012 4:57
00:29.2 10/1/2012 4:57
00:34.5 10/1/2012 4:56
01:22.4 10/1/2012 4:55
00:08.1 10/1/2012 4:55 FALSE 9:23.3(i.e.,00:55.8+...+:08.1)
03:20.9 10/1/2012 4:51 FALSE 03:20.9
00:56.3 10/1/2012 5:42 FALSE 00:56.3





That works fine, but in the inverse procedure that i need to implement. In my spreadsheet the counting starts in the first non blank cell and goes throghout the blank cells until the next non blank cell is reached, and then input the sum in the last blank cell (sum A cells from the first non blank until the last blank cell and then start again from the next non blank cell).
Thanks


00:21.6 10/1/2012 1:43 FALSE 00:21.6
01:47.7 10/1/2012 2:13 FALSE 01:47.7
00:56.7 10/1/2012 2:49 FALSE
00:54.9 10/1/2012 3:43
00:11.8 10/1/2012 3:43 2:34(00:56.7 +00:54.9+00:11.8)
02:10.9 10/1/2012 3:46 FALSE 02:10.9
01:05.4 10/1/2012 3:58 FALSE
00:55.8 10/1/2012 4:53
04:41.8 10/1/2012 4:52
00:26.3 10/1/2012 4:58
00:04.2 10/1/2012 4:58
00:15.3 10/1/2012 4:59
00:06.4 10/1/2012 4:57
00:10.7 10/1/2012 4:56
00:04.4 10/1/2012 4:56
00:04.2 10/1/2012 4:57
00:29.2 10/1/2012 4:57
00:34.5 10/1/2012 4:56
01:22.4 10/1/2012 4:55 (01:05.400:55.8+04:41.8+...+01:22.4)

00:08.1 10/1/2012 4:55 FALSE 00:08.1
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Hi and Welcome to the Board,

Try modifying this line to...
Code:
        totalTime = totalTime + cell.Value
        [COLOR="#0000CD"][B]If cell.Offset(1, 2).Value <> "" Or cell.Row = lastRow Then[/B][/COLOR]
            cell.Offset(, 3).Value = totalTime
 
Upvote 0

Forum statistics

Threads
1,214,629
Messages
6,120,630
Members
448,973
Latest member
ChristineC

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