Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 正在尝试在单元格中查找精确字符串的下一个实例_Excel_Methods_Syntax_Vba - Fatal编程技术网

Excel 正在尝试在单元格中查找精确字符串的下一个实例

Excel 正在尝试在单元格中查找精确字符串的下一个实例,excel,methods,syntax,vba,Excel,Methods,Syntax,Vba,我需要帮助在单元格中查找精确字符串的下一个实例 准确地说,我想看一系列标题,找到声明变量的下一个实例,以获得列号,我想看一系列标题,找到下一个空单元格并保存该编号,最后,我想取第一个列号,从第二行开始搜索,直到找到空单元格的第一个实例,并将该数字保存到变量中。我一直在做的是: With Rows(1) Set found = .Find(what:=Target, After:=.Cells(1, 1)) End With 但是,如果我不小心键入“s”,它似乎会找到第一个包含子字符串“

我需要帮助在单元格中查找精确字符串的下一个实例

准确地说,我想看一系列标题,找到声明变量的下一个实例,以获得列号,我想看一系列标题,找到下一个空单元格并保存该编号,最后,我想取第一个列号,从第二行开始搜索,直到找到空单元格的第一个实例,并将该数字保存到变量中。我一直在做的是:

With Rows(1)
    Set found = .Find(what:=Target, After:=.Cells(1, 1))
End With
但是,如果我不小心键入“s”,它似乎会找到第一个包含子字符串“s”(LastName)的单元格实例,而不是第一个只包含“s”的单元格

我担心的是,如果其中有带“”的列,那么我的程序将无法正常运行

除此之外,我按列排序,当该列中的一个单元格为空时,我的程序将其一直推到列表的底部,我试图删除该空单元格空间

我试过使用Application.WorksheetFunction.Match、HLookup和VLookup,但通常工作表函数对我不起作用

举个我想做的例子:

I have 10 Columns with headings. I want to find the first instance of a column that 
contains exactly the string I send into this class. For instance, if the
columns are "FirstName | LastName | Name", I want it to return "Name" 
and not "FirstName". 

I want to find a column that the user requests as a sort key and verify it's existence
I also want to find a column that is empty (last column)
Finally, I want to find the last row that has a value in relation to the SortColumn.
您可以使用do循环:

headerToFind = "Name" 'or whatever header you're looking for
x = 1 'or whatever header row is
y = 1 'or whatever first column with header is
Do Until Cells(x,y) = ""
    If Cells(x,y) = headerToFind then
        MsgBox "The header you are looking for is in row " & x & ", column " & y
        Exit Sub
    End If
    y = y + 1
Loop
MsgBox "Header not found"
在消息框的地方,放上你想用你找到的东西做的任何代码。如果找到标头(x等于行号,y等于列号),则将执行第一个MsgBox。如果找不到所需的标题,则将执行第二个MsgBox。

如果设置为xlother,则它将只匹配单元格的全部内容,例如:

With Rows(1)
    Set found = .Find(what:=target, After:=.Cells(1, 1), lookat:=xlWhole)
End With
若要检查是否找到值,可以检查“找到”是否为“无”

Dim exists As Boolean
If Not found Is Nothing Then exists = True
要在值的行或列的末尾找到第一个空单元格,我将使用查找包含数据的行/列中的最后一个单元格,然后使用偏移量查找下一个单元格:

With Rows(1)
    Set found = .Find(what:=target, After:=.Cells(1, 1), lookat:=xlWhole)
End With
Dim emptyCell As Range

If Not found Is Nothing Then
    Dim col As Integer
    col = found.Column
    Set emptyCell = Columns(col).End(xlDown)
    emptyCell.Offset(1, 0).Select
End If

但是,如果您的值表中间有一些空单元格,则不能使用此值。(例如,如果A1、A2、A3中有值,则A4为空,A5、A6、A7中有更多值)

谢谢大家!!我已经寻找了一个半小时的答案,因为我不能在这个板块上发布其他问题,甚至不能评论其他人的问题。没有足够的因果报应,或者不管这里叫什么。同时,如何搜索列而不是行?从一个特定的专栏开始?我尝试使用列(ColumnIndex:=SortColumn),但这会给我带来错误。这也是另一个问题。现在它给我带来了问题,因为当我想找到我最初发送的空单元格的第一个实例时:“作为目标”。现在它返回第一个单元格作为解决方案。不客气。答案中的链接指向Range.Find的帮助。这描述了各种参数,其中包括SearchOrder,它指定是按行还是按列进行搜索,然后指定范围内的起点。您可以使用列(编号)而不是行(编号)搜索特定列。有趣的。。。如果找不到什么,为什么要用?这意味着什么?