我正在从Microsoft Project导出Excel工作表,并尝试将导出的工作表导入Excel中的现有工作表。
它工作正常,但是,即使我在Excel的GUI中选择m/d/yy hh:mm AM/PM
并尝试格式化为日期,也会无法正确格式化字符串日期表示(存储为Format Cells
) 。字符串不会改变。
我该怎么办?我可以在导入子中放置代码吗?是否有更好的方法通过Project导出,以便日期格式正确?
这是我当前用于导入工作表的子文件(通过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。
答案 0 :(得分:3)
A1
的日期存储为字符串代码示例
Columns(1).NumberFormat = "m/d/yy hh:mm AM/PM"
Range("A1").Formula = Range("A1").Value
以上是一个示例,对于一个满是值的列,您必须遍历单元格并执行上述操作。
<强>截图强>