在工作表上输入日期数组将转换为美国格式

时间:2018-08-06 10:25:31

标签: excel vba date-format

我在VBA中有一组日期。如果我一次将它打印到纸张上,那一切就完美了。但是,当我将整个数组批量打印到一列时,日期格式从(dd / mm / yy)更改为(mm / dd / yy)。因此,5月7日转换为7月5日,而5月25日的格式未设置为工作表上的日期。

这是我的代码。

(另一个较小的问题:在打印到列时如何摆脱转置的需要?这段“伪代码”执行了30K次。)

Public Sub test05()
Dim dt As Date, fromDate As Date, toDate As Date
Dim dayCounter As Integer
Dim sheet As Worksheet
Dim dtArr() As Date
Set sheet = Sheets("Sheet1")
fromDate = DateSerial(2018, 8, 6)
toDate = DateSerial(2023, 8, 5)
sheet.Cells.ClearContents
dayCounter = 0
For dt = fromDate To toDate
    wd = Weekday(dt)
    If wd = 1 Or wd = 7 Then
        'skip weekends
        GoTo NextDayIteration
    End If
    dayCounter = dayCounter + 1

    ReDim Preserve dtArr(1 To dayCounter)

    dtArr(dayCounter) = dt

    'print the dates (one by one) to the sheet in column 1
    sheet.Cells(dayCounter, 1).Value2 = dt
NextDayIteration:
Next 'end of each day

'print all the dates array to the sheet in bulk, in column 2
Dim rng As Range
With sheet
    Set rng = .Range(.Cells(1, 2), .Cells(UBound(dtArr) - LBound(dtArr) + 1, 2))
End With

rng.Value2 = Application.Transpose(dtArr)

End Sub

1 个答案:

答案 0 :(得分:1)

请勿使用gautam1 <- sqldf( " SELECT ORG_OFFICIAL_NORM_NAME,ORG_IMMEDIATE_PARENT_SRC ,MAX(ORG_IP_SRC_MD_DATE),ROW_ID FROM OA_output where ROW_ID = 1 and ORG_IMMEDIATE_PARENT_SRC like '%exhibit21%' GROUP BY ORG_IMMEDIATE_PARENT_ORGID ORDER BY ORG_IMMEDIATE_PARENT_ORGID " ) Transpose。您可以改为执行以下操作:

Preserve