This For loop REFUSES TO WORK AND It'S ANNOYING PLEASE HELP ME

WrldIntlR

New Member
Joined
Jul 30, 2011
Messages
41
Hello all,

So I have a userform that tracks credits throughout a month. I need to keep a running tally of the credits given on the current day. This is what I have. It runs through the loop once, and quits. It's very annoying. Please for your assistance I will be grateful.

Code:
Function CalcDlyTtl() As Long
 
Dim i As Long
Dim l As Long
Dim iRow As Long
 
Workbooks(FileNameIs).Activate
 
ActiveWorkbook.Worksheets("OCCData").Activate
 
l = 0
 
iRow = ActiveSheet.Cells(Rows.Count, 1) _
  .End(xlUp).Row
 
With ActiveSheet
 
For i = 1 To iRow
    MsgBox (.Cell(i, 6).Value)
    If Format(.Cell(i, 8).Value, "MM-DD") = Format(Now, "MM-DD") _
    Then
        l = l + .Cell(i, 6).Value
    End If
Next
End With
 
ThisWorkbook.Activate
 
CalcDlyTtl = l
 
End Function
 
Instead of all that code, why not simply:

Code:
Function CalcDlyTtl() As Long
 
    With Workbooks(FileNameIs).Worksheets("OCCData")
        With .Range("A1", Range("A" & .Rows.Count).End(xlUp))
            CalcDlyTtl = WorksheetFunction.SumIf(.Offset(, 7), Date, .Offset(, 5))
        End With
    End With
 
End Function

Try to avoid loops in Excel and even more, use builtin functionality like the SumIf function.

Normally that's exactly what I would do, but the spreadsheet I'm working with contains values from the entire month, I need to add up the values only for the current day. The only reason I have to do this at all is because the users have a tendency to close out of the application and re-open it randomly, so I can't just keep a running tally from when it's open.
 
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
And just for my edification, what is the difference??

Cell does not exist. Cells refers to a certain cell on a sheet.

You could have used intellisense after the With statement, when you type . it will give you a list of possible choices (methods, properties, ...) In that list, Cells will be shown.

If you use ActiveSheet, that does not work. You'd better not activate or select specific cells, but work with the sheets directly. Just as I showed in my With ... End With construct above. Much neater, faster and no flickering of the screen.
 
Upvote 0
Normally that's exactly what I would do, but the spreadsheet I'm working with contains values from the entire month, I need to add up the values only for the current day.

That's what my SumIf example gave you - right?
 
Upvote 0
Cell does not exist. Cells refers to a certain cell on a sheet.

You could have used intellisense after the With statement, when you type . it will give you a list of possible choices (methods, properties, ...) In that list, Cells will be shown.

Also using

Option Explicit

At the top of your modules will reveal these types of errors.
 
Upvote 0
Also using

Option Explicit

At the top of your modules will reveal these types of errors.

all good things to learn. In truth I've probably written about 700 lines of code in what could have been done in 300, but I'm still a n3wb (also I'm not getting paid for it).

TBHWY, I guess I don't understand the SUMIF statement. I'll have to research that on my own. I really, really appreciate all of all y'alls help on this. It's working perfectly now.
 
Upvote 0

Forum statistics

Threads
1,225,151
Messages
6,183,197
Members
453,151
Latest member
Lizamaison

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