Warm tip: This article is reproduced from stackoverflow.com, please click
excel sql vba

Wrong data types when loading sql query with vba in excel

发布于 2020-03-29 21:03:47

I have a SQL Query, which I want to use in VBA to load the data into Excel, the problem is that when the load is done the data in Excel is with the wrong type. For example, where it needs to be a date, it remains an integer, where an integer is a date. Is there any way to fix this? Example my query:

sql = " SELECT DATA, ITEM, COMPANY, SALES FROM SALES "
Questioner
Luciano Amaro
Viewed
26
Naveen Arora 2020-01-31 19:03

You will have to format cells as per date format. Also ensure that values are correct in your query output.

Sheet1.Range("A2", "A100").NumberFormat = "yyyy-mm-dd"