简体   繁体   English

VBA 如何使用 Application.WorksheetFunction 添加循环。?

[英]VBA How to add a loop with Application.WorksheetFunction.?

can you please advise how to loop my code through all populated rows (based on row D)?您能否建议如何在所有填充的行中循环我的代码(基于 D 行)? I need to subtract d2 from ad2, d3 from ad3 and so on and put the results in ae column (offset I guess).我需要从 ad2 中减去 d2,从 ad3 中减去 d3 等等,然后将结果放在 ae 列中(我猜是偏移量)。

Ideally, avoiding entering formulas in ae and instead using Application.WorksheetFunction.Value=Total ?理想情况下,避免在 ae 中输入公式,而是使用Application.WorksheetFunction.Value=Total

Sub valuedifference()
Dim Total As Double
Dim TimeX As Date
Dim TimeY As Date

With ThisWorkbook.Sheets("Test1")
TimeX = CDate(Range("d2").Value)
TimeY = CDate(Range("ad2").Value)
Total = TimeValue(TimeY) - TimeValue(TimeX)

Range("ag2").Value = Abs(Total * 24)
Range("ah2").Value = Abs(Total * 1440)
End With

End Sub

The following macro uses Column D to find the last row, and then loops through each row and places the results in Column AE...下面的宏使用 D 列查找最后一行,然后循环遍历每一行并将结果放在 AE 列中...

Sub valuedifference()

    Dim Total As Double
    Dim TimeX As Date
    Dim TimeY As Date
    Dim LastRow As Long
    Dim i As Long
    
    With ThisWorkbook.Sheets("Test1")
        LastRow = .Cells(.Rows.Count, "d").End(xlUp).Row
        For i = 2 To LastRow
            TimeX = CDate(.Range("d" & i).Value)
            TimeY = CDate(.Range("ad" & i).Value)
            Total = DateDiff("n", TimeY, TimeX)
            .Range("AE" & i).Value = Total
            .Range("AG" & i).Value = Format(Abs(Total), "#.##")
            .Range("AH" & i).Value = Format(Abs(Total), "#.##")
        Next i
    End With

End Sub

I'd strongly recommend using Range variables and offsets rather than assembling cell name references.我强烈建议使用Range变量和偏移量,而不是组装单元格名称引用。

Since the write back to the spreadsheet is a block of 3 cells, you can use an Array of the required values writing to the block to reduce spreadsheet updates.由于回写电子表格是一个包含 3 个单元格的块,因此您可以使用写入块的所需值的Array来减少电子表格更新。

One outstanding question for me is whether you want to capture the difference in days also, or just the time difference (regardless of date) as you do here?对我来说,一个悬而未决的问题是你是否也想捕捉天数的差异,或者只是像你在这里所做的那样捕捉时间差异(无论日期如何)?

Sub valuedifference()

Dim Total As Double
Dim TimeX As Date
Dim TimeY As Date
Dim LastD As Range
Dim DRange As Range
Dim ACell As Range

Set LastD = Sheets("Test1").Cells(Sheets("Test1").Cells.Rows.Count, 4).End(xlUp)
Set DRange = Range(Sheets("Test1").Range("D2"), LastD)
For Each ACell In DRange
    TimeX = CDate(ACell.Value)               ' from D column
    TimeY = CDate(ACell.Offset(0, 26).Value) ' from AD column
    Total = TimeValue(TimeY) - TimeValue(TimeX)
    ' Place results in AE rightward cells
    ACell.Offset(0, 27).Resize(1, 3).Value _
          = Array(Total, Abs(Total * 24), Abs(Total * 1440))
Next ACell

End Sub

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

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