[英]VBA value larger than x days x hours
我正在嘗試為從網站中提取的數據制作 VBA 腳本。 現在我正在努力隱藏大於一定天數的行。
我掙扎的原因是因為格式如下:
我一直在瀏覽互聯網,但我找不到任何可以在沒有分隔符的情況下使用值的代碼。 通常時間有一個“:”分隔符。 我試圖將其排除在外,但我的代碼無法正常工作。
我的代碼如下所示:
Dim BooBoo As Long, TheEnd As Long
'Tell me where is the end!
TheEnd = Cells(Rows.Count, "A").End(xlUp).Row
'Play it where
With Sheets("Sheet2")
For BooBoo = 2 To TheEnd
'Anything in column E that is older than 3 days, has to be hidden
If .Cells(BooBoo, "E").Value > "3d 0h 0m 0s" Then
.Rows(BooBoo).EntireRow.Hidden = True
If Rows(BooBoo).Hidden = True Then Rows(BooBoo).EntireRow.Delete
End If
Next
End With
我覺得我以錯誤的方式接近這個問題。 希望有人可以幫助我。
問候,塞巴斯蒂安
嘗試使用此 function,請:
Private Function TotalReference(strTime As String) As Double 'hours
Dim arrD As Variant
arrD = Split(strTime, " ")
TotalReference = Split(arrD(0), "d")(0) * 24 + Split(arrD(1), _
"h")(0) + Split(arrD(2), "m")(0) / 60 + Split(arrD(3), "s")(0) / 3600
End Function
您可以通過以下方式對其進行測試:
Sub testHideBiggerDate()
Dim x As String
x = "3d 2h 20m 50s"
Debug.Print TotalReference(x)
End Sub
在您的代碼中,您可以通過以下方式使用它:
If TotalReference(.Cells(BooBoo, "E").Value) > TotalReference("3d 0h 0m 0s") Then
除此之外,您的代碼首先隱藏該行,然后檢查它是否被隱藏並刪除它。 為什么要先隱藏它……? 並且使用Rows(BooBoo).Hidden = True
將檢查活動工作表的行,而不是您的代碼處理的行,如果它沒有被激活。 您還應該使用: .Rows(BooBoo).Hidden = True
。
您的代碼希望這樣:
Sub compareDatevalues()
Dim BooBoo As Long, TheEnd As Long
TheEnd = Cells(Rows.count, "A").End(xlUp).row
With Sheets("Sheet2")
For BooBoo = 2 To TheEnd
'Anything in column E that is older than 3 days, has to be hidden
If TotalReference(.Cells(BooBoo, "E").Value) > TotalReference("3d 0h 0m 0s") Then 'or > 72
.Rows(BooBoo).EntireRow.Hidden = True
If .Rows(BooBoo).Hidden = True Then Rows(BooBoo).EntireRow.Delete 'why to delete it in two phases?
End If
Next
End With
End Sub
您可以很容易地從字符串中提取天數,然后比較整數。 我假設您不想考慮時間等。
Dim BooBoo As Long, TheEnd As Long
'Tell me where is the end!
TheEnd = Cells(Rows.Count, "A").End(xlUp).Row
'Play it where
With Sheets("Sheet2")
For BooBoo = 2 To TheEnd
'Anything in column E that is older than 3 days, has to be hidden
If Mid(.Cells(BooBoo, "E"), 1, WorksheetFunction.Find("d", .Cells(BooBoo, "E").Value) - 1) * 1 > 3 Then
.Rows(BooBoo).EntireRow.Hidden = True
If Rows(BooBoo).Hidden = True Then Rows(BooBoo).EntireRow.Delete
End If
Next
End With
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.