【发布时间】:2020-01-16 14:04:56
【问题描述】:
我有以下代码。我将“d”列中的单元格值作为字符串(一周中的几天的列表,可以是一天或所有 7 天)传递给这个函数。我还使用该函数来填充整数数组。我在指示的行出现类型不匹配,我不知道为什么。特别是因为我在填充它并逐步通过它之前对数组进行了调暗。我将不胜感激任何帮助/解释。显式选项已打开。
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
在常规模块中我使用这样的函数...
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
【问题讨论】: