【问题标题】:Importing .csv Files into Excel and reformatting Date Columns Using VBA将 .csv 文件导入 Excel 并使用 VBA 重新格式化日期列
【发布时间】:2014-05-19 18:52:32
【问题描述】:

previous question 中,我询问了一种使用 Excel 使用 VBA 导入 .csv 文件的方法。我收到了有关使用 VBA 打开 .csv 文件的有用答案,但我担心使用此方法会导致日期格式出现问题,as a commenter on another of my questions mentioned。考虑到这一点,是否有与 Dan 用于导入文件的方法类似的方法?我知道录制的宏通常很笨拙,所以我想知道如何改进下面的标准代码之类的东西。

With ActiveSheet.QueryTables.Add(Connection:= _
    "FAKENAME.csv" _
    , Destination:=Range("$A$1"))
    .CommandType = 0
    .Name = "FAKENAME"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .TextFilePromptOnRefresh = False
    .TextFilePlatform = 65001
    .TextFileStartRow = 1
    .TextFileParseType = xlDelimited
    .TextFileTextQualifier = xlTextQualifierDoubleQuote
    .TextFileConsecutiveDelimiter = False
    .TextFileTabDelimiter = False
    .TextFileSemicolonDelimiter = False
    .TextFileCommaDelimiter = True
    .TextFileSpaceDelimiter = False
    .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, _
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 _
    , 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1)
    .TextFileTrailingMinusNumbers = True
    .Refresh BackgroundQuery:=False
End With

更新:

如果我选择不导入,日期会出现这样的问题:

     DateTime               Format

     05/11/2014 3:22        Custom
4/27/2014 9:53:01 AM        General
     11/22/2013 8:29:35 AM  Custom
     05/11/2014 8:26        Custom
1/17/2014 12:28:24 PM       General
     05/11/2014 3:22        Custom

虽然这可以在我导入时解决,但如果我只是打开文件尝试手动更改整个列的日期格式实际上并不会更改格式。

【问题讨论】:

  • CSV 中的日期格式是否一致?还是您的宏需要先弄清楚日期格式?
  • @CodeJockey 它们都是 MDY 格式,有些还包含时间。我不认为将他们都视为有时间的问题,不是吗?
  • 我之前曾与讨厌的日期纠缠不清,但不可否认的是,我最终还是使用了 ruby​​ 来清理 CSV。您能否通过一些示例向我们展示 MDY 字段的外观?
  • @Dan 当然,当我在导入期间也未能更改日期时,我会告诉你最终的错误是什么。

标签: vba excel


【解决方案1】:

我也不太可能使用这种 QueryTables 方法,正如 @Dan-Wagner 在您之前的帖子中提到的那样。我会使用 Microsoft Scripting Runtime 库中的 Text Stream 对象之类的东西来解析文件。您必须将其添加为参考(工具-->参考 + 查找并从列表中选择它)请参阅下面的小示例,了解如何使用它:

Dim fileSys as New FileSystemObject 'the New keyword is important here!
Dim TS as TextStream
Dim txt as String, sp() as String   'first one is one string, second is an Array
Dim i as Integer

Set TS = fileSys.OpenTextFile("C:\myinputfile.csv")
i = 1
Do While Not TS.AtEndOfStream
    txt = TS.ReadLine               'Read one whole line of data
    sp = Split(txt, ",")            'Split by commas into "Cells" (kinda)
    Sheets(2).Cells(i, 1) = sp(0)     'Note, sp() is "Zero-Based" but ranges are 1 based
    Sheets(2).Cells(i, 2) = sp(1)     'So you'll always access sp onelower than myOutputRange
    '
    '
    '
    i = i + 1
Loop

请注意,我将您的输出分配给第二个工作表,但您可以在此处使用任何范围。

MSDN 文本流对象:http://msdn.microsoft.com/en-us/library/aa242724%28v=vs.60%29.aspx 引用库:How do I use FileSystemObject in VBA?

希望这会有所帮助!

【讨论】:

  • 您知道为什么我会在 fileSys 上收到“未定义用户定义的类型”错误吗?
  • 您需要告诉宏在哪里可以找到对象的定义。您可以通过单击工具--> 引用并从列表中选择“Microsoft Scripting Runtime”来添加永久引用。或者,您可以Dim fileSys As Object 并调用Set fileSys = CreateObject("Scripting/FileSystemObject") 而不是As New FileSystemObject
  • 谢谢!现在唯一的问题(这可能是微不足道的)是如果我尝试将代码作为子例程引入,并且如果我不包含 Sub() End Sub 它会打开“子未定义错误”宏浏览器。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-05
  • 1970-01-01
  • 2018-03-28
  • 1970-01-01
  • 2014-12-28
  • 2014-07-20
  • 1970-01-01
相关资源
最近更新 更多