【发布时间】:2015-11-04 18:30:46
【问题描述】:
在组合框中选择一个选项后,我试图从工作表中提取数据。所以我在这里所做的是,当我单击搜索按钮时,根据 SearchSelectPPComboBox 中选择的选项,我应该进入 wb 中的 ws 以找到该选项值,然后找到该值在哪一行,这样我就可以通过移动到同一行中的下一列单独提取数据。
但是,我下面的代码在这一行出现“运行时错误 1004 方法‘对象工作表的范围’失败”
设置 FoundCell = ws.Range("F8:F").Find(what:=WHAT_TO_FIND, lookat:=xlWhole)
提前感谢您的帮助!!
Private Sub SearchButton_Click()
If SearchTeamComboBox.ListIndex < 0 And SearchSelectPPComboBox.ListIndex < 0 Then
MsgBox "Please select Team and the Process/project you want to search ."
SearchTeamComboBox.SetFocus
ElseIf SearchTeamComboBox.ListIndex < 0 Then
MsgBox "Please select Team."
SearchTeamComboBox.SetFocus
ElseIf SearchSelectPPComboBox.ListIndex < 0 Then
MsgBox "Please select the Process/project you want to search ."
SearchSelectPPComboBox.SetFocus
Else
Const WHAT_TO_FIND As String = "SearchSelectPPComboBox.value"
Dim ws As Excel.Worksheet
Dim FoundCell As Excel.Range
Set ws = Sheets(SearchTeamComboBox.Value)
Set FoundCell = ws.Range("F8:F").Find(what:=WHAT_TO_FIND, lookat:=xlWhole)
If Not FoundCell Is Nothing Then
MsgBox (WHAT_TO_FIND & " is found ")
Me.checklistComboBox.Value = FoundCell.Offset(0, 1).Value
End If
End If
End Sub
附加代码:
Private Sub SearchTeamComboBox_Change()
Application.EnableEvents = False
SearchSelectPPComboBox.Clear
Application.EnableEvents = True
Dim PP As Object
Dim rngList As Range
Dim strSelected As String
Dim LastRow As Long
' check that a team has been selected
If SearchTeamComboBox.ListIndex <> -1 Then
strSelected = SearchTeamComboBox.Value
If strSelected = "ACLT" Then
LastRow = Worksheets("ACLT").Range("E" & Rows.Count).End(xlUp).row
Set rngList = Worksheets("ACLT").Range("E8:E" & LastRow)
ElseIf strSelected = "AIF/CIF" Then
LastRow = Worksheets("AIFCIF").Range("E" & Rows.Count).End(xlUp).row
Set rngList = Worksheets("AIFCIF").Range("E8:E" & LastRow)
ElseIf strSelected = "FDM" Then
LastRow = Worksheets("FDM").Range("E" & Rows.Count).End(xlUp).row
Set rngList = Worksheets("FDM").Range("E8:E" & LastRow)
ElseIf strSelected = "Imaging" Then
LastRow = Worksheets("Imaging").Range("E" & Rows.Count).End(xlUp).row
Set rngList = Worksheets("Imaging").Range("E8:E" & LastRow)
ElseIf strSelected = "MRT" Then
LastRow = Worksheets("MRT").Range("E" & Rows.Count).End(xlUp).row
Set rngList = Worksheets("MRT").Range("E8:E" & LastRow)
ElseIf strSelected = "PAT" Then
LastRow = Worksheets("PAT").Range("E" & Rows.Count).End(xlUp).row
Set rngList = Worksheets("PAT").Range("E8:E" & LastRow)
ElseIf strSelected = "SSU" Then
LastRow = Worksheets("SSU").Range("E" & Rows.Count).End(xlUp).row
Set rngList = Worksheets("SSU").Range("E8:E" & LastRow)
ElseIf strSelected = "VEL" Then
LastRow = Worksheets("VEL").Range("E" & Rows.Count).End(xlUp).row
Set rngList = Worksheets("VEL").Range("E8:E" & LastRow)
End If
For Each PP In rngList
SearchSelectPPComboBox.AddItem PP.Offset(, 1)
Next PP
End If
End Sub
【问题讨论】: