简体   繁体   English

Excel VBA-如果字符串的前3个字符与关键字匹配,则对项目进行计数

[英]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). 如果字符串的前3个字符是“ APP”(例如),则需要计算一个项目的字符串。 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. 如下所示,我能够对具有“ APP”的项目进行计数,但是我只想计数是否具有“ APP”作为字符串的前三个字符。 I need a code like if InStr(items, "APP") And take the 1st 3 character Then count_of_string = count_of_string + 1 我需要类似if InStr(items, "APP") And take the 1st 3 character Then count_of_string = count_of_string + 1的代码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. 我不知道如何在vba中编写该代码。 I have seen something like str2 = Left(str1,3) but it's giving me 0 as results. 我见过类似str2 = Left(str1,3)但结果为0。

在此处输入图片说明

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函数。

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. 这可以用来获取您案中文本的前3个字符,然后就可以测试这些字符。

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). 如果APP(即非app)的情况很重要,则可以使用FIND ,否则@ YowE3K在其注释中建议的COUNTIF (应将其添加为答案)。
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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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