Case Else循环回Select Case

时间:2017-06-30 15:00:21

标签: excel vba

我有一个宏,它使用Select Case根据单元格的值选择一个电子邮件模板。

然而,在某些情况下,正在检查的单元格为空白。是否有可能我可以更改正在检查的单元格,例如Case Else中的(row + 1)然后循环回Select Case的开头,例如:

CellToCheck = .Cell(1, 1)

Select Case CellToCheck

Case = 1
Email = "blah blah 1"

Case = 2
Email = "blah blah 2"

Case = 3
Email = "blah blah 3"

Case Else
CellToCheck = Cell(2,1)
'Loop back to "Select Case CellToCheck

我考虑将其包含在Do While循环中,但如果没有调用Case Else,我也不知道如何退出循环。

思想?

2 个答案:

答案 0 :(得分:0)

它只是你检查的2个细胞吗?如果是这样,请在开头尝试条件。

Function EmailCheck()

    Dim Email As String
    Dim CellToCheck As Integer

    CellToCheck = Cells(1, 1).Value
    If Cells(1, 1).Value = "" Then CellToCheck = Cells(2, 1).Value

    Select Case CellToCheck

    Case Is = 1
    Email = "blah blah 1"

    Case Is = 2
    Email = "blah blah 2"

    Case Is = 3
    Email = "blah blah 3"

    Case Else
    MsgBox "Check Range Values"
    Exit Function

    End Select

    MsgBox Email

End Function

答案 1 :(得分:-1)

这样的事情应该有效:

   CellToCheck = .Cell(1, 1)
   email = ""

   Do While email = ""
      Select Case CellToCheck
         Case Is = 1
            email = "blah blah 1"

         Case Is = 2
            email = "blah blah 2"

         Case Is = 3
            email = "blah blah 3"

         Case Else
            CellToCheck = Cell(2, 1)
      End Select
   Loop