在VBA中选择Case fall through?

时间:2018-01-17 16:11:02

标签: excel vba excel-vba

我正在使用一个select case语句与一个函数一起在VBA中创建,但我似乎无法弄清楚它应该如何构建。以下是我正在寻找的事情:

  1. 检查行值是否为1& 10具有相同的唯一ID(如果不是:1& 9; 1& 8,1& 7,...,1& 2)

    1a上。如果行值为1& 10具有相同的唯一ID,然后中间的所有行也可以

    2a上。尽管所有行都具有相同的唯一ID,但并不意味着我之后要检查的单元格都是相同的 - 例如:行1,3,4,7,8,10可能有空白单元格,而行2,5,9不是空白。为了确定这一点,它必须经过从10到1的每一行来确定这一点。一行不影响另一行; 1-10中的任何/全部/没有行可以有空白单元格,尽管它们都具有相同的唯一ID。

  2. 检查第一个和最后一个

  3. 之间所有行的空白单元格
  4. 使用空白单元格连接所有行的唯一ID。

  5. 问题是:我将需要10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2嵌套选择语句进行第一次检查。

    这是一个伪代码示例:

    开始第一个循环(第一个范围:Excel工作表行2-12; 10行):

    第1行和第1行10具有相同的唯一ID

    检查第10行是否有空白单元格

    第10行没有空白单元格

    检查第9行是否有空白单元格

    找到空白细胞 - >将信息添加到字符串

    检查第8行是否有空白单元格

    找到空白细胞 - >将信息添加到字符串

    检查第7行是否有空白单元格

    找不到空白单元格

    检查第6行是否有空白单元格

    找到空白细胞 - >将信息添加到字符串

    检查第5行是否有空白单元格

    ...

    找不到空白单元格

    开始第二个循环(第二个范围:Excel工作表行12-14; 3行):

    单元格12和14匹配

    检查第14行是否有空白单元格

    找到空白细胞 - >将信息添加到单独的字符串

    检查第13行是否有空白单元格

    找不到空白单元格

    检查第12行是否有空白单元格

    找不到空白单元格

    我目前的代码如下:

    Sub selectcasetryagain()
    Dim c As Range
    Dim r As Range
    Dim lastRow As Long
    Dim lastCol As Long
    
    lastRow = Range("A:A").End(xlDown).Row
    lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
    
    Set r = ActiveSheet.Range(Cells(1, 1), Cells(lastRow, lastCol))
    
    For i = 2 To lastRow
        Set c = r.Cells(i, 6)
        Select Case c.Value
            Case SelectCaseFallThru(c)
    
        End Select
    Next i
    
    End Sub
    
    Option Explicit
    Public c, r As Range
    Public i As Integer
    Public lastRow, lastCol As Long
    Public RMissing As Variant
    
    Function SelectCaseFallThru(Optional c As Variant, Optional d As Variant)
        If c.Value = c.Offset(10, 0).Value Then
            Debug.Print c.Value & " - " & c.Offset(10, 0).Value
            If IsEmpty(c.Offset(0, 46)).Value And IsEmpty(c.Offset(0, 47)).Value Then
                RMissing = c.Offset(0, 42).Value
            i = i + 10
        ElseIf c.Value = c.Offset(9, 0).Value Then
            Debug.Print c.Value & " - " & c.Offset(9, 0).Value
            i = i + 9
        ElseIf c.Value = c.Offset(8, 0).Value Then
            Debug.Print c.Value & " - " & c.Offset(8, 0).Value
            i = i + 8
        ElseIf c.Value = c.Offset(7, 0).Value Then
            Debug.Print c.Value & " - " & c.Offset(7, 0).Value
            i = i + 7
        ElseIf c.Value = c.Offset(6, 0).Value Then
            Debug.Print c.Value & " - " & c.Offset(6, 0).Value
            i = i + 6
        ElseIf c.Value = c.Offset(5, 0).Value Then
            Debug.Print c.Value & " - " & c.Offset(5, 0).Value
            i = i + 5
        ElseIf c.Value = c.Offset(4, 0).Value Then
            Debug.Print c.Value & " - " & c.Offset(4, 0).Value
            i = i + 4
        ElseIf c.Value = c.Offset(3, 0).Value Then
            Debug.Print c.Value & " - " & c.Offset(3, 0).Value
            i = i + 3
        ElseIf c.Value = c.Offset(2, 0).Value Then
            Debug.Print c.Value & " - " & c.Offset(2, 0).Value
            i = i + 2
        ElseIf c.Value = c.Offset(1, 0).Value Then
            Debug.Print c.Value & " - " & c.Offset(1, 0).Value
            i = i + 1
        Else
            Exit Function
        End If
    
    
    End Function
    

1 个答案:

答案 0 :(得分:0)

我重写了您的selectcasefallthrough以使其更灵活,并添加了一些其他评论。由于我不确定你的伪代码的哪个部分目前给你带来麻烦,我没有添加任何额外的功能。但是,我可以编辑它们,一旦我知道您目前停留了哪个伪代码步骤。

Sub selectcasetryagain()
Dim c As Range 'Will no longer access Global c
Dim r As Range
Dim lastRow As Long
Dim lastCol As Long

lastRow = Range("A:A").End(xlDown).Row
lastCol = Cells(1, Columns.Count).End(xlToLeft).Column

Set r = ActiveSheet.Range(Cells(1, 1), Cells(lastRow, lastCol))

For i = 2 To lastRow
    Set c = r.Cells(i, 6)
    Select Case c.Value
        Case SelectCaseFallThru(10, c) ' Added in row number to make it variable

    End Select
Next i

End Sub

Option Explicit
Public c, r As Range 'Note: currently Global c is defined as a Variant
Public i As Integer
Public lastRow, lastCol As Long
Public RMissing As Variant

'This function does not currently return anything.
Function SelectCaseFallThru(maxRowNum as Integer, Optional c As Variant, Optional d As Variant) 'This Function will use the Optional c, not the Global c
    Dim counter as Integer
    Dim found as Boolean

    For counter = maxRowNum to 1 step -1
        If c.Value = c.Offset(counter, 0).Value Then
            Debug.Print c.Value & " - " & c.Offset(counter, 0).Value
            If IsEmpty(c.Offset(0, 46)).Value And IsEmpty(c.Offset(0, 47)).Value and counter = maxRowNum Then 'added check for counter
                RMissing = c.Offset(0, 42).Value
            End if
            i = i + counter
            found = True
            Exit For
        End If
    Next counter

    If not found Then Exit Function

End Function