精确搜索标题名称

时间:2018-07-18 08:06:16

标签: excel-vba

我创建了一个宏来搜索标题名称并插入一列,但是该宏会搜索包含命令中给出的内容的单词,而不是搜索确切的标题名称。

'Looks in entire first row.
Dim rngHeaders As Range Set rngHeaders = Range("1:1")

'To set this to a specific sheetname, use Set ws = Sheets("Sheetname")
Set ws = ActiveSheetSet 
Set rngUsernameHeader = rngHeaders.Find(What:="Account Name",After:=Cells(1,1))

If rngUsernameHeader Is Nothing Then

Else 
    rngUsernameHeader.Offset(0, 1).EntireColumn.Insert 
endif

2 个答案:

答案 0 :(得分:1)

您缺少查找的参数:

Dim rngHeaders As Range 

Set rngHeaders = Range("1:1") 
Set ws = ActiveSheetSet 

Set rngUsernameHeader = rngHeaders.Find(What:="Account Name",After:=Cells(1,1), LookAt:=xlWhole)

If Not rngUsernameHeader Is Nothing Then rngUsernameHeader.Offset(0, 1).EntireColumn.Insert 

答案 1 :(得分:1)

请参考查找功能文档:https://msdn.microsoft.com/en-us/vba/excel-vba/articles/range-find-method-excel

您正在寻找LookIn参数。此参数应设置为xlValues值,这将使函数能够根据其值搜索单元格。