類似CTRL + F我試圖弄清楚什麼是對CTRL +˚F命令的實際代碼,但無法找到它。我發現的最近的是粘貼在下面;唯一的問題是我無法正確更改範圍。搜索框/按鈕在VBA
我想改變範圍來查找整個工作表中的值只在A列。我還想,如果不是返回一條消息說它找到了X行的值,我更喜歡它是否真的去它。我是VBA的新手,我對理解活動範圍的理解最爲困難。
Sub PlayMacro()
Dim Prompt As String
Dim RetValue As String
Dim Rng As Range
Dim RowCrnt As Long
Prompt = ""
' The macro recorder has used the active worksheet. This says which
' worksheet is to be used whether it is active or not. Change "Sheet4"
' to the name of your worksheet.
With Sheets("Sheet4")
' This will loop forever unless a statement within
' the loop exits the Do.
Do While True
RetValue = InputBox(Prompt & "Give me a value to look for")
'RetValue will be empty if you click cancel
If RetValue = "" Then
Exit Do
End If
' I do not wish to active the cell containing the required value.
' I want to know where it is.
Set Rng = .Columns("A:A").Find(What:=RetValue, After:=.Range("A1"), _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If Rng Is Nothing Then
' The entered value could not be found
Prompt = "I could not find """ & RetValue & """"
Else
' The entered value was found
RowCrnt = Rng.Row
Prompt = "I found """ & RetValue & """ on row " & RowCrnt
End If
Prompt = Prompt & vbLf
Loop
End With
什麼是你的代碼錯誤?它只搜索A列。如果找到'Rng',那麼您可以執行'Rng.select'來激活找到該值的單元格。你也可以這樣說'If Rng.Row = 3 Then ...'如果值在第三行...?如果你想搜索整個表格,只要執行'Set Rng = .Cells.Find(...' – BruceWayne
FWIW'LookIn:= xlFormulas,LookAt:= xlWhole'是一個奇怪的組合,特別是當用戶提示是「給我一個**值**尋找」 - 你確定你不想看「xlValues」嗎? – YowE3K