0

我想要代码检查一列数据的条件,即:范围限定。如果他们需要进入范围,则值将是“REQ”,如果不是,则值将是“E”、“S”、“M”和“NR”。我使用 [select case] 来检查条件。在选择案例开始时,我收到此错误。

我不确定我是否做出了销售参考权。用另一列中的名称填充数组后,我会检查并从数组中删除空元素,然后在 msgbox 中显示数组的所有元素。下面是我使用的代码:

'Declares total number of personnel as integer
    Dim total As Integer
    total = Worksheets("MASTER").Range("C4").Value

'Declares single element array with personnel full names
ReDim names(total) As String
'Loops through the array checking to see if personnel have qualified on the Rifle Range
For i = (1 + 6) To (total + 6)
    Select Case Worksheets("MASTER").Range(Cells(i, 23)).Text
        Case "REQ"
            names(i - 6) = Worksheets("MASTER").Range(Cells(i, 7)).Value
        Case "NR"
            names(i - 6) = vbNullString
        Case "E"
            names(i - 6) = vbNullString
        Case "S"
            names(i - 6) = vbNullString
        Case "M"
            names(i - 6) = vbNullString
        End Select
Next
'Declares a new array to remove blank elements from the orignal array
ReDim msgnames(LBound(names) To UBound(names))
'Loops through new array removing empty elements
For i = LBound(names) To UBound(names)
    If names(i) <> vbNullString Then
        x = x + 1
        msgnames(x) = names(i)
    End If
Next
'Displays every element of the array
For i = LBound(msgnames) To UBound(msgnames)
    msg = msg & msgnames(i) & vbNewLine
Next
'Declares COMP, NOTCOMP, REQ and NOTREQ variables
Dim COMP As String
Dim NOTCOMP As String
Dim REQ As String
Dim NOTREQ As String
'Adds a comment to the bottom of the Message Box
MsgBox msg, vbOKOnly, "Rifle Range"`
4

1 回答 1

2

您的范围有错误的语法。

改变这个:

Select Case Worksheets("MASTER").Range(Cells(i, 23)).Text

对此:

Select Case Worksheets("MASTER").Cells(i, 23).Value2

另一件事——你应该使用.valueor.value2代替,.text除非你有一个非常具体的使用理由.text。有关三个属性的出色分析,请参阅 Charles Williams 的文章:TEXT vs VALUE vs VALUE2 - Slow TEXT 以及如何避免它

于 2013-08-17T19:21:34.530 回答