应用程序或对象定义的错误 – VBA Excel 2013

我想要代码检查一个条件的数据列,即:范围资格。 如果他们被要求去的范围的价值将是“REQ”,如果他们的价值将是“E”,“S”,“M”和“NR”。 我使用[select case]来检查条件。 在select案例的开始,我得到这个错误。

我不知道我是否在做销售参考。 数组填充另一列的名称之后,我接着从数组中移除空元素,然后在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"` 

你的范围有错误的语法。

改变这个:

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

对此:

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

另一件事 – 你应该使用.value.value2而不是.text除非你有一个非常具体的使用.text理由。 查看Charles Williams的文章,对三个属性进行了很好的分析: TEXT vs VALUE和VALUE2 – Slow TEXT以及如何避免它 。