繁体   English   中英

VBA For循环按行和列

[英]VBA For loop by rows and columns

我有一个有点复杂的请求(对你们中的一些人来说可能很简单)。

我有这个小数据集,其中顶行代表Userform Combobox 在此处输入图像描述

我想遍历每一行以检查当前在用户表单中选择的内容与此数据集中的内容之间是否匹配(以查看是否已经选择了这组选择)。

如果它被选中,那么我想要一个MsgBox & Exit sub ,如果没有,那么我希望它继续循环直到它到达一个空单元格。

这就是我尝试的:

Sub check_procedures()
  Dim ws As Worksheet, rngF As Range
  
  Set ws = Worksheets("do not open!")
  Set rngF = ws.Range("F2")
   
   Do Until rngF.Value = ""
         If UI.ComboBoxSource = rngF.Value Then
         MsgBox "This procedure already exhists. Please click on Update Summary. "
         Exit Sub
         
         
         Else
         End If
            
            
         Set rngF = rngF.Offset(1)
         
         
    
    Loop
    
End Sub

它有效但仅适用于第一列( ComboBoxSource ),但我不知道如何通过其余列继续循环,我希望消息MsgBox & Exit sub仅在满足所有 5 个条件而不是一个条件时出现。

这就是我试图做的:

  Sub check_procedures()
  Dim ws As Worksheet, rngF As Range, rngG As Range, rngH As Range, rngI As Range, rngJ As Range, Form_Date As Date
  
  Set ws = Worksheets("do not open!")
  Set rngF = ws.Range("F1")
  Set rngG = ws.Range("G1")
  Set rngH = ws.Range("H1")
  Set rngI = ws.Range("I1")
  Set rngJ = ws.Range("J1")
  Form_Date = UI.txtDay.Value & "/" & UI.txtMonth.Value & "/" & UI.txtYear
   
   
   Do Until rngF.Value = ""
         If UI.ComboBoxSource = rngF.Value Then
            If UI.ComboBoxLayer1 = rngG.Value Then
                If UI.ComboBoxLayer2 = rngH.Value Then
                    If UI.ComboBoxGrain = rngI.Value Then
                        If Form_Date = rngJ.Value Then
                        MsgBox "This procedure already exhists. Please click on Update Summary. "
                        Exit Sub
                        
                        Else
                        
                        End If
                    
                    Else
                    End If
                
                Else
                End If
            
            Else
            End If
            
         
         
         
         
         Else
         End If
            
            
         Set rngF = rngF.Offset(1)
         Set rngG = rngG.Offset(1)
         Set rngH = rngH.Offset(1)
         Set rngI = rngI.Offset(1)
         Set rngJ = rngJ.Offset(1)
         
         
    
    Loop
    
End Sub

它有效,但我相信它可以是更好的代码并且重复性更少。

我希望这个解释很清楚。

提前致谢。

Set rngF = rngF.Offset(1)行每次将 rngF 向下移动一个,因此只向下移动一列(此代码中没有任何内容可以检查另一列)

编辑:这里唯一真正的变量是你正在检查的行,所以将 rngF、rngG 等存储为单独的变量并单独移动它们是没有意义的。 在 countif 方法中当然没有任何区别。

环形:

(类似于您的尝试,但您可以使用单个If语句和一个行变量)

Sub check_procedures()
    Dim ws As Worksheet, row as Long, Form_Date as Date
    Form_Date = CDate(UI.txtDay.Value & "/" & UI.txtMonth.Value & "/" & UI.txtYear)
    Set ws = Worksheets("do not open!")
    With ws
    Do Until .Range("F" & row) = ""
        If UI.ComboBoxSource = .Range("F" & row).Value _
        And UI.ComboBoxLayer1 = .Range("G" & row).Value _
        And UI.ComboBoxLayer2 = .Range("H" & row).Value _
        And UI.ComboBoxGrain = .Range("I" & row).Value _
        And Form_Date = .Range("J" & row).Value _
        Then
            MsgBox "This procedure already exists. Please click on Update Summary. "
            Exit Sub
        End If
        row = row + 1
    Loop
    End With
End Sub

单线修复

(在多个条件下写起来有点困难,但运行起来更快更简单)

Sub Check_procedures()
Dim ws As Worksheet, Form_Date as Date
Set ws = Worksheets("do not open!")
Form_Date = CDate(UI.txtDay.Value & "/" & UI.txtMonth.Value & "/" & UI.txtYear)
With ws
If WorksheetFunction.CountIfs( _
    .Range("F:F"), UI.ComboBoxSource, _
    .Range("G:G"), UI.ComboBoxLayer1, _
    .Range("H:H"), UI.ComboBoxLayer2, _
    .Range("I:I"), UI.ComboBoxGrain, _
    .Range("J:J"), Form_Date _
    ) > 0 Then
    MsgBox "This procedure already exists. Please click on Update Summary."
End If
End With
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM