I have the following code. I'm passing in a cell's value from column "d" as a string (a list of days of the week, could be one day or as many as all 7 days) into this function. I'm also using the fuction to fill an array of Integers. I'm getting a type mismatch at the indicated line and I can't figure out why. Especially since I Dim the array right before filling it and stepping through it. I'd appreciate any help/explaination. Option Explicit is turned on.
Public i as Integer
Public Function ParseColumnD(cellvalue as String) as String
Dim BREdaysString() as String
Dim daysAsInt() As Integer
BREdaysString() = Split(cellvalue, ", ")
For Each i in BREdaysString()
If BREdaysString(i) = "Monday" Then '<-----Type Mismatch error here.
daysAsInt(i) = 4
ElseIf BREdaysString(i) = "Tuesday" Then
daysAsInt(i) = 5
ElseIf BREdaysString(i) = "Wednesday" Then
daysAsInt(i) = 6
ElseIf BREdaysString(i) = "Thursday" Then
daysAsInt(i) = 7
ElseIf BREdaysString(i) = "Friday" Then
daysAsInt(i) = 8
ElseIf BREdaysString(i) = "Saturday" Then
daysAsInt(i) = 9
ElseIf BREdaysString(i) = "Sunday" Then
daysAsInt(i) = 10
End If
Next
'to check to make sure the conversion from days of the week to integers is correct
'I take the int array and put it as the function's string, return it and put in another cell.
For Each i in daysAsInt()
If i = 1 Then
ParseColumnD = daysAsInt(i)
ElseIf i > 1 Then
ParseColumnD = ParseColumnD & ", " & daysAsInt(i)
End If
Next
End Funciton
In the regular module I use the function like this...
Sub MySub()
Dim BREitems as Range
Dim lastrow as Long
lastrow = Cells(Rows.count, "a").End(xlUp).Row
Set BREitems = Range("a5:a" & lastrow)
For Each i in BREitems
Cells(i.Row, "g").value = ParseColumnD(Cells(i.row, "d"))
Next
End Sub