问题
I use this code for import in XLSX Excel file the TXT text file :
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
objExcel.DisplayAlerts = False
objExcel.Workbooks.OpenText strTXTfile, , , xlDelimited, , , , , , , True, ";"
Set wb = objExcel.ActiveWorkbook
objExcel.Sheets(1).Columns("H:H").NumberFormat = "dd/mm/yyyy"
wb.SaveAs strXLSfile, xlOpenXMLWorkbook, , , , False
wb.Close
objExcel.Quit()
Set ObjExcel = Nothing
But I have problem on formatting dd/mm/yyyy the values contained in H cell.
On TXT text file the value of H cell are formatted dd/mm/yyyy hh:mm.
On XLSX Excel file I need only dd/mm/yyyy.
I have tried without success this code :
objExcel.Sheets(1).Columns("H:H").NumberFormat = "dd/mm/yyyy"
But in XLSX Excel file I see always the formatted dd/mm/yyyy hh:mm
I need delete hh:mm on the cell.
Please, can you help me?
Thank you in advance for any help.
Edit #01
回答1:
Try inserting a column to the right of H, splitting H on space and then deleting the new col I.
ws = wb.sheets(1)
ws.Columns("I:I").Insert(Shift:=-4161, CopyOrigin:=0)
ws.Columns("H:H").TextToColumns(Destination:=ws.Range("H1"), DataType:=1, Space:=True)
ws.Columns("I:I").delete
回答2:
Change:
objExcel.Sheets(1).Columns("H:H").NumberFormat = "dd/mm/yyyy"
to...
wb.Sheets(1).Columns("H:H").NumberFormat = "dd/mm/yyyy"
It might be easier to spot if you did away with the object references...
With CreateObject("Excel.Application")
.Visible = False
.DisplayAlerts = False
.Workbooks.OpenText strTXTfile, , , xlDelimited, , , , , , , True, ";"
With .ActiveWorkbook
.Sheets(1).Columns("H:H").NumberFormat = "dd/mm/yyyy"
.SaveAs strXLSfile, xlOpenXMLWorkbook, , , , False
.Close
End With
.Quit
End With
来源:https://stackoverflow.com/questions/60862910/vbscript-formatting-dd-mm-yyyy-the-values-contained-in-cell-excel-file