VBScript formatting dd/mm/yyyy the values contained in cell Excel file [duplicate]

时光毁灭记忆、已成空白 提交于 2020-04-07 02:26:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!