รับวันที่ทั้งหมดระหว่าง 2 วันใน vba

ฉันเป็นมือใหม่ใน vba และฉันกำลังพยายามเข้า vba ทุกวันระหว่างวันที่ 2 วัน เช่น ฉันจะเรียกใช้ฟังก์ชันด้วยพารามิเตอร์ 01-01-2015 และ 15-01-2015 และฉันจะได้รับอาร์เรย์ส่งคืน ด้วยวันที่ที่เป็นไปได้ทั้งหมด เช่น :

01-01-2015
02-01-2015
03-01-2015
.....
15-01-2015

ฉันไม่พบคำตอบในฟอรัม ดังนั้นขอขอบคุณล่วงหน้าสำหรับความช่วยเหลือของคุณ


person user2443476    schedule 17.03.2015    source แหล่งที่มา
comment
ฟังก์ชันนี้มีจุดประสงค์เพื่อส่งคืนอาร์เรย์เป็นประเภทตัวแปร var ใน VBA หรือคุณกำลังพยายามส่งคืนไปยังแผ่นงานเพื่อประมวลผลฟังก์ชันดั้งเดิมเพิ่มเติมหรือไม่   -  person    schedule 17.03.2015
comment
คุณสามารถบรรลุผลเช่นเดียวกันโดยใช้ตัวกรองใน Excel ทั้งนี้ขึ้นอยู่กับการใช้งานและความต้องการ   -  person CustomX    schedule 17.03.2015
comment
ฉันต้องการคอลเลกชั่นที่มีวันที่ทั้งหมด เนื่องจากฉันจะนำไปใช้ในฟังก์ชัน vba อื่น   -  person user2443476    schedule 17.03.2015
comment
คุณสามารถแปลงวันที่เป็นแบบยาวและสร้างลูป (+1) และรับวันที่ทั้งหมดระหว่าง 2 วัน (แปลงเป็นวันที่อีกครั้ง)   -  person Arya    schedule 17.03.2015


คำตอบ (5)


คุณสามารถแปลงวันที่เป็นแบบยาวและสร้างลูป (+1) และรับวันที่ทั้งหมดระหว่าง 2 วัน (แปลงเป็นวันที่อีกครั้ง)

Sub Calling()
    Dim test
    test = getDates(#1/25/2015#, #2/5/2015#)
End Sub

Function getDates(ByVal StartDate As Date, ByVal EndDate As Date) As Variant

    Dim varDates()      As Date
    Dim lngDateCounter  As Long

    ReDim varDates(1 To CLng(EndDate) - CLng(StartDate))

    For lngDateCounter = LBound(varDates) To UBound(varDates)
        varDates(lngDateCounter) = CDate(StartDate)
        StartDate = CDate(CDbl(StartDate) + 1)
    Next lngDateCounter

    getDates = varDates

ClearMemory:
    If IsArray(varDates) Then Erase varDates
    lngDateCounter = Empty

End Function
person Arya    schedule 17.03.2015
comment
ขอบคุณสำหรับคำตอบของคุณ มันทำงานได้อย่างสมบูรณ์แบบ ยกเว้นวันที่สิ้นสุดไม่รวมอยู่ในคอลเลกชัน - person user2443476; 17.03.2015
comment
โอ้ แก้ไขสิ่งนี้: ReDim varDates(0 ถึง CLng(EndDate) - CLng(StartDate)) - person Arya; 17.03.2015

ฟังก์ชั่นรับวันที่ทั้งหมดจากช่วงที่กำหนด

Function GetDatesRange(dateStart As Date, dateEnd As Date) As Collection
    Dim dates As New Collection
    Dim currentDate As Date
    currentDate = dateStart
    Do While currentDate <= dateEnd
        dates.Add currentDate
        currentDate = DateAdd("d", 1, currentDate)
    Loop
    Set GetDatesRange = dates
End Function

ตัวอย่างการใช้งาน

Dim dateStartCell as Range, dateEndCell as Range
Dim allDates as Collection
Dim currentDateSter as Variant
Dim currentDate as Date
Set dateStartCell = ActiveSheet.Cells(3, 3)
Set dateEndCell = ActiveSheet.Cells(3, 6)
Set allDates = GetDatesRange(dateStartCell.Value, dateEndCell.Value)    
For Each currentDateSter In allDates
    currentDate = CDate(currentDateSter)
    'Do something with currentDate
Next currentDateSter
person cezarypiatek    schedule 19.11.2017

อาร์เรย์ 'sn' ที่มีวันที่ทั้งหมดตั้งแต่ 01-01-2015 ถึง 15-01-2015 Msgbox แนะนำเพื่อแสดงผลลัพธ์

Sub M_snb()
  sn = Evaluate("index(text(datevalue(""01-01-2015"")+row(1:" & DateDiff("d", CDate("01-01-2015"), CDate("15-01-2015")) & ")-1,""dd-mm-yyyy""),)")
  MsgBox sn(1, 1) & vbLf & sn(2, 1) & sn(UBound(sn), 1)
End Sub
person snb    schedule 17.03.2015

บางทีนี่อาจเป็น

Function udf_Array_of_Dates(dtSTART As Date, dtEND As Date, rDATEs As Range)
    Dim dt() As Date, r As Range, d As Long
    For Each r In rDATEs
        If r.Value >= dtSTART And r.Value <= dtEND Then
            d = d + 1
            ReDim Preserve dt(1 To d)
            dt(d) = r.Value
        End If
    Next r
    udf_Array_of_Dates = dt
End Function

หลักฐานและไวยากรณ์:

UDF สำหรับอาร์เรย์วันที่

person Community    schedule 17.03.2015

หากคุณเพียงต้องการพิมพ์วันที่ระหว่างวันที่สองวันใน Excel คำแนะนำของฉันคือให้คุณลองใช้โค้ดด้านล่าง

Sub DateFill()

Dim Start_Date As Date
Dim End_Date As Date
Dim Number_Of_Days As Integer


Start_Date = InputBox(prompt:="Enter the Start Date", Title:="Date Print", Default:="3/1/2013")
End_Date = InputBox(prompt:="Enter the End Date", Title:="Date Print", Default:="3/23/2013")

Range("A1").Value = Start_Date
'Range("B1").Value = End_Date
Range("A1").Select
Number_Of_Days = DateDiff("d", Start_Date, End_Date) ' Return Day

Number_Of_Days = Number_Of_Days + 1
'Range("C1").Formula = "=DATEDIF(A1, B1, ""D"") "


Selection.AutoFill Destination:=Range("A1:A" & Number_Of_Days), Type:=xlFillDefault
    Range("A1:A" & Number_Of_Days).Select


End Sub

ที่นี่คุณจะต้องหลีกเลี่ยงการใช้ Loop ที่ช่วยประหยัดเวลาในการดำเนินการ

person Swapnil Wankhede    schedule 17.03.2015