Vba 查找方法返回搜索的第一次出现
在前面的代码中,我运行了Range.Find方法来返回在特定列中找到特定日期的所有工作表的名称。然后使用这些工作表名称填充组合框CboReviewModule。在此子例程中,a希望用户从此组合框中选择一个工作表名称。选择后,我希望在所选工作表的第40列中运行Range.Find方法,查找变量myDate中保存的日期值的第一次出现,然后将单元格设置为Activecell。之后,我将使用.offset填充一系列文本框,其中包含位于Activecell左侧的所有单元格的值 我不知道如何使范围。Find方法返回一个肯定的结果。以下代码是许多失败尝试的最新迭代:Vba 查找方法返回搜索的第一次出现,vba,excel,methods,combobox,Vba,Excel,Methods,Combobox,在前面的代码中,我运行了Range.Find方法来返回在特定列中找到特定日期的所有工作表的名称。然后使用这些工作表名称填充组合框CboReviewModule。在此子例程中,a希望用户从此组合框中选择一个工作表名称。选择后,我希望在所选工作表的第40列中运行Range.Find方法,查找变量myDate中保存的日期值的第一次出现,然后将单元格设置为Activecell。之后,我将使用.offset填充一系列文本框,其中包含位于Activecell左侧的所有单元格的值 我不知道如何使范围。Find
Private Sub CboReviewModule_Change()
Dim ws As Worksheet
Dim wsName As String
Dim myDate As Date
Dim rngFind As Range
Dim firstAddress As String
Dim iCount As Integer
Dim myArray(38) As Variant
'Set date variable equal to value of combobox selection
myDate = Me.CboReviewWeek.Value
'MsgBox (myDate) '{test successful}
'Set ws name variable equal to value of combobox
wsName = Me.CboReviewModule.Value
'MsgBox (wsName) '{test successful}
With ActiveWorkbook.Worksheets(wsName)
'Run Find command on defined range and save result to range variable
Set rngFind = .Columns(40).Find(What:=myDate, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlRows, SearchDirection:=xlNext, MatchCase:=False).Row
'***** Add line to clear CboReviewModule each time the CboReviewWeek is changed. ***** _
****** Once a module is found on the selected date, the module Cbo won't clear when choosing a new date. *****
'If cell is empty, loop to next row, if cell value matches myDate _
then pass cell address value to string variable
If rngFind Is Nothing Then
GoTo myNext
ElseIf rngFind = myDate Then
Do 'do this thing
'set values of array by cell contents to the left of active cell
myArray(0) = ActiveCell.Offset(, -39).Value
myArray(1) = ActiveCell.Offset(, -38).Value
myArray(2) = ActiveCell.Offset(, -37).Value
myArray(3) = ActiveCell.Offset(, -36).Value
myArray(4) = ActiveCell.Offset(, -35).Value
'and so on
'populate values of userform cells based on contents of array
Me.TxtAccount.Value = myArray(0)
Me.TxtMR.Value = myArray(1)
Me.TxtName.Value = myArray(2)
Me.TxtType.Value = myArray(3)
Me.TxtFinClass.Value = myArray(4)
Loop While rngFind.Address <> firstAddress And Not rngFind Is Nothing
End If
End With
myNext:
Next Cell
以下是在第40列中查找特定日期的一种方法: 查找保存在变量myDate中的日期值的第一次出现,然后将单元格设置为Activecell。之后,我将使用.offset填充一系列文本框,其中包含位于Activecell左侧的所有单元格的值 您的.Find的代码是正确的。但是,您不正确地使用了后面的部分。用rngFind替换ActiveCell 试试这个
Set rngFind = .Columns(40).Find(What:=mydate, _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlRows, _
SearchDirection:=xlNext, _
MatchCase:=False).Row
If Not rngFind Is Nothing Then
myArray(0) = rngFind.Offset(, -39).Value
'
'~~> Rest of the code
'
End If
Set rngFind = .Columns(40).Find(What:=mydate, _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlRows, _
SearchDirection:=xlNext, _
MatchCase:=False).Row
If Not rngFind Is Nothing Then
myArray(0) = rngFind.Offset(, -39).Value
'
'~~> Rest of the code
'
End If