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 8337741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:23:04+00:00 2026-06-09T04:23:04+00:00

I have trouble with some code I just wrote. Basically, this code is inside

  • 0

I have trouble with some code I just wrote. Basically, this code is inside a dialog-box style form which is opened by the main form.

This code has two subs:
The first is the OnLoad method which discreetly open an Excel workbook to build a list of its worksheets.
The second sub is the OnClick() method of a button on that dialog box which returns two values and does a bit of error checking.

For some reason the Sheets() method fails sometimes and executing exactly the same steps right after it failed may actually work. I have used the ‘local variables’ window as well as the spy thing to watch that the values actually make sense and they do.
Any hint as to why this may happen ?

Also, this would not be a big deal if this database was in the hands of experienced users but we cannot assume any knowledge at all of Access by the users in this case (not even closing and reopening the form).

Option Compare Database
Option Explicit

Dim xlApp As Excel.Application
Dim xlWb As Excel.Workbook

Private Sub Form_Open(Cancel As Integer)
    Dim xl_sub_ws As Excel.Worksheet
    Set xlApp = New Excel.Application
    With xlApp
        .Visible = False
        Set xlWb = .Workbooks.Open(Forms("MenuGeneral")("CheminFichierImport").Value)
    End With

    For Each xl_sub_ws In xlWb.Worksheets
        lst_sheets.AddItem (xl_sub_ws.Name)
    Next xl_sub_ws
End Sub

Private Sub renvoyer_valeur_Click()
    Dim ret As VbMsgBoxResult

    If lst_sheets.Value <> "" Then
        Forms("MenuGeneral")("worksheet_name").Value = lst_sheets.Value
        Forms("MenuGeneral")("nb_colonnes").Value = Sheets(lst_sheets.Value).UsedRange.Columns.Count

        If Forms("MenuGeneral")("nb_colonnes") < 4 Then
            ret = MsgBox("La feuille à importer contient moins de 4 champs de la clé comptable." & _
                    "Souhaitez-vous procéder ainsi (oui) ou choisir une autre feuille (non) ?", vbYesNo)
            If ret = vbNo Then
                Forms("MenuGeneral")("nb_colonnes").Value = ""
                Exit Sub
            End If
        End If

        xlWb.Close
        xlApp.Quit
        DoCmd.Close acForm, "dlg_modal_feuille", acSaveNo
    Else
        MsgBox "S'il-vous-plaît choisir la feuille source"
    End If
End Sub

Many thanks in advance.

  • 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-06-09T04:23:05+00:00Added an answer on June 9, 2026 at 4:23 am

    I’ll suggest Worksheets instead of Sheets.

    Worksheets is a collection which exists in a workbook. Qualify Worksheets with the object variable (xlWb?) for the workbook.

    Forms("MenuGeneral")("nb_colonnes").Value = _
        xlWb.Worksheets(lst_sheets.Value).UsedRange.Columns.Count
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having some trouble with my code. Okay so I have an ASPx
I have some troubles with positioning my label/password field. With this code they both
I have some troubles with receiving data on server side. Here goes the code:
I have a ASP.NET MVC site using Membership Provider. I have trouble testing some
I have some trouble when I try to map object to int . My
I have some trouble with my trac installation (version 11.4). What should I do
I have some trouble with jquery ui-events: In my application, there are some sliders.
I have some trouble achieving adequate real-time performance from my application and wondering if
I'm have some trouble with the fulltext CONTAINS operator. Here's a quick script to
i seem to have some trouble installing autopy.h https://github.com/msanders/autopy/#introduction i already tried the installation

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.