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
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
What is the value of irow after this line executes

iRow = ActiveSheet.Cells(Rows.Count, 1) _
.End(xlUp).Row

Step through the code with F8, and after that line executes, hover your mouse over the irow variable, what is it's value?
 
Upvote 0
What is .Cell in .Cell(i, 8).Value? (occurs a number of times)
 
Upvote 0
What is the value of irow after this line executes

iRow = ActiveSheet.Cells(Rows.Count, 1) _
.End(xlUp).Row

Step through the code with F8, and after that line executes, hover your mouse over the irow variable, what is it's value?

It's 3.
 
Upvote 0
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.
 
Upvote 0
MsgBox (.Cell(i, 6).Value)
should be
MsgBox (.Cells(i, 6).Value)

You have .Cell repeated 3 times, change all 3 to Cells
 
Upvote 0
OK, so after stepping through the code, when I get to this the iRow value that it returns is correct value, but the function just quits entirely and returns 0 at the first IF statement. Just dumps out entirely.

The value is either going to empty, or a date in standard Now format.

FRUSTRATING.
 
Upvote 0
MsgBox (.Cell(i, 6).Value)
should be
MsgBox (.Cells(i, 6).Value)

You have .Cell repeated 3 times, change all 3 to Cells

jesus mary and joseph that was it.

Now if you know a way to convert a string to a long I'll love long time.
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,844
Members
452,948
Latest member
UsmanAli786

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