Print dates in proper format

╄→гoц情女王★ 提交于 2020-01-05 10:37:18

问题


I have an excel workbook with many dates.

  • On the worksheet they are in a format of "mm/dd/yy hh:mm"
  • What I see in the white box above the sheet is in the format of "dd/mm/yy hh:mm"

I need to print the worksheets into text files, but when I do so, it's printing in the format of dd/mm/yy hh:mm, Instead of mm/dd/yy hh:mm.

Here's the code:

         For i = 1 To LastRow
            For j = 1 To LastCol
                If j = LastCol Then
                    DataLine = DataLine + Trim(Cells(i, j).Value)
                Else
                    DataLine = DataLine + Trim(Cells(i, j).Value) + vbTab
                End If
            Next j

            If (DataLine <> "") Then
                Print #FileNum, DataLine
                DataLine = ""
            End If
        Next i

Does any one has an idea how to print the sheets in the format I need?


回答1:


You can use the Format function to customize the text formatting of dates.

I escaped the forward slash / because otherwise it is interpreted as the date separator and "The actual character used as the date separator in formatted output is determined by your system settings" (from documentation). With escaping, it should always output a / character.

Format(Cells(1, 1).Value, "MM\/dd\/yy hh:mm")



回答2:


You could use Format(Cells(i,j), "mm/dd/yy hh:mm") to do this

Or what might work better is you could use

Cells(i,j) = Application.WorksheetFunction.Text(Now(), "dd/mm/yy hh:mm")

This would return a text variable instead of datetime.



来源:https://stackoverflow.com/questions/25160717/print-dates-in-proper-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!