I have a series of identically formatted spreadsheets that I need to import into an access db. Unfortunately the spreadsheet data isn't in tabular form so I need to import a bunch of specific cells.
I read specific cells into variables and construct a query to insert rows into a table.
The code fails, when a cell contains a formula that equates to an error. To avoid the error, I must insert Null instead of the error value. How do I insert Null instead of the error value?
Public Function ImportSheet()
Dim xl As Object
Dim jobno, Address, PM As String
Dim EDate As Date
Dim SID As String, SM, SDepth, SCon As String
Dim SDate As Date
Dim Sby, SDesc, Tby, Inc, Crack, Crumb As String
Dim AD1, AD2, AD3, AL1, AL2, AL3 As Double
Dim SHCID As String
Dim SHCMass, ILength, IDiam, M0, M1, M2, M3, M4, M5, L0, L1, L2, L3, L4, L5, MC0, MC1, MC2, MC3, MC4, MC5 As Double
Dim ST0, ST1, ST2, ST3, ST4, ST5, SW0, SW10, SW30, SW1h, SW21h, SW24h, SWih, Esw As Double
Dim BSWCID As String
Dim BCMass, BIM, BFM As Double
Dim ASWCID As String
Dim ACMass, AIM, AFM, MCI, MCISw, MCFSw, SFS, WD, DD, ISS As Double
Set xl = CreateObject("Excel.Application")
Dim xfileName As Variant
xfileName = Dir("C:\Users\username\Desktop\Database\Sheets\*.xls")
DoCmd.SetWarnings (False)
On Error Resume Next
While xfileName <> ""
With xl.Workbooks.Open(fileName:="C:\Users\username\Desktop\Database\Sheets\" & xfileName)
With .Sheets("Working Sheet")
jobno = .Cells(3, "G").Value
Address = .Cells(3, "C").Value
PM = .Cells(2, "G").Value
EDate = .Cells(4, "C").Value
SID = .Cells(5, "C").Value
SM = .Cells(6, "C").Value
SDepth = .Cells(7, "C").Value
SCon = .Cells(8, "C").Value
SDate = .Cells(5, "G").Value
Sby = .Cells(6, "G").Value
SDesc = .Cells(10, "C").Value
Tby = .Cells(4, "G").Value
Inc = .Cells(7, "G").Value
Crack = .Cells(8, "G").Value
Crumb = .Cells(9, "G").Value
AD1 = .Cells(13, "C").Value
AD2 = .Cells(14, "C").Value
AD3 = .Cells(15, "C").Value
AL1 = .Cells(13, "D").Value
AL2 = .Cells(14, "D").Value
AL3 = .Cells(15, "D").Value
SHCID = .Cells(12, "G").Value
SHCMass = .Cells(13, "G").Value
ILength = .Cells(14, "G").Value
IDiam = .Cells(15, "G").Value
M0 = .Cells(19, "C").Value
M1 = .Cells(20, "C").Value
M2 = .Cells(21, "C").Value
M3 = .Cells(22, "C").Value
M4 = .Cells(23, "C").Value
M5 = .Cells(24, "C").Value
L0 = .Cells(19, "D").Value
L1 = .Cells(20, "D").Value
L2 = .Cells(21, "D").Value
L3 = .Cells(22, "D").Value
L4 = .Cells(23, "D").Value
L5 = .Cells(24, "D").Value
MC0 = .Cells(19, "E").Value
MC1 = .Cells(20, "E").Value
MC2 = .Cells(21, "E").Value
MC3 = .Cells(22, "E").Value
MC4 = .Cells(23, "E").Value
MC5 = .Cells(24, "E").Value
ST0 = .Cells(19, "F").Value
ST1 = .Cells(20, "F").Value
ST2 = .Cells(21, "F").Value
ST3 = .Cells(22, "F").Value
ST4 = .Cells(23, "F").Value
ST5 = .Cells(24, "F").Value
SW0 = .Cells(29, "B").Value
SW10 = .Cells(30, "B").Value
SW30 = .Cells(31, "B").Value
SW1h = .Cells(32, "B").Value
SW21h = .Cells(33, "B").Value
SW24h = .Cells(34, "B").Value
SWih = .Cells(28, "G").Value
Esw = .Cells(34, "G").Value
BSWCID = .Cells(43, "F").Value
BCMass = .Cells(44, "F").Value
BIM = .Cells(45, "F").Value
BFM = .Cells(46, "F").Value
ASWCID = .Cells(43, "G").Value
ACMass = .Cells(44, "G").Value
AIM = .Cells(45, "G").Value
AFM = .Cells(46, "G").Value
MCI = .Cells(50, "D").Value
MCISw = .Cells(51, "D").Value
MCFSw = .Cells(52, "D").Value
SFS = Abs(.Cells(52, "E").Value)
WD = .Cells(53, "G").Value
DD = .Cells(54, "G").Value
ISS = .Cells(56, "G").Value
xl.Workbooks(xfileName).Close SaveChanges:=False
End With
End With
On Error GoTo 0
xfileName = Dir
Wend
xfileName = ""
Set xl = Nothing
DoCmd.SetWarnings (True)
Dim SQL As String
SQL = "INSERT INTO Results ( JobNo, Address, PM, EDate, SID, SM, SDepth, SCon, SDate, SBy, SDesc, TBy, Inc, Crack, Crumb, " _
& "AD1, AD2, AD3, AL1, AL2, AL3, SHCID, SHCMass, ILength, IDiam, M0, M1, M2, M3, M4, M5, L0, " _
& "L1, L2, L3, L4, L5, MC0, MC1, MC2, MC3, MC4, MC5, ST0, ST1, ST2, ST3, ST4, ST5, SW0, SW10, SW30, SW1h, SW21h, " _
& "SW24h, SWih, Esw, BSWCID, BCMass, BIM, BFM, ASWCID, ACMass, AIM, AFM, MCI, MCISw, MCFSw, SFS, WD, DD, Iss ) " _
& "SELECT '" & jobno & "', '" & Address & "', '" & PM & "', #" & EDate & "#, '" & SID & "', '" & SM & "', '" & SDepth & "', '" & SCon & "', #" & SDate & "#, " _
& "'" & Sby & "', '" & SDesc & "', '" & Tby & "', '" & Inc & "', '" & Crack & "', '" & Crumb & "', '" & AD1 & "', " _
& "'" & AD2 & "', '" & AD3 & "', '" & AL1 & "', '" & AL2 & "', '" & AL3 & "', '" & SHCID & "', '" & SHCMass & "', " _
& "'" & ILength & "', '" & IDiam & "', '" & M0 & "', '" & M1 & "', '" & M2 & "', '" & M3 & "', '" & M4 & "', " _
& "'" & M5 & "', '" & L0 & "', '" & L1 & "', '" & L2 & "', '" & L3 & "', '" & L4 & "', '" & L5 & "', '" & MC0 & "', " _
& "'" & MC1 & "', '" & MC2 & "', '" & MC3 & "', '" & MC4 & "', '" & MC5 & "', '" & ST0 & "', '" & ST1 & "', '" & ST2 & "', '" & ST3 & "', " _
& "'" & ST4 & "', '" & ST5 & "', '" & SW0 & "', '" & SW10 & "', '" & SW30 & "', '" & SW1h & "', '" & SW21h & "', " _
& "'" & SW24h & "', '" & SWih & "', '" & Esw & "', '" & BSWCID & "', '" & BCMass & "', " _
& "'" & BIM & "', '" & BFM & "', '" & ASWCID & "', '" & ACMass & "', '" & AIM & "', '" & AFM & "', '" & MCI & "', " _
& "'" & MCISw & "', '" & MCFSw & "', '" & SFS & "', '" & WD & "', '" & DD & "', '" & ISS & "'"
DoCmd.RunSQL SQL
MsgBox "Done"
End Function