Consulting

Results 1 to 7 of 7

Thread: Solved: countif function

  1. #1
    Banned VBAX Regular
    Joined
    Feb 2009
    Posts
    51
    Location

    Cool Solved: countif function

    How to use Excel countif function in VBA?


    Not as an Excel Formula... so that User Defined Function does not appear on Worksheet, but posts only calculated values on Worksheet.

    Refer attached file, where I want VBA to do calculations on Sheet1

    Attached Files Attached Files
    Last edited by sukumar.vb; 08-28-2011 at 06:45 AM. Reason: Attached Excel file

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim i, j

    With Worksheets("Sheet1")

    For j = 3 To .Range("B1").End(xlToRight).Column

    For i = 2 To .Range("A2").End(xlDown).Row

    .Cells(i, j).Formula = "=SUMPRODUCT(--(Sheet2!A2:A200=" & .Cells(i, "A").Address & "),--(Sheet2!B2:B200=" & .Cells(1, j).Address & "))"
    .Cells(i, j).Value = .Cells(i, j).Value
    Next i
    Next j
    End With
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Banned VBAX Regular
    Joined
    Feb 2009
    Posts
    51
    Location

    Question Conditional Count

    Requesting you to review this code again and ...Please advice SOP.
    It might occur when the selection changes on a worksheet.

    Can it not happen with Workbook Open event?

    Please review attached file here and help me know how to put counts in yellow and Green cells of Sheet1.

    Would Pivot table be an idea? I am looking to use different.
    Attached Files Attached Files
    Last edited by sukumar.vb; 08-28-2011 at 04:35 PM. Reason: New revised attachment

  4. #4
    Banned VBAX Regular
    Joined
    Feb 2009
    Posts
    51
    Location

    Thumbs up



    The was Dude's program, I have ever seen. Excellent.
    I made some changes in below attachment.
    This attachment works, but it needs another "FOR NEXT loop" to calculate Total for each row and col.

    I also read your SOP about SUMPRODUCT. G8 Job done.
    Thus, awaiting your response to close this thread in your praise.



    Attached Files Attached Files
    Last edited by sukumar.vb; 08-28-2011 at 06:31 PM.

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Private Sub Workbook_Open()
    Dim i As Long, j As Long

    Application.ScreenUpdating = False

    With Worksheets("Sheet1")

    .Activate

    For j = 2 To .Range("B1").End(xlToRight).Column - 1

    For i = 2 To .Range("A2").End(xlDown).Row - 1

    .Cells(i, j).Formula = "=SUMPRODUCT(--(Sheet2!A2:A200=" & .Cells(i, "A").Address & "),--(Sheet2!B2:B200=" & .Cells(1, j).Address & "))"
    .Cells(i, j).Value = .Cells(i, j).Value
    Next i

    Next j
    .Cells(2, .Range("B1").End(xlToRight).Column).Resize(.Range("A2").End(xlDown).Row - 2).FormulaR1C1 = "=SUM(RC2:RC[-1])"
    .Cells(.Range("A2").End(xlDown).Row, 2).Resize(, .Range("B1").End(xlToRight).Column - 1).FormulaR1C1 = "=SUM(R2C:R[-1]C)"
    End With

    Application.ScreenUpdating = True
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    And yes, a pivot would work great.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    Banned VBAX Regular
    Joined
    Feb 2009
    Posts
    51
    Location
    You are really distinguished member of VBAX.

Posting Permissions

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