我已经成功创建了一个Access表,并使用VBA UDF将excel范围内的数据加载到该表中。
但是,当选择excel中列255(IU)之外存在的(小)范围时,不会创建访问表。
例如:范围A1:B500将作为Excel数据库范围使用,但范围JA3:JB100将无效。
问题:有没有办法克服这个限制或限制?
以下是我的代码片段。请提供任何建议。
Function upload2Access(dbPath As String, tabName As String, DataRange As Range) As String
Dim con As New ADODB.Connection
Dim connectionString As String
Dim filename As String
Dim sql, newTable As String
Dim callSheet As String
Dim callWbook As String
'Stop
' Only run funciton when Ctrl+Shift+R is selected
If refresh = False Then
upload2Access = "Press [Ctrl+Shift+R]"
GoTo Final
End If
' Caller Cell Information
callSheet = Application.Caller.Worksheet.Name
callWbook = Application.Caller.Worksheet.Parent.FullName
' Connect to Database File
filename = dbPath
connectionString = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" & filename
con.Open connectionString
' Delete rows of current table
sql = "DELETE * FROM " & tabName
On Error Resume Next
con.Execute sql
sql = "SELECT * INTO [" & tabName & "] " & _
"FROM [Excel 8.0;HDR=YES;DATABASE=" & callWbook & "].[" & callSheet & "$" & DataRange.Address(RowAbsolute:=False, columnabsolute:=False) & "]"
con.Execute sql
' Update Access Database
sql = "INSERT INTO " & tabName & " " & _
"SELECT * FROM [Excel 8.0;HDR=YES;DATABASE=" & callWbook & "].[" & callSheet & "$" & DataRange.Address(RowAbsolute:=False, columnabsolute:=False) & "]"
con.Execute sql
' Close Database Connection
con.Close
Set con = Nothing
' Return Update Information to Cell
upload2Access = "success_" & Time & "_" & Date
Final:
End Function