|
-
Jun 11th, 2010, 06:24 PM
#1
Thread Starter
New Member
[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
-
Jun 11th, 2010, 06:47 PM
#2
Thread Starter
New Member
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
-
Jun 12th, 2010, 02:57 AM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|