VBA查找函数不能使用变量 [英] VBA Find function not working with variable

查看:311
本文介绍了VBA查找函数不能使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个工作表,需要该应用程序来搜索excel表以获取保存在变量中的值。
我正在使用.Find功能。

我的问题是,当我输入一个实际的值时,它被存储在一个变量名中,找不到值。

I am creating a sheet that needs the application to search excel sheet for a value that is saved in a variable.
I am using the ".Find" function for this.
My problem is that the value being searched for can not be found while it is stored in a variable name although it works when i input an actual value.

例如:

此作品

Dim cellersd, celltid, ersdcol, tidcol
Set cellersd = book.Worksheets(mon & yer).Range("5:5").Find("28/08/2013")
Set celltid = book.Worksheets(mon1 & yer1).Range("5:5").Find("30/09/2013")

If cellersd Is Nothing Then
MsgBox "not found"
Else
MsgBox cellersd.Column
End If

If celltid Is Nothing Then
MsgBox "not found"
Else
MsgBox celltid.Column
End If

这不工作

rsd = Worksheets("workload").Range("p4").Value
tid = Worksheets("workload").Range("p3").Value
...

Dim cellersd, celltid, ersdcol, tidcol
Set cellersd = book.Worksheets(mon & yer).Range("5:5").Find(rsd)
Set celltid = book.Worksheets(mon1 & yer1).Range("5:5").Find(tid)

If cellersd Is Nothing Then
MsgBox "not found"
Else
MsgBox cellersd.Column
End If

If celltid Is Nothing Then
MsgBox "not found"
Else
MsgBox celltid.Column
End If

代码中的...只显示我在其间执行不同操作的其他代码

The ... in the code just shows that i have other codes inbetween which perform different actions

你能发现我的错误吗?
我也可以提出可以同样工作的其他搜索方法的建议。
非常感谢。

Can you spot my error? i am also open to suggestions on other search methods that could equally work. Thanks a lot.

推荐答案

也许尝试

rsd = Worksheets("workload").Range("p4").Text
tid = Worksheets("workload").Range("p3").Text

或者您希望保留 .Value .Text 不工作,你可以尝试转换为字符串:

or if you wish to keep .Value or .Text does not work you chould try to convert to string:

rsd = str(rsd)
tid = str(tid)

我相信 .Value 返回日期而不是字符串。而工作代码正在使用一个字符串。

I believe the .Value returns a date and not a string. and the working code is using a string.

这篇关于VBA查找函数不能使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆