0
我想寫一個VBA腳本,它將一個文件夾中的所有Excel文件導入到Access 2003中的表中,首先檢查它們是否有已被輸入或沒有輸入。那部分很好。我遇到的問題是清除一些在電子表格中未使用的公式,這會在Access嘗試導入範圍時造成困難。當按原樣運行代碼時,出現「用戶定義的類型未定義」錯誤。「用戶定義類型未定義」Excel範圍使用後期綁定在Access 2003中
由於我正在開發使用多個版本的Office的網站,因此無法使用早期綁定引用相同的庫,因此我使用了晚期綁定。問題代碼如下:
Private Sub Command2_Click()
'Declare Variables
Dim xlApp As Object
Dim xlBook As Object
Dim LSQL As String
Dim SkippedCounter As Integer
Dim ImportedCounter As Integer
Dim BUN As Long
Dim SubmitDate As Date
Dim LSQL2 As String
Dim LSQL3 As String
'Start counters for final notice
SkippedCounter = 0
ImportedCounter = 0
Dim myDir As String, fn As String
'Set directory for importing files
myDir = "U:\Five Star\Operations\restore\Surveys\My InnerView - 2010\Action plans\Action plans - input for DB\"
'Function for selecting files in folder
fn = Dir(myDir & "*.xls")
'Determine if there are files in side the folder
If fn = "" Then
MsgBox "Folder is Empty!"
Else
'Begin cycling through files in the folder
Do While fn <> ""
'Create new Excel Object
Set xlApp = CreateObject("Excel.Application")
'Make it appear on the screen while importing
xlApp.Visible = True
'Open the workbook at hand
Set xlBook = xlApp.Workbooks.Open(myDir & fn)
'Check to see if it has been imported already
If xlBook.Sheets("Action plan form").Range("A1").Value = "Imported" Then
'If it has been imported, add 1 to the counter, close the file and close the instance of Excel
SkippedCounter = SkippedCounter + 1
xlBook.Close
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
Else
'Otherwise, unprotect the worksheet
xlBook.UnProtect Password:="2010"
Dim c As Range
'Unhide worksheet needed and clean it up
xlBook.Sheets("Action plan DB data").Visible = True
xlBook.Sheets("Action plan DB data").Range("B10:O10").ClearFormats
xlBook.Sheets("Action plan DB data").Range("N11:N84").ClearFormats
For Each c In xlBook.Sheets("Action plan DB data").Range("DB_import")
If c.Value = "" Or c.Value = 0 Then c.Clear
Next c
...
代碼的其餘部分應該運行良好,它僅僅指剛與「範圍」的聲明,並通過它循環的問題。謝謝你的幫助!
當然Dim c As Object? – Fionnuala 2010-07-01 13:58:55
@Remou:我相信「As Object」並非嚴格要求,但是,是的,那就是效果。 – grammar31 2010-07-01 15:16:30