0

我有一项任务是计算包含特定文本的行数并在电子邮件上打印该值。

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

即使“橙色”确实存在于其中一行中,我的代码也会将所有值返回为“0”。

4

3 回答 3

0

如果您需要计算字符串中包含该单词的任何行,您可以将 countif 函数中的字符串从 "Orange" 修改为"*Orange*".

* 通配符代表任意数量的字符。例如,带有字符串“三个橙色气球”的单元格将不会被计算在内

CountIf(Range("M1:M100"), "Orange")但会通过CountIf(Range("M1:M100"), "*Orange*")

于 2013-08-18T23:42:18.407 回答
0

感谢您的解决方案,我找到了我的问题的答案,我会在这里发布

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*")

我需要做的基本上是为您要引用的工作表设置一个变量,在我的情况下,它将是“IN PROG”并在声明范围时调用该变量。我在顶部发布的代码是正确的,但是它不是在源 excel 表上查看文本“橙色”,而是在我执行 VBA 的 excel 文件上找到文本值。希望能帮助到你

于 2013-08-30T16:40:24.193 回答
0

我做了:

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
于 2015-09-08T21:06:36.190 回答