我已编辑我的帖子,发现格式错误。 谢谢您的反馈。
现在,我正在使用很多“ For Each”来查找范围。 我首先垂直搜索匹配的组合框值(列(然后是horizondaly(行) 然后转到特定范围。
有什么方法可以使用循环找到带有我的组合框值的单元格,然后选择范围。
谢谢。 注意:-如果您觉得我的节目像漫画,请不要笑。因为我从谷歌搜索中学到了这一切。
'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
答案 0 :(得分:0)
我认为这可能是您想要的。 SheetNames是一个数组,RangeStrings也是一个数组,以便以后可以更改它们。 (注意:数组是从零开始的,因此我们跳过了零位的项目)
主要,仅遍历Key区域中的单元格一次。然后,使用找到的单元格的列位置来确定要激活的正确SheetName,并使用找到的单元格的行位置来确定要选择的正确RangeString。找到第一个比赛时退出。 (我不明白您原来的Exit For,然后再退出Exit->您期望发生什么?可以“选择”多个页面吗?)
'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()