【发布时间】:2021-08-31 09:46:54
【问题描述】:
我有一系列格式相同的电子表格,我需要将它们导入访问数据库。不幸的是,电子表格数据不是表格形式,所以我需要导入一堆特定的单元格。
我将特定单元格读入变量并构造一个查询以将行插入表中。
当单元格包含等于错误的公式时,代码将失败。为了避免错误,我必须插入 Null 而不是错误值。如何插入 Null 而不是错误值?
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
【问题讨论】:
-
精简发布到 stackoverflow 的源代码是个好主意。它使人们更容易帮助您。
-
这样做是为了避免难以发现的错误:当返回值未分配给某物时,不要将括号放在方法的参数周围。并尽量避免嵌套 With 语句 - 即在这种情况下将工作簿分配给一个变量。
-
您的示例将始终只插入一行,因为 DoCmd.RunSQL 在循环之外。
-
是的,抱歉打错了,我在进行故障排除时移动了 Wend。
-
还有一件事。这将 A 声明为 Variant,B 声明为 Long “Dim A, B As Long”。如果您希望它们都是 longs ""Dim A As Long, B As Long",则必须为每个变量指定类型。