我在组合框中选择一个选项后尝试从工作表中提取数据。所以我在这里做的是当我点击搜索按钮时,根据SearchSelectPPComboBox中选择的选项,我应该进入wb中的ws找到这个选项值,然后找到这个值所在的那一行我可以通过移动到同一行中的下一列来单独提取数据。
但是,我的下面的代码是"运行时错误1004方法'范围的对象_工作表'失败"在这一行
设置FoundCell = ws.Range(" F8:F")。查找(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
答案 0 :(得分:0)
应该是,因为您使用了不完整的范围Range("F8:F")
。
您要在哪里找到单词,整个F列或只是F列的一部分。
对于整个专栏,您应该使用如下:
Set FoundCell = ws.Range("F:F").Find(what:=WHAT_TO_FIND, lookat:=xlWhole)
对于列的部分,您应该使用如下:
Set FoundCell = ws.Range("F8:F100").Find(what:=WHAT_TO_FIND, lookat:=xlWhole)
如果您不知道固定的最后一行,可以按照以下步骤使用。
Set FoundCell = ws.Range("F8:F" & ws.Range("F8").SpecialCells(xlCellTypeLastCell).Row).Find(what:=WHAT_TO_FIND, lookat:=xlWhole)
其中,ws.Range("F8").SpecialCells(xlCellTypeLastCell).Row
获取最后一个使用过的单元格。
答案 1 :(得分:0)
尼古拉斯为你的第一期提供了答案。
整栏。现在没有错误,但是当我点击搜索时没有发生任何事情。我正在尝试将找到的单元格旁边的列中的数据放入我的userform中的文本框中。 @ nicolas- Gwen44分钟前
以下代码不正确:
Const WHAT_TO_FIND As String = "SearchSelectPPComboBox.value"
您将值“SearchSelectPPComboBox.value”提供给WHAT_TO_FIND。 代码应该是:
Const WHAT_TO_FIND As String = SearchSelectPPComboBox.value
“”里面的文字成为一个字符串,因此它搜索字符串“SearchSelectPPComboBox.value”,通过删除“”SearchSelectPPComboBox.value的值成为字符串。
编辑:
常量需要非变量字符串。 SearchSelectPPComboBox.value是一个变量字符串,因此不能定义为常量。
请改用以下代码:
Dim WHAT_TO_FIND as string
WHAT_TO_FIND = SearchSelectPPComboBox.value