I have a VBA script I'm trying to run that reads incoming emails to an account and then marks the corresponding cell in a spreadsheet. My test run had 9 jobs it was looking for in a Nested IF Statement.
Like this:
If InStr(itm.subject, "Test Backup") > 0 Then
J = 2
ElseIf InStr(itm.subject, "TESTdchq") > 0 Then
J = 3
ElseIf InStr(itm.subject, "TESTdynamics") > 0 Then
J = 4
ElseIf InStr(itm.subject, "TEST-VSS-HQ") > 0 Then
J = 5
ElseIf InStr(itm.subject, "TESTWSUS01") > 0 Then
J = 6
ElseIf InStr(itm.subject, "TEST-Camera") > 0 Then
J = 7
ElseIf InStr(itm.subject, "TEST-Vcenter") > 0 Then
J = 8
ElseIf InStr(itm.subject, "TEST-View Connection") > 0 Then
J = 9
ElseIf InStr(itm.subject, "TESTktsrv1") > 0 Then
J = 10
End If
However one of my practical applications has 64 jobs. I need a more efficient way to assign the value to J based on the keyword in the subject of the email. Id assume I could do something with an array and then call the array and compare to the subject.
Here is the whole test script if that helps.
Sub ReconcileTest(itm As Outlook.MailItem)
Dim xlApp As Excel.Application
Dim ExcelWkBk As Excel.Workbook
Dim FileName As String
Dim PathName As String
Dim J As Integer
'J = will be used to declare the proper Job row
PathName = "C:\Users\Owner\Dropbox\Backups\"
FileName = "TESTReconcileSheet.xlsx"
'Declare J
If InStr(itm.subject, "Test Backup") > 0 Then
J = 2
ElseIf InStr(itm.subject, "TESTdchq") > 0 Then
J = 3
ElseIf InStr(itm.subject, "TESTdynamics") > 0 Then
J = 4
ElseIf InStr(itm.subject, "TEST-VSS-HQ") > 0 Then
J = 5
ElseIf InStr(itm.subject, "TESTWSUS01") > 0 Then
J = 6
ElseIf InStr(itm.subject, "TEST-Camera") > 0 Then
J = 7
ElseIf InStr(itm.subject, "TEST-Vcenter") > 0 Then
J = 8
ElseIf InStr(itm.subject, "TEST-View Connection") > 0 Then
J = 9
ElseIf InStr(itm.subject, "TESTktsrv1") > 0 Then
J = 10
End If
Set xlApp = Application.CreateObject("Excel.Application")
With xlApp
.Visible = True ' Visible is used for debugging
Set ExcelWkBk = xlApp.Workbooks.Open(PathName & FileName)
With ExcelWkBk
'VBA code to update workbook here
Dim todaysDate As Date
Dim D As Integer
Dim subject As String
'D = will be used to declare the proper Date column
todaysDate = Day(Now)
D = todaysDate
'Marksheet
If InStr(itm.subject, "[Success]") > 0 Then
.Sheets("sheet1").Cells(J, (D + 2)).Value = "S"
.Sheets("Sheet1").Cells(J, (D + 2)).Interior.ColorIndex = 43
ElseIf InStr(itm.subject, "[Failed]") > 0 Then
.Sheets("sheet1").Cells(J, (D + 2)).Value = "F"
.Sheets("Sheet1").Cells(J, (D + 2)).Interior.ColorIndex = 3
ElseIf InStr(itm.subject, "[Warning]") > 0 Then
.Sheets("sheet1").Cells(J, (D + 2)).Value = "W"
.Sheets("Sheet1").Cells(J, (D + 2)).Interior.ColorIndex = 27
End If
.Save
.Close
End With
.Quit
End With
End Sub