Excel中的VBA错误:属性的无效使用
我已经在CommandButton_Click()
编写了下面的代码,但是它给出了以下错误:
编译错误:属性的无效使用。
Public Sub SectorNAme() Dim score As String, result As Integer Dim a_counter As Integer For a_counter = 1 To 473 score = Range("AB" + a_counter).Value If score = "Technology & FM" Then result = "25" ElseIf score = "Operating Equipment & Supplies" Then result = "26" ElseIf score = "Interiors & Design" Then result = "27" ElseIf score = "Outdoor & Resort Experience" Then result = "28" ElseIf score = "HORECA" Then result = "29" ElseIf score = "Retail & Franchise" Then result = "30" ElseIf score = "Design" Then result = "33" ElseIf score = "Health & Fitness Eqpts, P&S" Then result = "32" ElseIf score = "Recreational Fun & Adventure" Then result = "31" Else result = "35" Range("AC" + a_counter).Value End If Next a_counter End Sub
请帮我找一个解决scheme。
你有一些小问题:
-
你得到的错误是:
Range("AC" + a_counter).Value
我假设你打算这样做:
Range("AC" + a_counter).Value = result
-
这个:
Range("AB" + a_counter)
和这个:Range("AC" + a_counter)
应该使用&
而不是+
连接string,否则你会得到一个types不匹配,即:Range("AB" & a_counter)
和Range("AC" & a_counter)
。 -
我刚刚编辑了你的问题,并且用更好的缩进,我也注意到你的
End If
是在Range("AC" + a_counter).Value
,这是错误的。 它应该在这之前 ,否则你的结果将只在Else
情况下分配,而不是任何其他。
所以,你的最终代码应该是这样的:
Public Sub SectorName() Dim score As String, result As Integer Dim a_counter As Integer For a_counter = 1 To 473 score = Range("AB" & a_counter).Value If score = "Technology & FM" Then result = "25" ElseIf score = "Operating Equipment & Supplies" Then result = "26" ElseIf score = "Interiors & Design" Then result = "27" ElseIf score = "Outdoor & Resort Experience" Then result = "28" ElseIf score = "HORECA" Then result = "29" ElseIf score = "Retail & Franchise" Then result = "30" ElseIf score = "Design" Then result = "33" ElseIf score = "Health & Fitness Eqpts, P&S" Then result = "32" ElseIf score = "Recreational Fun & Adventure" Then result = "31" Else result = "35" End If Range("AC" & a_counter).Value = result Next a_counter End Sub
这现在应该如你所愿。