I am trying to get data more easily usable in access when I import from excel. the report is from a webpage and is downloaded in an excel spreadsheet format. the spreadsheet has a heading that takes up the first 9 rows and a footer that is the last 3 rows. I had some problems with the import also but I get the data starting with row 10 and the footer is just 3 rows with some text. I got the import to work with this sample from googling.
this gets me the data that I need to use, but dates are not formatted to be able to use in a query. I tried to convert in a query but cant get it right yet. here is what I tried.
this code might work but data in the date field "f16" is not always a date. it sometimes has a dot "." or sometimes blank. this puts me into a datatype mismatch and takes forever to get to debug. I don't know how to get this working. can anyone help me?
correct dates in the imported report are formatted like this. "14 JAN 2014" with spaces between date parts. because it imports as a text field it cannot be used for query. I tried to change the import table to use it as a date field but then it does not import. so I am pretty sure I just need to convert it to a usable date with a query. unless anyone has a better idea?
Code:
Sub ImportDataFromRange()
' Assign the Excel Object
Dim excelapp As Object
Set excelapp = CreateObject("excel.application")
' Assign the workbook
Dim wb As Object
Set wb = excelapp.Workbooks.Open("C:\Users\me\Desktop\plan\20140114.xlsx")
' Assign the result of your CountA function used in the next line
Dim numberofrows As Integer
numberofrows = 9
' Get the bottom-most row number needed to complete our dynamic range address
numberofrows = 9 + excelapp.Application.CountA(wb.worksheets("Sheet1").Range("A10:y2000"))
' Delete any previous access table, otherwise the next line will add an additional table each time it is run
'DoCmd.DeleteObject acTable, "WebRpt"
' Import data from Excel using a range that now knows where bottom row number is
DoCmd.TransferSpreadsheet acTable, acSpreadsheetTypeExcel12Xml, "WebRpt", "C:\Users\me\Desktop\plan\20140114.xlsx", False, "Sheet1!A10:y" & numberofrows
' Close and clean
wb.Close
Set wb = Nothing
excelapp.Quit
Set excelapp = Nothing
End Sub
Code:
Public Function ConvertMyStringToDateTime(strIn As String) As Date
If (strIn = ".") Then
strIn = "0"
Else
ConvertMyStringToDateTime = CDate(Mid(strIn, 1, 2) & Mid(strIn, 4, 3) & Mid(strIn, 8, 4))
End If
End Function
correct dates in the imported report are formatted like this. "14 JAN 2014" with spaces between date parts. because it imports as a text field it cannot be used for query. I tried to change the import table to use it as a date field but then it does not import. so I am pretty sure I just need to convert it to a usable date with a query. unless anyone has a better idea?