Consulting

Results 1 to 7 of 7

Thread: Code for a changable range in COUNTIF function

  1. #1
    VBAX Newbie
    Joined
    Nov 2017
    Posts
    4
    Location

    Code for a changable range in COUNTIF function

    Hi, I am new to VBA.

    I use this code: Worksheets("Data").Range("t19").Value = [COUNTIF(Data!W40:W174,">0.5")]

    It is working nicely but I would like to use it in a loop and change the range W40:W174 in each iteration (i): W40:W(i). For example, for i = 173 the range would be W40:W173.
    How should I rewrite the code to have a changeable range?

    I tried the following but it is not working:

    Dim i As Integer
    i = 174
    Worksheets("Data").Range("t19").Value = [COUNTIF(Range(.Cells(40, 23), .Cells(i, 23)),">0.5")]


    Thank you in advance for any help.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Not tested, but I usually do something like this


    Dim sAddr as string
    
    sAddr = "Data!" & Range(.Cells(40, 23), .Cells(i, 23)).Address
    
    Worksheets("Data").Range("t19").Value = Application.WorksheetFunction.CountIf (sAddr, ">0.5")
    ---------------------------------------------------------------------------------------------------------------------

    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

  3. #3
    VBAX Newbie
    Joined
    Nov 2017
    Posts
    4
    Location
    Thank you, Paul, but it is not working.

    It gives me this message: "Compile error: Invalid or unqualified reference" and highlights ".Cells".

    Maybe there is an error in the Range(.Cells(40, 23), .Cells(i, 23)) part of the code which I cannot see?

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Without a sample workbook and the rest of the macro, it's still just guessing (you can attach one -- look at #2 in my signature)

    I assume that since you used .Cells() in your sample that you were inside a With / End With

    If not, then try


    With Worksheets("Data")
    sAddr = "Data!" & Range(.Cells(40, 23), .Cells(i, 23)).Address
    End With
    ---------------------------------------------------------------------------------------------------------------------

    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

  5. #5
    VBAX Newbie
    Joined
    Nov 2017
    Posts
    4
    Location
    I am sorry. I attach the workbook and macro now. It is this simple for now:

    [CODE]
    Sub Count()

    Dim sAddr As String
    Dim i As Integer
    i = 174


    With Worksheets("Data")
    sAddr = "Data!" & Range(.Cells(40, 23), .Cells(i, 23)).Address
    End With

    Worksheets("Data").Range("t19").Value = Application.WorksheetFunction.CountIf(sAddr, ">0.5")

    End Sub
    [/CODE ]

    Now it gives me: "Compile error: Type mismatch" nad highlights the second sAddr.

    Thank you for help.
    Attached Files Attached Files

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Sorry - I guess I was in a hurry and forgot the Range( )

    This returns 61



    Option Explicit
    
    Sub Count()
    Dim sAddr As String
    Dim i As Integer
    i = 174
     
    With Worksheets("Data")
        sAddr = "Data!" & Range(.Cells(40, 23), .Cells(i, 23)).Address
        .Range("t19").Value = Application.WorksheetFunction.CountIf(Range(sAddr), ">0.5")
    End With
     
    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

  7. #7
    VBAX Newbie
    Joined
    Nov 2017
    Posts
    4
    Location
    Yes, this works! Many thanks!

Posting Permissions

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