/ / Zakres wyszukiwania Excel VBA w zależności od pory dnia - excel, excel-vba, vba

Zakres wyszukiwania Excel VBA w zależności od pory dnia - excel, excel-vba, vba

Mam kolumnę komórek w formacie „dd / mm / rr GG: MM: SS” i chcę policzyć liczbę komórek, które mają datę i godzinę w następującym zakresie, w zależności od pory dnia.

Jeśli bieżący czas wynosi <07:00, a następnie

Count Occurances in range between 07:00 the previous day and now

Jeszcze

Count occurances between 07:00 today and now

Koniec, jeśli

Z góry dziękuję.

Odpowiedzi:

0 dla odpowiedzi № 1

Ten kod będzie najprawdopodobniej działać dla Ciebie. Jest również dość prosta i łatwa do zmienienia, aby odwołać się do odpowiednich arkuszy i komórek, których potrzebujesz do obejrzenia. Mam nadzieję, że to pomogło!

Sub CountingNumberOfOccurancesWithingRangeDependingOnTime()
Dim curTime As Date
Dim preTime As Date
Dim todayTime As Date
Dim curHour As Integer
Dim counter As Integer

curTime = Now
curHour = Format(curTime, "HH")
preTime = curTime - 1

x = 1

If curHour < 7 Then
Do Until Sheet1.Cells(x, 1).Value = Empty
cellTime = Sheet1.Cells(x, 1).Value

If cellTime > preTime And cellTime < curTime Then
counter = counter + 1
End If
x = x + 1
Loop
Else
todayTime = Date & " " & "07:00:00"
Do Until Sheet1.Cells(x, 1).Value = Empty
cellTime = Sheet1.Cells(x, 1).Value

If cellTime > todayTime And cellTime < curTime Then
counter = counter + 1
End If
x = x + 1
Loop
End If

MsgBox counter "This isn"t necissary, but it just is here to show the number of cells counted



End Sub