简体   繁体   中英

Excel VBA - count an item if the first 3 character of a string matches the keyword

I need to count an item if the first 3 character of a string if the first 3 characters are "APP" (for example). As shown below, I am able to count the item if it has the "APP", but what I want is only counting if the item has "APP" as the 1st 3 characters of the string. I need a code like if InStr(items, "APP") And take the 1st 3 character Then count_of_string = count_of_string + 1

I don't know how to write that in vba. I have seen something like str2 = Left(str1,3) but it's giving me 0 as results.

在此处输入图片说明

Here is my current code. Thank you.

Public Sub TEST()
Dim a As String
Dim row_number As Long
Dim count_of_string As Long
Dim items As Variant


row_number = 0
count_of_string = 0
Do

row_number = row_number + 1 
items = Sheets("Sheet1").Range("A" & row_number)
    If InStr(items, "APP") Then
        count_of_string = count_of_string + 1
    End If
Loop Until items = ""

Range("A1").Select

Selection.End(xlDown).Select
lastcell = ActiveCell.Address

ActiveCell.Offset(3, 0).Value = "APP  " & count_of_string


End Sub

I'd recommend you use the Left function which can be used like this.

Left(yourString, length)

This can be used to get the 3 first characters of the text in your case and then you'll be able to test these characters.

In your code, it'd do something like that:

Public Sub TEST()

    Dim a As String
    Dim row_number As Long
    Dim count_of_string As Long
    Dim items As Variant

    row_number = 0
    count_of_string = 0

    Do
        row_number = row_number + 1
        items = Sheets("Sheet1").Range("A" & row_number)

        'Check if the 3 first characters are APP
        If (Left(items, 3) = "APP") Then
            count_of_string = count_of_string + 1
        End If
    Loop Until items = ""

    Range("A1").Select

    Selection.End(xlDown).Select
    lastcell = ActiveCell.Address

    ActiveCell.Offset(3, 0).Value = "APP  " & count_of_string
End Sub

If the case of APP (ie not app) is important then you can use FIND , otherwise the COUNTIF that @YowE3K suggests in his comment (should add it as an answer).
Saying that, someone's going to show how to do it with a formula that is case sensitive in a minute.

Public Sub Test()
    Dim count_of_string As Long
    Dim rItem As Range
    Dim sFirstAdd As String

    With ThisWorkbook.Worksheets("Sheet1").Columns(1)

        'Find the first occurrence in column A.
        Set rItem = .Find(What:="APP*", _
                          LookIn:=xlValues, _
                          LookAt:=xlWhole, _
                          SearchDirection:=xlNext, _
                          MatchCase:=True)
        If Not rItem Is Nothing Then
            sFirstAdd = rItem.Address
            'Find the following occurrences until the code reaches the first instance again.
            Do
                count_of_string = count_of_string + 1
                Set rItem = .FindNext(rItem)
            Loop While rItem.Address <> sFirstAdd
        End If

        .Cells(.Rows.Count).End(xlUp).Offset(3) = "APP " & count_of_string

    End With

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM