Excel VBA - 选择案例 - 多个测试表达式

时间:2014-11-17 14:24:58

标签: excel vba excel-vba select case

我正在尝试编写一个Select Case语句,该语句查看整个A列中的一系列值,然后预先形成语句。

到目前为止,我可以将我的选择案例语句用于第A2行,但我无法使用一系列值而不会给出类型不匹配错误'。

工作

Select Case range("A2").Value

Case "01"
    range("G2").Value = "Admin"
    range("I2").Value = "None"
    range("J2").Value = "None"
    range("K2").Value = "None"
    range("H2").Value = "No Affiliation"
    range("T2").Value = "99999"

不工作( 类型不匹配错误

Select Case Range ("A2:A1000").Value

Case "01"
    range("G2").Value = "Admin"
    range("I2").Value = "None"
    range("J2").Value = "None"
    range("K2").Value = "None"
    range("H2").Value = "No Affiliation"
    range("T2").Value = "99999"

Case "A"
    range("G2").Value = "Dealer"
    range("I2").Value = "None"
    range("J2").Value = "None"
    range("K2").Value = "None"

1 个答案:

答案 0 :(得分:2)

您需要逐步浏览范围中的每个单元格,“A2:A1000”将返回一个数组,而不是值。

Sub SO()

Dim rNum As Long

For rNum = 2 To 1000

   Select Case Range("A" & rNum).Value
      Case "01"
         Range("G" & rNum).Value = "Admin"
         Range("I" & rNum).Value = "None"
         Range("J" & rNum).Value = "None"
         Range("K" & rNum).Value = "None"
         Range("H" & rNum).Value = "No Affiliation"
         Range("T" & rNum).Value = "99999"

      Case "A"
         Range("G" & rNum).Value = "Dealer"
         Range("I" & rNum).Value = "None"
         Range("J" & rNum).Value = "None"
         Range("K" & rNum).Value = "None"

      Case Else
         '// Do something else...
   End Select

Next rNum

End Sub