excel - VBA cell.value 作为字符串 - 类型不匹配
问题描述
我有一个姓名数据库,我正在尝试对其进行设置,以便我可以搜索名字,然后将其带到具有所述名字的行。我现在使用的代码是:
Option Compare Text
Sub NameFinder()
'Search for name, then produce name and birthday
Sheets("Guests").Activate
Dim s As String, r As Range, found As Integer
Set begin = Range("D2")
s = InputBox("Enter a name to search.")
Set r = Range(begin, begin.End(xlDown))
found = WorksheetFunction.CountIf(r, s)
MsgBox ("There are " & found & " people with the name " & s)
For Each cell In r
If cell.Value = s Then
Application.Goto cell.EntireRow, tru
End If
Next
End Sub
cell.Value 不查找字符串,因此它不断给我一条错误消息。我能做些什么来阻止这种情况?
解决方案
只需过滤您的名称范围
Option Compare Text
Sub NameFinder()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Guests")
Dim s As String, lr As Long
Dim Found As Range
lr = ws.Range("D" & ws.Rows.Count).End(xlUp).Row
s = InputBox("Enter a name to search")
Set Found = ws.Range("D1:D" & lr).Find(s)
If Not Found Is Nothing Then
'Filter to name
ws.Range("D1:D" & lr).AutoFilter 1, s
Else
MsgBox "Name not found"
End If
End Sub
推荐阅读
- browser - 使用 Applescript 控制多个浏览器
- java - BlockingQueue 的替代方案,用于解决生产者-消费者类型问题的线程间通信
- razor-pages - Blazor(Razor 组件)从父组件刷新子组件
- unity3d - Unity Editor 在播放模式下随机冻结
- html - 如何将 VBA 程序更改为从 Internet Explorer 运行到 Microsoft Edge?
- node.js - 使用 npm nock 对基本功能进行单元测试(主机名未定义)
- python-3.x - 读取多个 txt 并为每个使用原始文件中的名称创建 df
- python - 基于时间戳间隔的求和频率
- php - Laravel 使用 XML 测试 POST 路由
- kubernetes - Kubernetes Pod 安全策略默认特权值