For loop 请更改为嵌套for循环

For loop 请更改为嵌套for循环,for-loop,combobox,nested,range,cell,For Loop,Combobox,Nested,Range,Cell,由于格式错误,我已编辑了我的帖子。 谢谢你的反馈 现在我使用了很多“For Each”来查找范围。 我首先垂直搜索匹配的组合框值(列),然后水平搜索(行) 然后进入特定范围 是否有任何方法可以使用loop找到包含我的组合框值的单元格,然后选择范围 多谢各位。 NB:-如果你觉得我的节目是个喜剧,请不要笑。因为我从谷歌上学到了这一切 'Cancel Button` Private Sub CommandButton2_Click() Unload UserForm1 End Sub` 'Goto

由于格式错误,我已编辑了我的帖子。 谢谢你的反馈

现在我使用了很多“For Each”来查找范围。 我首先垂直搜索匹配的组合框值(列),然后水平搜索(行) 然后进入特定范围

是否有任何方法可以使用loop找到包含我的组合框值的单元格,然后选择范围

多谢各位。 NB:-如果你觉得我的节目是个喜剧,请不要笑。因为我从谷歌上学到了这一切

'Cancel Button`
Private Sub CommandButton2_Click()
Unload UserForm1
End Sub`

'Goto range selection3
Sub CommandButton1_Click()
On Error Resume Next
If ComboBox3.Value = "Select" Then
MsgBox "Please Select a Seller"
Else
Call Show_Page
Unload UserForm1`
End If
End Sub

'select page
Sub Show_Page()

For Each cell In Range("A2:A10")
If cell.Value = Me.ComboBox3.Value Then
Worksheets("p1").Activate
Call Show_Range
Exit For
Exit Sub
End If
Next

For Each cell In Range("B2:B10")
If cell.Value = Me.ComboBox3.Value Then
Worksheets("p2").Activate
Call Show_Range
Exit For
Exit Sub
End If
Next

For Each cell In Range("C2:C10")
If cell.Value = Me.ComboBox3.Value Then
Worksheets("p3").Activate
Call Show_Range
Exit For
Exit Sub
End If
Next

For Each cell In Range("D2:D10")
If cell.Value = Me.ComboBox3.Value Then
Worksheets("p4").Activate
Call Show_Range
Exit For
Exit Sub
End If
Next

For Each cell In Range("E2:E10")
If cell.Value = Me.ComboBox3.Value Then
Worksheets("p5").Activate
Call Show_Range
Exit For
Exit Sub
End If
Next

End Sub

Sub Show_Range()

For Each cell In Worksheets("home").Range("A2:E2")
If cell.Value = Me.ComboBox3.Value Then
ActiveSheet.Range("A1:j106").Select
Exit For
Exit Sub
End If
Next

For Each cell In Worksheets("home").Range("A3:E3")
If cell.Value = Me.ComboBox3.Value Then
ActiveSheet.Range("A111:j216").Select
Exit For
Exit Sub
End If
Next

For Each cell In Worksheets("home").Range("A4:E4")
If cell.Value = Me.ComboBox3.Value Then
ActiveSheet.Range("A221:j326").Select
Exit For
Exit Sub
End If
Next

For Each cell In Worksheets("home").Range("A5:E5")
If cell.Value = Me.ComboBox3.Value Then
ActiveSheet.Range("A331:j436").Select
Exit For
Exit Sub
End If
Next

For Each cell In Worksheets("home").Range("A6:E6")
If cell.Value = Me.ComboBox3.Value Then
ActiveSheet.Range("A441:j546").Select
Exit For
Exit Sub
End If
Next

For Each cell In Worksheets("home").Range("A7:E7")
If cell.Value = Me.ComboBox3.Value Then
ActiveSheet.Range("A551:j656").Select
Exit For
Exit Sub
End If
Next

For Each cell In Worksheets("home").Range("A8:E8")
If cell.Value = Me.ComboBox3.Value Then
ActiveSheet.Range("A661:j766").Select
Exit For
Exit Sub
End If
Next

For Each cell In Worksheets("home").Range("A9:E9")
If cell.Value = Me.ComboBox3.Value Then
ActiveSheet.Range("A771:j876").Select
Exit For
Exit Sub
End If
Next

For Each cell In Worksheets("home").Range("A10:E10")
If cell.Value = Me.ComboBox3.Value Then
ActiveSheet.Range("A881:j986").Select
Exit For
Exit Sub
End If
Next
End Sub

我想这可能是您想要的。SheetName是一个数组,RangeStrings也是一个数组,以便以后可以更改它们。(注意:数组是基于零的,因此我们跳过了zero eth项)

主要是,只检查关键区域中的单元格一次。然后使用找到的单元格的列位置确定要激活的正确SheetName,并使用找到的单元格的行位置确定要选择的正确RangeString。当找到第一个匹配项时,将退出此操作。(我不理解您最初的退出,然后是退出子-->您希望发生什么?可以“选择”多个页面吗?)


上面的可能无法运行。它缺少一个SUB和一个NEXT。看来您已经编辑/排列了原始代码,可能添加了空行,可能遗漏了一些行。您可以粘贴原始代码吗?先生,谢谢您的回复。我已经编辑了我的帖子。请看一看。现在我使用了很多“for Each”来查找范围。我是首先垂直搜索匹配的组合框值(列),然后水平搜索(行)然后转到特定范围。是否有任何方法可以使用循环找到包含我的combobox值的单元格,然后选择范围。?感谢您的大力支持…我将我的程序替换为Sub Show_页面。不幸的是,它无法工作。没有显示错误。我感谢我未能将其与ComboBox3连接。是否需要进行任何修改关于这个…等待您的答复…再次感谢。修改为使用ComboBox3(我一直在用表(“home”)测试。单元格(1,“A”))不,先生。它不起作用…我会再检查一次,让你知道。我不能按照第二部分给出的条件找到特定的范围。请给出答复。先生,它不起作用,没有显示错误。我不能按照第二部分给出的条件。我是否必须再给出一个循环才能找到范围。请答复。
'select page
Sub Show_Page()

    ' look only once at all cells in the key area
    For Each aCell In Sheets("home").Range("A2:E10")

        If aCell.Value = Me.ComboBox3.Value Then  '>> OOps, my test only Sheets("home").Cells(1, "A") Then
            Dim sheetNamesArray() As Variant, sheetIndex As Long, theSheet As String
            sheetNamesArray = Array("zero", "offset1", "p1", "p2", "p3", "p4", "p5")
            sheetIndex = aCell.Column   ' numeric value equivalent of "A", ... "E"
            theSheet = sheetNamesArray(sheetIndex)
            Worksheets(theSheet).Activate

            Dim sheetRangesArray() As Variant, rangeIndex As Long, rangeString As String
            sheetRangesArray = Array("zero", "notRow1", "A001;J106", "A111;J216", "A221;J326", "A331;J436", "A441;J546", "A551;J656", "A661;J766", "A771;J876", "A881;J986")
            rangeIndex = aCell.Row      ' numeric value 2...10
            rangeString = sheetRangesArray(rangeIndex)

            ActiveSheet.Range(rangeString).Select

            Exit Sub    ' once we have found it, exit the SUB (and implicitly also the FOR)
        End If

    Next aCell

End Sub

'eliminate Show_Range()