excel cell date格式

时间:2011-09-16 09:23:50

标签: excel vba csv

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

If IsDate(Cells(r, c)) Then

不幸的是,程序现在将一个带有“2A”的单元格作为日期解释(即使我已经检查了excel中的格式是文本),将其导出为“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

4 个答案:

答案 0 :(得分:4)

问题出现是因为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"

答案 1 :(得分:1)

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

  1. 变体数组比范围
  2. 更有效
  3. 它处理“,”内部cels,当前代码将此视为分割单元格内容的标志
  4. 当用两个短字符串连接长字符串时,最好先将两个短字符串连接在一起(使用paretheses),否则将长字符串连接两次
  5. 下面的示例确实转换了您不想遵循的列和行

    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
    

答案 2 :(得分:1)

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

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 可以避免您遇到格式

时遇到的所有问题

(我还建议阅读此help on writing loops with Excel ranges。这意味着您可以定义与您的表相关的行,列,而不是绝对地址或选择)

答案 3 :(得分:0)

这对日期格式

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

UV是列,newrow是变量

相关问题