tags:

views:

39

answers:

1

Hello,

I am trying to write a VBA script which imports all of the Excel files in a folder into a table in Access 2003, first checking if they have been imported or not. That part is fine. The issue I run into is clearing out some of the formulas that don't get used on the spreadsheet which causes difficulty when Access tries to import the range. when running the code as-is, I get an error "User-defined type not defined".

I am using late binding since I am developing for a site that uses multiple versions of Office and therfore can't reference the same library using early binding. The problem code is below:

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
                ...

The rest of the code should run fine, it jsut has an issue with the declaration of "range" and looping through it. Thanks for your help!

+1  A: 

Remove As Range from Dim c As Range and that will make c into an object. That way when it gets late-bound to a range you won't have any issues.

grammar31
Surely Dim c As Object?
Remou
@Remou: "As Object" is not strictly required, I believe, but yes, that's the effect.
grammar31