我有一個 VBA 問題,我試圖以每月的步驟回圈到一個日期范圍內,但我在“每月步驟”部分掙扎。
其實我能夠得到這個輸出:
13/08/2021
14/08/2021
15/08/2021
16/08/2021
17/08/2021
18/08/2021
我想要得到的更像是:
08/2021
09/2021
10/2021
11/2021
12/2021
01/2022
這是我的代碼:
aIndex = 1
For Each Cell In Range("F2", Range("F2").End(xlDown))
aIndex = aIndex 1
For J = Range("D" & aIndex) To Cell
Debug.Print J
Next J
Next Cell
“F”和“D”列只包含格式為“DD/MM/YYYY”的日期,我在“D”和“F”日期之間回圈。
提前致謝,
尼古拉斯。
uj5u.com熱心網友回復:
有很多方法可以做到這一點,但要盡量保持它與您擁有的相似,洗掉不需要的東西,并改用字典:
Sub test()
Dim sDate As String
Dim dict As Object
Dim Cell As Range
Set dict = CreateObject("Scripting.Dictionary")
For Each Cell In Range("F2", Range("F2").End(xlDown))
sDate = Format$(Range("D" & Cell.Row), "mm/yyyy")
If Not dict.Exists(sDate) Then
dict.Add sDate, 1
Debug.Print sDate
End If
Next
Debug.Print "Total: " & dict.Count
End Sub
如果您Debug.Print不完全是您所需要的,那么您可以在之后使用字典做一些事情。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/361277.html
上一篇:如何使用vba代碼添加類別總和?
下一篇:宣告整數陣列
