Count the number of times the If statement is true

Esmannitor

New Member
Joined
May 23, 2015
Messages
8
Hi,
I am doing an assignment in IT. I don't have that much experience in VBA, so my code below is pretty primitive. I have some troubles with the If statements. I want to count the number of times the If statement is True and bind it to "CountL". So in my assignment as shown below, I want it to search through column 2 to last column and then if there are any cells with "L" as the first character, then it should count it and add it to the CountL, so I can use it later depending on the value.
What is the best way to do this?



Dim AddRow As Integer
With ws.Range("A:A")
Set FindAddRow = .Find(ProjektComboBox2.Value, LookIn:=xlValues)
If Not FindAddRow Is Nothing Then
firstAddress = FindAddRow.Address
Do
AddRow = FindAddRow.Row
Set FindAddRowj = .FindNext(FindAddRow)
Loop While Not FindAddRow Is Nothing And FindAddRow.Address <> firstAddress
End If
End With


Dim LastColumn As Integer
Dim CountL As Integer
LastColumn = WorksheetFunction.CountA(Range("2:2"))

For Target_Cells = 2 To LastColumn
If Left(Range(Cells(AddRow, Target_Cells), Cells(AddRow, Target_Cells)), 1) = "L" Then
CountL = Count(Upper statement = True)
MsgBox "A projectleader do already exist for this project."
End If
Next Target_Cells



Thank you in advance!
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
I modified it slightly but couldn't test it
Code:
Sub NewMacro()
    Dim AddRow As Integer
    With ws.Range("A:A")
        Set FindAddRow = .Find(ProjektComboBox2.Value, LookIn:=xlValues)
        If Not FindAddRow Is Nothing Then
            firstAddress = FindAddRow.Address
            Do
                AddRow = FindAddRow.Row
                Set FindAddRowj = .FindNext(FindAddRow)
            Loop While Not FindAddRow Is Nothing And FindAddRow.Address <> firstAddress
        End If
    End With
    
    Dim LastColumn As Integer
    Dim CountL As Integer
    LastColumn = WorksheetFunction.CountA(Range("2:2"))
    
    CountL = 0
    For Target_Cells = 2 To LastColumn
        If Left(Range(Cells(AddRow, Target_Cells), Cells(AddRow, Target_Cells)), 1) = "L" Then
            CountL = CountL + 1
            MsgBox "A projectleader do already exist for this project."
        End If
    Next Target_Cells
End Sub
 
Upvote 0
That was actually a very easy change.
It works like a charm and you are definetly my VBA hero.
Thank you very much!
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,739
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top