2

我正在从 Microsoft Project 导出 Excel 工作表,并尝试将该导出的工作表导入 Excel 中的现有工作表。

m/d/yy hh:mm AM/PM它工作正常,但是,即使我Format Cells在 Excel 的 GUI 中选择并尝试格式化为日期,也有字符串日期表示(存储为)格式不正确。字符串只是不会改变。

我能做些什么?我可以在我的导入子中放置代码吗?有没有更好的方法通过 Project 导出,以便日期的格式已经正确?

这是我当前用于导入工作表的子(通过项目保存):

Sub Import_from_Project()
    Dim wbSource As Workbook, wbDestination As Workbook
    Dim wsSource As Worksheet, wsDestination As Worksheet
    Dim DTAddress As String

    DTAddress = CreateObject("WScript.Shell").SpecialFolders("Desktop") & Application.PathSeparator

    Application.ScreenUpdating = False

    Set wbSource = Workbooks.Open(DTAddress & "Import.xlsx")
    Set wbDestination = ThisWorkbook

    Set wsSource = wbSource.Sheets(1)
    Set wsDestination = wbDestination.Sheets(6)

    'The range below is the data I'm copying
    'Only columns B and C contain the strings that I want to convert to date
    With wsSource
        .Range(.Range("A2"), .Range("C2").End(xlDown)).Copy wsDestination.Range("A3")
    End With

    'Pseudocode to loop through strings and convert to dates
    'With wsSource
    '    .Range(.Range("B2"), .Range("C2").End(xlDown)) .FORMAT TO DATE?

    wbSource.Close SaveChanges:=False
    ' wbDestination.Close SaveChanges:=True
    Application.ScreenUpdating = True

End Sub

我假设我必须使用 CDATE 函数,但我不确定它如何适合上面的伪代码:

Format(CDate(stringDateRepresentation), "m/yyyy")

编辑:如果它很重要,我正在使用 Office 2010。

4

1 回答 1

3
  1. 将列格式化为日期
  2. 然后使用此代码覆盖单元格。假设A1有一个存储为字符串的日期

代码示例

Columns(1).NumberFormat = "m/d/yy hh:mm AM/PM"
Range("A1").Formula = Range("A1").Value

以上是一个示例,对于一个充满值的列,您必须遍历单元格并执行上述操作。

截屏

在此处输入图像描述

于 2013-01-11T14:18:55.683 回答