繁体   English   中英

Excel单元格日期格式

[英]excel cell date format

这是我的第一个问题,所以我希望一切正确。 我破解了一些代码以从excel读取并为每个单元格输出带语音标记的CSV,无论是否为空白。 问题出在Excel将YYYY-MM-DD日期写入CSV时将其转换为MM-DD-YYYY的日期。 为了解决这个问题,我编写了以下if语句:

If IsDate(Cells(r, c)) Then

不幸的是,程序现在也将其中带有“ 2A”的单元格解释为日期(即使我已经检查了excel中的格式为text),也将其导出为“ 1899-12-30”。 我输入了一个非常快速和肮脏的子句来对此进行过滤

If Selection.Cells(r, c) = "2A" Then

但是我想知道是否还有一种更优雅的方法来转换仅采用YYYY-MM-DD或类似格式的日期格式。 为什么它认为A2是约会!? 完整代码如下:

For r = 1 To Selection.Rows.Count
    s = """"
    c = 1
    For c = 1 To Selection.Columns.Count
            If IsDate(Selection.Cells(r, c)) Then
                'ridiculous clause to get rid of A2 being treated as a date
                If Selection.Cells(r, c) = "2A" Then
                    s = s & Selection.Cells(r, c)
                Else
                    s = s & Format(Selection.Cells(r, c), "YYYY-MM-DD")
                End If
            Else
                s = s & Selection.Cells(r, c)
            End If
            If c = Selection.Columns.Count Then
                s = s & """" 'end of row
            Else
                s = s & """,""" 'mid row
            End If
    Next c
    a.writeline s
Next r

出现问题是因为2A被解释为2 AM

?isdate("2A")
True

?cdate("2A")
02:00:00 

处理方式取决于日期旁边列中的值的类型,您可以检查其长度或检查DateTime不包含日期部分时使用的默认日期。

if format$(cdate("2A"), "YYYY-MM-DD") = "1899-12-30" then ..dodgy..

或看看它的模式; if isdate(x) and x like "####-##-##"或者您可以将值转换为日期并检查其是否在适当的范围内。

您还可以重新格式化列本身Selection.NumberFormat = "yyyy-mm-dd"

除了Alex关于日期问题的详细说明之外,您可能希望查看http://www.experts-exchange.com/A_3509.html上的我的代码(全文中有说明),以优化您的总体方法,

  1. 变体数组比范围更有效
  2. 它处理cels内部的“,”,即您当前的代码会将其视为分割单元格内容的标志
  3. 将长字符串与两个短字符串连接在一起时,最好先将两个短字符串连接在一起(使用paretheses),否则将长字符串连接两次

下面的示例确实转置了您不希望遵循的列和行

Sub CreateCSV_FSO()
    Dim objFSO
    Dim objTF
    Dim ws As Worksheet
    Dim lRow As Long
    Dim lCol As Long
    Dim strTmp As String
    Dim lFnum As Long

    Set objFSO = CreateObject("scripting.filesystemobject")
    Set objTF = objFSO.createtextfile(sFilePath, True, False)

    For Each ws In ActiveWorkbook.Worksheets
        'test that sheet has been used
        Set rng1 = ws.UsedRange
        If Not rng1 Is Nothing Then
            'only multi-cell ranges can be written to a 2D array
            If rng1.Cells.Count > 1 Then
                X = ws.UsedRange.Value2
                'The code TRANSPOSES COLUMNS AND ROWS by writing strings column by column
                For lCol = 1 To UBound(X, 2)
                    'write initial value outside the loop
                    strTmp = IIf(InStr(X(1, lCol), strDelim) > 0, """" & X(1, lCol) & """", X(1, lCol))
                    For lRow = 2 To UBound(X, 1)
                        'concatenate long string & (short string with short string)
                        strTmp = strTmp & (strDelim & IIf(InStr(X(lRow, lCol), strDelim) > 0, """" & X(lRow, lCol) & """", X(lRow, lCol)))
                    Next lRow
                    'write each line to CSV
                    objTF.writeline strTmp
                Next lCol
            Else
                objTF.writeline IIf(InStr(ws.UsedRange.Value, strDelim) > 0, """" & ws.UsedRange.Value & """", ws.UsedRange.Value)
            End If
        End If
    Next ws

    objTF.Close
    Set objFSO = Nothing
    MsgBox "Done!", vbOKOnly

End Sub

一种更简单的方法是在单元格中使用文本值,例如

For r = 1 To Selection.Rows.Count
s = """"
c = 1
For c = 1 To Selection.Columns.Count
s = s & Selection.Cells(r, c).Text

        If c = Selection.Columns.Count Then
            s = s & """" 'end of row
        Else
            s = s & """,""" 'mid row
        End If
Next c
a.writeline s
Next r

使用.Text可避免格式遇到的所有问题

(我也建议您阅读有关使用Excel范围编写循环的帮助 。这意味着您可以定义与表相关的行,列,而不是绝对地址或选择)

这对于日期格式很有用

Range("U2:U" & newrow).NumberFormat = "mm/dd/yy"
Range("V2:V" & newrow).NumberFormat = "mm/dd/yy"

UV是列,而newrow是变量

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM