2012-02-27 46 views
2

我有一個工作表,有2列「鑰匙」和「價值」。通過VBA代碼,我想要在Key列上搜索Input_key,如果不存在,我將添加新行[input-key] - [input-value]。我如何編碼?如何使用VBA代碼在工作表上進行搜索?

+4

你有什麼試過?與搜索StackOverflow一樣,錄製宏可以是一個很好的開始。 – Fionnuala 2012-02-27 10:08:31

回答

12

你會從評論中認識到「請爲我解決我的問題」的問題不受歡迎。

我會猜測你不知道從哪裏開始,並會給你一些初步的指導。

轉到Google,然後輸入「excel vba tutorial」。你會被提供很多網站。他們都是不同的,所以嘗試一下,找到適合你的一個。

試試宏記錄器。我設置了一個與您的描述相匹配的工作表,打開宏記錄器,選擇列A,點擊Ctrl+F以獲得查找屏幕,並單擊選項按鈕向我顯示所有選項。其結果是:

enter image description here

看的選項。例如,案件重要嗎?根據需要選擇。我勾選了「匹配全部單元格內容」,輸入「k」並單擊Find Next。光標跳轉到包含「k」的單元格。然後,我關閉了宏記錄器。

保存爲我的代碼是:

Sub Macro1() 
' 
' Macro1 Macro 
' Macro recorded 27/02/2012 by Tony Dallimore 
' 
    Columns("A:A").Select 
    Selection.Find(What:="k", After:=ActiveCell, LookIn:=xlFormulas, LookAt _ 
     :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _ 
     False, SearchFormat:=False).Activate 
End Sub 

這是有效的,但VBA不好VBA。宏記錄器記錄了您執行它的每個動作。它不知道你的意圖。所以我們需要整理這些代碼。

的主要變化是:

  • 我們不想選擇A列或激活包含找到的值的單元格。
  • 我們需要允許找不到值。

將下面的宏複製到宏記錄器保存其代碼的模塊中。我通過修改已保存的代碼來創建這個宏,以創建一個供您玩的測試工具。它要求一個值,在列A中搜索它並說明該值是否被找到。這是您需要的代碼的基礎。

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 

End Sub 

現在再次打開宏記錄器。將光標置於列A下任何具有值的行下方。點擊Ctrl+UpArrow。光標將跳到列A中的最後一個值。關閉宏錄製器。

保存的代碼如下:

Sub Macro2() 
' 
' Macro2 Macro 
' Macro recorded 27/02/2012 by Tony Dallimore 
' 

' 
    Range("A64").Select 
    Selection.End(xlUp).Select 
    Range("A28").Select 
End Sub 

End(xlUp)是VBA的Ctrl+UpArrow。這是查找上次使用的行的最簡單方法。

要添加新行,你想要做的,如果沒有找到該值:

RowCrnt = .Cells(Rows.Count, "A").End(xlUp).Row + 1 
.Cells(RowCrnt,1),Value = "Key" 
.Cells(RowCrnt,2),Value = "Value" 

如果你看看其他的問題,你會發現,End有時會不給你你所期望的結果。在空列上嘗試Ctrl+DownArrowCtrl+UpArrow,在頂部有一個然後兩個值的列,底部有一個然後兩個值的列和具有由空白行分隔的幾個值的列。

這應該讓你開始。歡迎來到Excel編程。祝你好運。

+1

http://www.fishingkites.co.nz/cleaning-fish/fishindex.htm :) – Fionnuala 2012-02-27 13:58:39

+0

你可能是對的,我已經過分了。但是我仍記得大約十年前我第一次開始使用Excel VBA時。我知道所有來自其他語言的控制結構,但是對象模型和宏錄製器沒有任何意義,直到我遇到類似這樣的答案,然後突然全部落空。 – 2012-02-27 17:26:56