Sub forBuildingCategoryFromShortDescriptions()
Dim mainLastrow As Long, catLastrow As Long, catWords As Range, i As Long, j As Long
Sheets("Category").Select 'I placed the categories on a 2nd sheet. Select the Category sheet'
With ActiveSheet 'Find the number of rows on the Category Sheet.
catLastrow = .UsedRange.Rows.Count + .UsedRange.Row - 1
End With
Set catWords = Range("A1:F" & catLastrow) 'Creates a range for the categories.
Sheets("MainSheet").Select 'Select the MainSheet
With ActiveSheet
mainLastrow = .UsedRange.Rows.Count + .UsedRange.Row - 1
End With
For i = 2 To mainLastrow 'Loop through all the rows on the MainSheet sheet
For j = 2 To catLastrow 'Loop through all the rows of the Category sheet
'The below will set the case of the data to lowercase and check to see if column C(Short Description)
'contains the keywords from Category Sheet. If the keyword exist Column E is assigned the right value.
'If no keyword is found, it leaves it blank. You can always add, remove, or reassign keywords on the Category sheet.
If catWords.Columns(1).Rows(j).Value = "" Then
GoTo part2
ElseIf InStr(1, (LCase$(Range("C" & i).Value)), LCase$(catWords.Columns(1).Rows(j).Value)) > 0 And Range("E" & i).Value = "" Then
Range("E" & i).Value = " Other"
End If
part2: 'Since the columns lengths on the Category Sheet vary, I used goto to exit the if statement when blanks appears in the columns
If catWords.Columns(2).Rows(j).Value = "" Then
GoTo part3
ElseIf InStr(1, (LCase$(Range("C" & i).Value)), LCase$(catWords.Columns(2).Rows(j).Value)) > 0 And Range("E" & i).Value = "" Then
Range("E" & i).Value = " CPU"
End If
part3:
If catWords.Columns(3).Rows(j).Value = "" Then
GoTo part4
ElseIf InStr(1, (LCase$(Range("C" & i).Value)), LCase$(catWords.Columns(3).Rows(j).Value)) > 0 And Range("E" & i).Value = "" Then
Range("E" & i).Value = " Memory"
End If
part4:
If catWords.Columns(4).Rows(j).Value = "" Then
GoTo part5
ElseIf InStr(1, (LCase$(Range("C" & i).Value)), LCase$(catWords.Columns(4).Rows(j).Value)) > 0 And Range("E" & i).Value = "" Then
Range("E" & i).Value = " Disk"
End If
part5:
If catWords.Columns(5).Rows(j).Value = "" Then
GoTo part6
ElseIf InStr(1, (LCase$(Range("C" & i).Value)), LCase$(catWords.Columns(5).Rows(j).Value)) > 0 And Range("E" & i).Value = "" Then
Range("E" & i).Value = " Database"
End If
part6:
If catWords.Columns(6).Rows(j).Value = "" Then
GoTo part7
ElseIf InStr(1, (LCase$(Range("C" & i).Value)), LCase$(catWords.Columns(6).Rows(j).Value)) > 0 And Range("E" & i).Value = "" Then
Range("E" & i).Value = " Storage"
End If
part7:
Next j
Next i
For i = 2 To mainLastrow 'Fills in blank E column cells with N/A
If Range("E" & i).Value = "" Then
Range("E" & i).Value = "N/A"
End If
Range("D" & i).Value = Format(Range("B" & i), "mmmm") 'Looks at Date and pulls the month
Next i
Columns("E:D").AutoFit
End Sub