Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Visual Basic (https://www.askmehelpdesk.com/forumdisplay.php?f=469)
-   -   VBA 6.3 in Excel 2003 - "For" subroutine (https://www.askmehelpdesk.com/showthread.php?t=189333)

  • Feb 28, 2008, 02:58 PM
    jmcgranahan
    VBA 6.3 in Excel 2003 - "For" subroutine
    This seems so elementary that I'm afraid to ask, but I need to get this done and I haven't been able to get it to work. I have a pretty basic code that I have been working with and basically what I am looking for is anything in a particular column in my Excel spreadsheet that has the word "Overdue" in it, I want it to calculate the sum paid for that overdue and put it in cell I1 (9th column, 1st row). The word "Overdue" is found in column I in our spreadsheet (which is the 9th column). The paid amount is found in column Q (the 17th column). I created this below and I'm not sure what I am doing wrong. I have originally had the o variable set as currency. Then I have tried it as integer, and neither one does anything. It seems like it should work. Any ideas for this elementary question?
    ------------------------
    Sub Macro2()
    '
    ' Macro2 Macro
    ' Macro recorded 2/28/2008 by jmcgranahan
    '
    ' Keyboard Shortcut: Ctrl+q
    '
    Dim o As Integer
    Dim r As Long
    For r = 1 To 2000
    If Cells(r, 9).Value = "Overdue" Then
    o = o + (Cells(r, 17))
    Exit For
    End If
    Next
    If r = 2001 Then Cells(1, 9) = o
    End Sub


    Jamen McGranahan
  • Feb 29, 2008, 04:44 PM
    kabia
    I don't use VBA but I do use VB6
    and this is how I would do it

    Dim o As Integer
    Dim r As Long
    For r = 1 To 2000
    If lcase(trim(Cells(r, 9).text)) = lcase(trim("Overdue")) Then
    o = o + (Cells(r, 17)).value
    End If
    Next r
    Cells(1, 9) = o
    End Sub
  • Mar 13, 2008, 03:04 PM
    melondotnet
    Hi, I can program in VBA...

    Try replacing cells, with range.

    To get the value, you use .text, but to write to a cell, use .FormulaR1C1

    Note: R1C1 relate to a number-by-number referance, R = Row, C = Column

  • All times are GMT -7. The time now is 08:39 PM.