Results 1 to 3 of 3

Thread: [EXCEL] Using CountIf in VBA

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    5

    Exclamation [EXCEL] Using CountIf in VBA

    I am looking for some help fixing my counting problem. I am trying to count the number of rows that meet 2 criteria. The CountIf is using 2 different labels as the criteria. Here is what I have so far.

    Code:
    Private Sub CmdBtn_Click()
    'Perform search of column 'D' and return
    'results to LblNum
    
    Dim num As Integer
    
    ActiveSheet.Unprotect
    
    num = Application.CountIf(Sheets("Sheet1").Range("D:D", LblDate1) - Application.CountIf(Sheets("Sheet1").Range("D:D", LblDate2)))
    
    LblNum = num
    
    ActiveSheet.Protect
    
    End Sub

  2. #2

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    5

    Resolved Re: [EXCEL] Using CountIf in VBA

    If figured it out myself.

    Code:
    Private Sub CmdBtn_Click()
    'Perform search of column 'D' and return
    'results to LblNum
    
    Dim num As Integer
    Dim date1 As String, date2 As String
    
    ActiveSheet.Unprotect
    
    date1 = LblDate1.Caption
    date2 = LblDate2.Caption
    
    If date1 = date2 Then
        num = Application.CountIf(Columns("D:D"), date1)
    
    Else
        num = Application.CountIf(Columns("D:D"), date1) + Application.CountIf(Columns("D:D"), date2)
    
    End If
    
    LblNum = num
    
    ActiveSheet.Protect
    End Sub

  3. #3
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: [EXCEL] Using CountIf in VBA

    The code might fail under some circumstances. Would suggest this small change... See the code in red...

    Code:
    Private Sub CmdBtn_Click()
        Dim num As Integer, date1 As String, date2 As String
    
        ActiveSheet.Unprotect
    
        date1 = Trim$(LblDate1.Caption)
        date2 = Trim$(LblDate2.Caption)
    
        If date1 = date2 Then
            num = Application.CountIf(Columns("D:D"), date1)
        Else
            num = Application.CountIf(Columns("D:D"), date1) + _
            Application.CountIf(Columns("D:D"), date2)
        End If
    
        LblNum = num
    
        ActiveSheet.Protect
    End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

Posting Permissions

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



Click Here to Expand Forum to Full Width