【问题标题】:Using formula as a condition in do while loop在 do while 循环中使用公式作为条件
【发布时间】:2013-04-24 21:13:34
【问题描述】:

我有一列包含时间值:

05:13:32
05:13:32
05:13:32
05:13:32
05:13:33
05:13:33
05:13:34
05:13:34
05:13:35
05:13:35
05:13:36
05:13:36
05:13:36
05:13:37
05:13:37
05:13:38
05:13:38
05:13:39
05:13:39

这些值在 C 列中。

现在:第一个点是我开始寻找比第一个值多 2 秒的下一个值的点,这意味着我会找到 05:13:34。

我编写了这段代码,但它不起作用,因为 Find 函数 (What:=) 不接受我声明为 x= Activecell.Value + "00:00:2" 的变量

Sub sekundenfinder2()
    Dim sekundo2 As Range
    Dim x as Integer 
    Range("C153").Select  
    ' //// Here is the Point where I begin to go the colomn down and look for the next value 

    Selection.Activate
    Set sekundo2 = Range("C:C").Find(What:="x", After:=ActiveCell)
    sekundo2.Select
End Sub

第二个问题:

Activecell.Value 给出的值是 0,21.......,表示 Excel 将值从时间转为数字,但我想将时间保持为“00:00:00”这种格式.我尝试将格式更改为时间,它仍然将其更改为数字。

有什么建议吗?

【问题讨论】:

    标签: vba loops while-loop


    【解决方案1】:
    1. 使用DateAdd 添加时间
    2. 当您在引号内使用x 时,它不会保留为变量,而是会更改为字符串。
    3. x 声明为Date 而不是Integer

    这是你正在尝试的吗?我假设数据来自C1C105:13:32

    Sub sekundenfinder2()
        Dim sekundo2 As Range
        Dim x As Date
    
        x = DateAdd("s", 2, Range("C1").Value)
    
        Set sekundo2 = Range("C:C").Find(What:=x)
    
        If Not sekundo2 Is Nothing Then sekundo2.Select
    End Sub
    

    【讨论】:

    • 非常感谢!它帮助了我
    猜你喜欢
    • 1970-01-01
    • 2021-07-18
    • 2013-03-23
    • 1970-01-01
    • 1970-01-01
    • 2022-10-14
    • 2022-12-12
    • 2016-02-14
    • 1970-01-01
    相关资源
    最近更新 更多