【发布时间】:2019-10-01 07:40:22
【问题描述】:
我希望实现以下目标:
- 通过输入框接受用户输入。
- 在表格标题中搜索该文本。
- 过滤找到的列以删除所有空白单元格(只留下包含数据的单元格。)
我找到了一个脚本,给输入框,搜索表头并选择找到的单元格。
我需要将过滤找到的单元格列的步骤合并到这个步骤中。如果我记录步骤,无论我搜索什么,它都会过滤同一列,所以我认为我需要一种方法来读取找到的单元格详细信息并选择该列来过滤掉空白。
Sub Find_First()
Dim FindString As String
Dim Rng As Range
FindString = Application.InputBox("Enter a Search value")
If Trim(FindString) <> "" Then
With Sheets("ACM").Range("B2:DA2") ' This is the table headers
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Application.Goto Rng, True
Else
MsgBox "Nothing found"
End If
End With
End If
End Sub
我现在可以使用以下代码运行它,如果我取消 InputBox,我现在得到的唯一错误是 1004(WorksheetFunction 类)错误:-
Sub Find_First()
Dim i1 As Integer
Dim FindString As String
Dim Rng As Range
Dim rngData As Range
Set rngData = Application.Range("A2").CurrentRegion
FindString = Application.InputBox("Enter a Search value")
If Trim(FindString) <> "" Then
With Sheets("ACM").Range("B2:DA2") ' This is the table headers
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Application.Goto Rng, True
Else
MsgBox "Nothing found"
End If
End With
End If
i1 = Application.WorksheetFunction.Match(FindString, Application.Range("A2:CZ2"), 0)
Rng.AutoFilter Field:=i1, Criteria1:="<>"
End Sub
【问题讨论】:
-
您不会在 Internet 上找到可以为您完成所有工作的代码示例。从第一步开始并解释您具体遇到的问题,您可能会得到一个很好的答案。您是否能够制作输入框并将其输入设置为变量?你知道如何在一行中搜索你的变量吗?如果您的输入被用户拼写错误怎么办?你知道如何自动化自动过滤器吗?
-
如果您在修补该代码时遇到问题,请向我们展示您尝试过的代码
-
你的输入框工作了吗?或者任何失败的尝试?请阅读Why is “Can someone help me?” not an actual question?
-
这看起来是一个很好的开始,我要为第 2 部分更改的唯一部分是
xlWholetoxlPart这样您就可以只搜索标题中的一个单词而不是整个标题.真的,你只剩下如何完成第 3 部分了 -
您可以查看此链接以了解如何设置过滤器。在他们的示例中,他们使用
ActiveCell,但您的将是Rngmrexcel.com/forum/excel-questions/…