应用程序或对象定义错误-VBA Excel 2013
我希望代码检查一列数据的条件,即:范围限定。如果要求它们进入该范围,则该值将为REQ,如果它们不是,则该值将为E、S、M和NR。我使用[select case]检查该条件。在select案例开始时,我得到了这个错误 我不确定我是否做了正确的销售参考。用另一列中的名称填充数组后,我将遍历并从数组中删除空元素,然后在msgbox中显示数组的所有元素。下面是我使用的代码:应用程序或对象定义错误-VBA Excel 2013,vba,excel,Vba,Excel,我希望代码检查一列数据的条件,即:范围限定。如果要求它们进入该范围,则该值将为REQ,如果它们不是,则该值将为E、S、M和NR。我使用[select case]检查该条件。在select案例开始时,我得到了这个错误 我不确定我是否做了正确的销售参考。用另一列中的名称填充数组后,我将遍历并从数组中删除空元素,然后在msgbox中显示数组的所有元素。下面是我使用的代码: 'Declares total number of personnel as integer Dim total As I
'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
还有一件事-除非您有非常具体的理由使用.text,否则应该使用.value或.value2而不是.text。请参阅Charles Williams的文章,以获得对这三个属性的出色分析:.在两行中都更改为.Value2,但仍然得到相同的错误。当我单步执行时,它会在选择案例的第一行停止,并且不会执行任何案例。更改为.Value2是可选的。您必须更改您的Case语句,以便为您正在使用的范围使用正确的语法。为了清楚起见,请确保您删除了Cellsi周围的.range,23。请在您的网站上向您发送电子邮件,告知代码的最终产品是什么。