2012-02-27 128 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