I am trying to make and automated report searching cells for text and converting them into values. so one cell could have car, van, car, truck next cell could have just car.
Sub try_to_find_text ()
Dim ALCell As Range
Dim car As Integer
Dim van As Integer
Dim truck As Integer
Dim digger As Integer
For Each ALCell In ActiveSheet.Range("E21:E1000")
Select Case ALCell.Value
Case Is <> ""
Dim Search1, Where1
Search1 = "car"
Where1 = InStr(ActiveCell.Text, Search1)
If Where1 Then
car = car + 1
End If
Case Is = "van"
van = van + 1
Case Is = "truck"
truck = truck + 1
Case Is = "digger"
HCAS = HCAS + 1
Case Is = ""
End Select
Next ALCell
ActiveSheet.Range("B13").Value = car
ActiveSheet.Range("C13").Value = van
ActiveSheet.Range("D13").Value = truck
ActiveSheet.Range("E13").Value = digger
End Sub
The above will find the cells that have the specific value in the "" but if there is more e.g. car, van this will be missed in the count. at the top I am trying to create a loop for the active cell text but this doesn't work any help would be helpful. Thanks
The trick is to have an inner loop which iterates over an array obtained from the worksheet cell.
Option Explicit
Sub try_to_find_text()
Dim ALCell As Range
Dim car As Integer
Dim van As Integer
Dim truck As Integer
Dim digger As Integer
For Each ALCell In ActiveSheet.Range("E21:E1000")
Dim myVehicle As Variant
Dim myVehicles As Variant
myVehicles = TriageToArray(ALCell.Value,",")
For Each myVehicle In myVehicles
Select Case myVehicle
Case Is = "car"
car = car + 1
Case Is = "van"
van = van + 1
Case Is = "truck"
truck = truck + 1
Case Is = "digger"
HCAS = HCAS + 1
Case Is = ""
End Select
Next
Next ALCell
ActiveSheet.Range("B13").Value = car
ActiveSheet.Range("C13").Value = van
ActiveSheet.Range("D13").Value = truck
ActiveSheet.Range("E13").Value = digger
End Sub
Public Function TriageToArray(ByVal ipString As String, Optional ByVal ipSeparator As String = " ") As Variant
Dim myArray As Variant
myArray = Split(ipString, ipSeparator)
Dim myIndex As Long
For myIndex = LBound(myArray) To UBound(myArray)
myArray(myIndex) = LCase$(Trim$(myArray(myIndex)))
Next
TriageToArray = myArray
End Function
Amazing thanks, just had to remove the car bit and add like the van and it worked a charm. thank you so much