Consulting

Results 1 to 7 of 7

Thread: Why is the sub or Function not defined for the following code

  1. #1

    Question Why is the sub or Function not defined for the following code

    Is it the application.worksheet.function that's causing the issue ?

    Sub YesAmountFile()
    Dim TotalYesNum As Integer
    Dim TotalYes As String
    Dim counter2 As Long
    Dim lengthoflist As Long

    lengthoflist = Sheets("RawData").UsedRange.Rows.Count
    For counter2 = 2 To lengthoflist
    If Cells(counter2, 7) = "Yes" Then
    TotalYesNum = Application.WorksheetFunction.CountIf(Cell("counter2: 7"), "Yes")

    End If
    Range("t10") = TotalYesNum
    Next counter2
    End Sub

  2. #2
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,722
    Location
    No, this is the problem:
    Cell("counter2: 7")
    It should be:
    Cells(counter2, 7)
    as earlier in the code.

    However, it doesn't really make any sense to use Countif on one cell - and you already know it contains the value you want.
    Be as you wish to seem

  3. #3
    Quote Originally Posted by Aflatoon View Post
    No, this is the problem:
    Cell("counter2: 7")
    It should be:
    Cells(counter2, 7)
    as earlier in the code.

    However, it doesn't really make any sense to use Countif on one cell - and you already know it contains the value you want.
    Counter2 is the row, I defined counter2 as a loop where it will end once it goes to the last used row. lengthoflist = Sheets("RawData").UsedRange.Rows.
    As I want it to continually loop and add up all the "yes" in that specific column which is (counter2, 7)

  4. #4
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,722
    Location
    In that case all you really need is:
    Sub YesAmountFile()
       Range("t10") = Application.WorksheetFunction.CountIf(Sheets("RawData").Range("G:G"), "Yes")
    End Sub
    Be as you wish to seem

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location
    As I want it to continually loop and add up all the "yes" in that specific column which is (counter2, 7)
    Aflatoon's technique is better, but in terms of the original logic ..


    Sub YesAmountFile()
    
     Dim TotalYesNum As Integer
     Dim TotalYes As String
     Dim counter2 As Long
     Dim lengthoflist As Long
    
     lengthoflist = Sheets("RawData").UsedRange.Rows.Count
    
    For counter2 = 2 To lengthoflist
         If Cells(counter2, 7) = "Yes" Then
            TotalYesNum = TotalYesNum + 1 '<<----------------------------- Not CountIf()
        End If
    
        Range("t10") = TotalYesNum
    Next counter2
    
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,722
    Location
    Small caveat: if you do that, you should also use:
    Dim TotalYesNum As Long
    rather than Integer.
    Be as you wish to seem

  7. #7
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Always declare Row and Column counting Variables as longs
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •