Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 883495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:35:57+00:00 2026-05-15T12:35:57+00:00

I am trying to write a VBA script which imports all of the Excel

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T12:35:58+00:00Added an answer on May 15, 2026 at 12:35 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a VBA script in Excel 2003 (not my choice
I'm trying to write a VBA script that finds all embedded (.docx) files within
I am trying to write some vba code in Excel to query a table
I' trying to write an xml file from Excel VBA using Microsoft XML 6.0.
I'm trying to write an R-Excel vba addin and am having trouble using GetArrayToVBA
I am trying to write a VBA Script to format a list of several
My current knowledge: If you are trying to write text files in vbscript /
I'm am trying to write vba code to fill out web-forms and click buttons
I'm trying to write a vba macro for a group tha has one workbook
I'm trying write a query to find records which don't have a matching record

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.