簡體   English   中英

使用VBA搜索范圍並將行的值返回給用戶窗體

[英]Using VBA to search a range & return values of row to userform

我使用一種用戶窗體(BoaterChecklist)將數據輸入到excel工作表中,並且正在尋找一種功能,以按列C中的唯一ID(PermitNumber)搜索,以返回匹配ID的行以更新和編輯該數據。 我在將找到的行加載回用戶表單時遇到麻煩。 我相信我的代碼可以在C列中找到正確的ID單元格,但是並沒有使其處於活動狀態。 在工作表上選擇的任何單元格都是將行數據加載到用戶窗體中的內容,而不是找到的單元格。

我不確定在哪里實現“ ActiveCell.Value”以為找到的ID返回正確的行值。 任何幫助將不勝感激!

Private Sub SearchForm_Click()
Dim str as String
Dim rgFound as Range

if Permitnumber.text = "" then
msg box "enter a permit number"
exit sub
end if

with worksheets("sheet1")
 str = PermitNumber.Value
 Set rgFound = .Range("c2:c3000").Find(what:=str)
 if rgFound is Nothing then       
 msgbox "permit not found"
else
DateBox.Text = Cells(ActiveCell.row, 1)
TimeBox.Text = Format(cells(ActiveCell.Row, 2), "hh:mm:ss")
PermitNumber.Text = Cells (ActiveCell.Row, 3)
VesselName.Text = Cells (ActiveCell.Row, 4)
 'ect...

end if
end with 
end sub

謝謝!

根據建議,使用found范圍作為參考


Option Explicit

Private Sub SearchForm_Click()
    Dim str As String, found As Range

    If PermitNumber.Text = vbNullString Then
        MsgBox "Enter a permit number"
        Exit Sub
    End If

    With Worksheets("Sheet1")
        str = PermitNumber.Value
        Set found = .UsedRange.Find(What:=str)

        If found Is Nothing Then
            MsgBox "Permit not found"
        Else
            DateBox.Text = .Cells(found.Row, 1).Value
            TimeBox.Text = Format(.Cells(found.Row, 2), "hh:mm:ss")
            PermitNumber.Text = .Cells(found.Row, 3)
            VesselName.Text = .Cells(found.Row, 4)
            'ect...
        End If
    End With
End Sub

為了在搜索時進行更精確的控制,請指定不應使用默認值的參數

Range.Find-方法參數

 Required Parameter -------------------------------------------------------------------- What:= - Value to search for (string or any Excel data type) - If What:="" is used, it will return Nothing (no cell found) - If What:="*" is used, it will return first non empty cell 
 Optional Parameters ------------------------------------------------------------------- After:= - Starts search after (single) cell specified: After:=Cells(1) - It excludes the specified cell, so it starts on Cells(2) - It defaults to upper-left corner of the searched Range - To move to the next found value after the first use: - After:=Cells(1).Offset(1) along with - SearchOrder:=xlByRows or xlByColumns LookIn:= - Formulas, Values, Comments. Defaults to Formulas LookAt:= - xlWhole or xlPart. If not specified it defaults to xlPart SearchOrder:= - xlByRows or xlByColumns. Defaults to xlByRows SearchDirection:= - xlNext or xlPrevious. Defaults to xlNext MatchCase:= - True (search is case sensitive). Default to False MatchByte:= - Used only for double-byte language support - True to have double-byte chars match only double-byte chars - False to have double-byte chars match their single-byte equiv SearchFormat:= - Searches Font and/or cell formatting. Defaults to False * LookIn, LookAt, SearchOrder, and MatchByte are saved each time you use this method ** Not specifying these parameters, it will use the previous search settings 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM