简体   繁体   English

Excel VBA.find 找不到值

[英]Excel VBA .find not find the value

I wanted to make a schedule macro and i tried to build in some random element too.我想制作一个时间表宏,我也尝试构建一些随机元素。

Excel结构

And in VBA i tried to .Find() the minimum value of Column "I" at first it worked, but when I expand the code to make is more useful for this case, suddenly its not find the value.在 VBA 中,我最初尝试.Find()列“I”的最小值,但当我扩展代码以使其对这种情况更有用时,突然找不到该值。

Sub schedule()

    Dim ws As Worksheet
    Dim days As Range, balance As Range
    Dim min As String
    Dim a As Range, i As Integer, o As Integer, Cell As Range

    Set ws = Application.Sheets("Teszt")
    Set days = ws.Range("L11:AP23")
    Set balance = ws.Range("I11:I23")

    
    For o = 1 To 31
        For i = 1 To 6
            'min = Format(WorksheetFunction.min(balance), "0.00")
            min = WorksheetFunction.min(balance)
            
                        'For Each Cell In balance
                        '    Debug.Print Cell.Value
                        'Next Cell
                        Debug.Print "----"
                
            Set a = balance.Find(what:=min, _
                LookIn:=xlValues, _
                lookat:=xlWhole, _
                searchorder:=xlByRows, _
                searchdirection:=xlNext, _
                MatchCase:=False, _
                SearchFormat:=False)
                
            a.Offset(0, o + 2).Value = "x"
            a.Offset(0, 1).Value = 1
            
        Next i
        
        ws.Range("J11:J23").Value = ""
        
    Next o
End Sub

Now i don't find why the "Set a" part not working Column "I" contains a formula G-H+J the result is a double and I search the lowest value from that range.现在我不明白为什么“设置 a”部分不起作用 列“I”包含公式 G-H+J 结果是双精度值,我从该范围内搜索最小值。 For teszting purposes i only work with 11:23 Row出于测试目的,我只使用 11:23 Row

Well.出色地。 It's not a "true" answer for this problem but it works anyway.这不是这个问题的“真实”答案,但无论如何它都有效。 So, I just leave it here to anyone who have similar problem:所以,我把它留给有类似问题的人:

Just don't use Double.只是不要使用双。 Or Single.或单身。 No floating point, no problem... There are plenty of way to bring it up above zero and if it's just an integer it works fine.没有浮点,没问题......有很多方法可以将其提高到零以上,如果它只是一个 integer 它工作正常。

I thought some other methods but any of it would take much more computing capacity than it's worth.我想到了其他一些方法,但其中任何一种方法都需要比它的价值更多的计算能力。 Not to mention that working with integer is much faster.更不用说使用 integer 的速度要快得多。 After I switched to it the code is at least two times faster.在我切换到它之后,代码至少快了两倍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM