我使用select case语句和一个函数一起在VBA中创建fall,但我似乎不知道应该如何构造它。下面是我要做的事情:
问题是:我需要10+9+8+7+6+5+4+3+2嵌套select语句来进行第一次检查。
下面是一个psuedocode示例:
开始第一个循环(第一个范围: Excel工作表行2-12行;10行):
行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发布于 2018-03-20 18:10:24
我重写了您的selectcasefallthrough,使其更加灵活,并添加了一些额外的评论。因为我不确定您的psuedocode的哪个部分正在给您带来麻烦,所以我没有添加任何额外的功能。但是,我可以编辑它们,一旦我知道您当前要执行的是哪个psuedocode步骤。
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 Functionhttps://stackoverflow.com/questions/48305438
复制相似问题