2015-05-25 34 views
0

在前面的一段代碼中,我運行了一個Range.Find方法來返回在特定列中找到特定日期的所有工作表的名稱。 「How do I get vba loop result to populate a combobox?」這些工作表名稱隨後用於填充組合框(CboReviewModule)。在此子例程中,希望用戶從此組合框中選擇一個工作表名稱。當做出選擇時,我希望Range.Find方法在選定工作表的第40列中運行,找到變量(myDate)中保存的第一個日期值,然後將單元格設置爲Activecell。之後,我使用.offset填充一系列文本框,其中所有單元格的值位於Activecell左側。Range.Find方法返回搜索的第一次發生

我想不出如何使Range.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 

回答

0

這裏是要找到在列的特定日期一個辦法:

Sub FindingChristmas() 
    Dim myDate As Date, r As Range 
    Dim colmn40 As Range 
    Set colmn40 = Range("AN:AN") 
    myDate = DateSerial(2014, 12, 25) 

    Set r = colmn40.Find(What:=myDate, After:=colmn40(1)) 
    r.Select 
End Sub 

enter image description here

0

找到首次發生日期值保存在變量(myDate)中,然後將單元格設置爲Activecell。之後,我使用.offset填充一系列文本框,其中所有單元格的值位於Activecell左側。

您的代碼爲.Find是正確的。但是,您錯誤地使用了後面的部分。更換ActiveCellrngFind

試試這個

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 
相關問題