如何将string格式化为date – Microsoft Project导出到Excel?

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

它正常工作,但是,有stringdate表示(存储为m/d/yy hh:mm AM/PM ),即使我selectFormat Cells在Excel的GUI中尝试格式化为date。 string不会改变。

我能做什么? 有我可以放置在我的导入子代码? 有没有更好的方式通过项目导出,以便date格式正确?

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

 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。

  1. 将列格式化为date
  2. 然后用这个代码覆盖单元格。 假设A1有一个存储为string的date

代码示例

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

以上就是一个例子,对于一个满值的列,你必须循环遍历单元格并执行上述操作。

截图

在这里输入图像说明