VBA查找function忽略百分比 – >只发现小数
我的macrossearch所有打开的工作簿中的活动单元格值(例如98%)。 但是,在其他单元中,它只能find值0.98而不是98%。 为什么?
这是我的macros:
Sub FindIt2() Dim wSheet As Worksheet Dim wBook As Workbook Dim rFound As Range Dim firstAddress As String lookfor = Selection.Value On Error Resume Next For Each wBook In Application.Workbooks For Each wSheet In wBook.Worksheets Set rFound = wSheet.Cells.Find(What:=lookfor, After:=wSheet.Cells(1, 1), _ LookAt:=xlWhole, SearchOrder:=xlByRows, _ SearchDirection:=xlNext, MatchCase:=False) If Not rFound Is Nothing Then firstAddress = rFound.Address Do Application.Goto rFound, True MsgBox "The Search String has been found these locations: " Set rFound = wSheet.Cells.FindNext(rFound) Loop While Not rFound Is Nothing And rFound.Address <> firstAddress End If Next wSheet Next wBook On Error GoTo 0
任何人有一个想法如何解决这个问题? 谢谢!
编辑:我想要find两个:98%和0.98
有时98%是细胞的价值。 即当你在一个单元格中input98%时。 Excel会将其视为98%。 在其他情况下,单元格值为.98或.98231,并显示98%。 最有可能的是,你要查找两个有效数字的总结,以便当它的值是.98321时find.98。
我会尝试寻找两个。
cell.text和round(cell.value,2)
如何用excel VBA round()来完成?
本文介绍如何在Excel VBA中使用循环function
实际上,即使数值与源(函数Find)
参数Find)
相同, Find
也会以各种格式(不仅是百分比)丢失值。 在下面列出的各种条件下,“ Find
仅查找内容为0.98
单元格,使用常规或数字(2个十进制数字)格式。
我试过的:
-
使用
lookfor = Selection.Value
。 更改Selection
指向的(源)单元格的数字格式。 如果Selection
的格式是百分比,数字(任何十进制数字)或常规,则无关紧要。Find
只发现与0.98
单元格,而不是与0.980
,例如 -
使用
lookfor = Selection.Text
。 更改Selection
指向的(源)单元格的数字格式。 只查找与查看的号码完全相同的单元格。
奇怪,因为它可能是,这需要一个解决方法,因为Find
不会同时find0.98
和98%
。 一种select是使用统一格式的一个或多个帮助列,并在这些列上执行find
。
当FIND
不起作用时,一个解决scheme是循环遍历单元格。 如果你有大量的单元格,这可能会很慢,所以读取要search的范围将会使速度提高十倍或更多。
但是这是一个循环的想法,这将使你开始。 注意我使用了value2
属性。 请参阅Charles Williams的解答 。
首先运行Setup
macros,然后运行findValuePercent
,看看事情是如何工作的。 然后您可以根据您的具体要求进行调整。
Option Explicit Sub Setup() Dim WS As Worksheet Set WS = Worksheets("sheet1") With WS .Cells(1, 1).Value = "98%" .Cells(2, 1).Value = 0.98 Debug.Print .Cells(1, 1).Value, .Cells(1, 1).Text ' --> 0.98 98% Debug.Print .Cells(2, 1).Value, .Cells(2, 1).Text ' --> 0.98 0.98 End With End Sub Sub findValuePercent() Dim WS As Worksheet Dim R As Range Dim C As Range Dim S As String Const dWhat As Double = 0.98 Set WS = Worksheets("sheet1") Set R = WS.UsedRange For Each C In R If C.Value2 = dWhat Then S = S & vbLf & C.Address & vbTab & C.Text End If Next C MsgBox "Matches in" & S End Sub