简体   繁体   English

计算具有特定文本的行数

[英]Count number of rows with specific text

I have an assignment to count the number of rows which contain specific text and print the value on email.我有一项任务是计算包含特定文本的行数并在电子邮件上打印该值。

Public Function First()
    Dim Source As Workbook
    Dim Var1 As Integer
    Dim Var10 As Integer
    Dim Source2 As Workbook

    Set Source = Workbooks.Open("C:\Users\HP\Desktop\IN INPROG.xlsx")

    Var1 = Application.WorksheetFunction.CountIf(Range("M1:M100"), "Orange")

    Source.Close SaveChanges:=False

    Set Source2 = Workbooks.Open("C:\Users\HP\Desktop\SR INPROG.xlsx")

    Var10 = Application.WorksheetFunction.CountIf(Range("M1:M100"), "Orange")

    Source2.Close SaveChanges:=False

    eTo = "orange@aod.au"
    esubject = Format(Date, "d/mmmm/yyyy") & " " & "Weekly Open Incident Reminder"
    ebody = "Dear All," & vbCrLf & "" & vbCrLf & "" & vbCrLf & "Orange: " & "SR: " & Var10 & " IN: " & Var1 

    Set app = CreateObject("Outlook.Application")
    Set itm = app.createitem(0)
    On Error Resume Next
    With itm
        .Subject = esubject
        .To = eTo
        .body = ebody
        .display

My code will return all the values as "0" even though "Orange" does exist in one of the rows.即使“橙色”确实存在于其中一行中,我的代码也会将所有值返回为“0”。

If you need to count any row that contains the word within a string you could modify the string in the countif function from "Orange" to "*Orange*" .如果您需要计算字符串中包含该单词的任何行,您可以将 countif 函数中的字符串从 "Orange" 修改为"*Orange*"

The * wildcard represents any number of characters. * 通配符代表任意数量的字符。 Eg a cell with the string "Three orange balloons" will not be counted by例如,带有字符串“三个橙色气球”的单元格将不会被计算在内

CountIf(Range("M1:M100"), "Orange") but will be by CountIf(Range("M1:M100"), "*Orange*") . CountIf(Range("M1:M100"), "Orange")但将是CountIf(Range("M1:M100"), "*Orange*")

Thanks for your solution, I have found an answer for my question and I will post it here感谢您的解决方案,我找到了我的问题的答案,我会在这里发布

Set Source = Workbooks.Open("C:\Users\itsm-student\Downloads\IN INPROG.xlsx")
Set Wks = Source.Worksheets("IN INPROG")

Var1 = Application.WorksheetFunction.CountIf(Wks.Range("M1:M100"), "*Orange*")

What I need to do is basically set a variable for the worksheet that you gonna refer to and in my case it will be "IN PROG" and call the variable while declaring the range.我需要做的基本上是为您要引用的工作表设置一个变量,在我的情况下,它将是“IN PROG”并在声明范围时调用该变量。 And the codes that i posted on top are right however instead of looking the text "orange" at the source excel sheet, it find the text value on the excel file which I do my VBA on.我在顶部发布的代码是正确的,但是它不是在源 excel 表上查看文本“橙色”,而是在我执行 VBA 的 excel 文件上找到文本值。 Hope it helps希望能帮助到你

I did:我做了:

Sub Worksheet_Change(ByVal Target As Range)

Set Source = ThisWorkbook

Dim Var1 As Integer

Set Wks = Source.Worksheets("Recebimento")

Var1 = Application.WorksheetFunction.CountIf(Wks.Range("U:U"), "*NOK*")

If Var1 > 0 Then
 'your code

End sub

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

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