+ Reply to Thread
Results 1 to 9 of 9

Trouble getting excel controls from a worksheet

  1. #1
    Registered User
    Join Date
    08-11-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2010
    Posts
    9

    Trouble getting excel controls from a worksheet

    Hello all,
    I have a legacy excel book which has multiple controls on a sheet which I am currently trying to get the data off into an Access form.

    Multiple users enter data onto this form (template) which get saved as separate excel spreadsheets in a common folder.

    My job is to gather statistics from these sheets. Up until now I've been manually entering the data into an Access database, what I'm trying to now do is automatically load each workbook, get the data from the relevant sheet and then populate the relevant fields in the access form.

    So far I've successfully opened the first workbook and read the cell data. Now I'm trying to get the data off the controls.

    First I tried using:
    Please Login or Register  to view this content.
    And I get a data type mismatch error at the for each statement if I DIM sh as shape,
    or an error at sh.value if I leave sh as a variant.

    I've created a for i=1 to .shapes.count loop, with the second statement set sh= shapes(i) but that fails at the set statement with a type mismatch error also.

    Now the person who set up the worksheet just created checkboxes called ch1 to ch21, I was hoping to
    get the caption and value for each checkbox one at a time in a loop and use a select case statement to place the data on the access form but have head butting all day so far and would prefer an expert start me along the right track as I can't now see the wood for the trees!

    Thanks for your help...

    ps: Here's my current attempt at reading the data. To put it in context, this code is running in access, xlWS is a link to the worksheet which contains the controls

    Please Login or Register  to view this content.

  2. #2
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: Trouble getting excel controls from a worksheet

    With the code you provided, two things:

    1. Can we assume that preceding
    Please Login or Register  to view this content.
    you used
    Please Login or Register  to view this content.
    2. With what you provided, we have no way of knowing if you properly set the xlWS worksheet object. You would have the following common options:
    Please Login or Register  to view this content.
    or
    Please Login or Register  to view this content.
    Perhaps it was the Noid who should have avoided me...
    If you are satisfied with my solution click the small star icon on the left. Thanks
    1. Make a copy of your workbook and run the following code on your copy (just in case)
    2. With excel open, press ALT+F11 to open the Visual Basic Editor (VBE). From the "Insert" menu, select "Module".
    3. Paste the code from above into the empty white space. Close the VBE.
    4. From the developer tab, choose "Macros", select the Sub Name, and click "Run".

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Trouble getting excel controls from a worksheet

    Hello bigtrev2,

    My first question is what is Excel 2012 and who makes it? I am only familiar with Office 2010, 2011, and 2013.

    You could have saved yourself a lot of trouble by posting the workbook in question.
    You should post a copy of your workbook. This will provide all the information needed to develop and test any solutions.

    To Post Your Workbook
    1. At the bottom right of the Reply window, Click the button Go Advanced
    2. At the top of the Your Message Window, in the first row of icons, Click the Paperclip icon.
    3. Click the Add Files button at the top right in the dialog box. This displays the File Manager dialog.
    4. Click the Select Files button at the bottom middle of the dialog.
    5. In the new window Find the file you want to upload, select it, and Click Open.
    6. You will now be back in the File Manager dialog. Click the bottom Middle button Upload File.
    7. Wait until the file has completely uploaded before you exit the File Manager dialog.

    File Manger Picture
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Registered User
    Join Date
    08-11-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Trouble getting excel controls from a worksheet

    Firstly Leith,
    Sorry about the Excel version, I guess I just entered 2012 when I set my profile up last year, I'm actually using Excel 2010. I apologize for the mistake and I've corrected my profile now.

    I might be able to bend some rules and post a blank copy of the excel template if that is needed, I'd prefer not to - I'm fairly sure that ICT dept at work would find a reason to frown on that.
    I'm not sure if it is that helpful, as I stated above, this code runs from an Access database, and I definitely can't post that online, that's a offense which could result in me looking for a new job...

    Anyway, here is the code fragment that sets up the xlWS variable. As I'm trying to work out what is going on I've gradually explicitly referenced it again through the code so that I can try and rule out that as an issue.

    Please Login or Register  to view this content.
    I'm fairly sure that because this is the first time I've tried to get data from controls using VBA, there is something that I don't understand about referencing the shapes collection and type casting the resultant shape to a control so that I can get to the properties and methods of the various controls used.

    Basically, can I reference WS.shapes(i) and expect to get a shape or do I need to do something with a shaperange to get down to a specific shape?

    Otherwise I guess that I could explicitly reference each control via the worksheet somehow, perhaps something like

    Please Login or Register  to view this content.
    but I guess I am trying to do something that is a bit more elegant as I'd need to manually track each control down on the excel spreadsheet (there are over 21 checkboxes and more than a dozen other controls I need to read and handle.)

    Anyway, thanks for trying to assist, I hope I've provided enough info now.

    Let me know if you really need a copy of the excel spreadsheet, if you do, I guess I'll just have to dummy up a sheet with a few controls on it.

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Trouble getting excel controls from a worksheet

    Hello bigtrev2,

    I understand your reasons. can you tell me if the controls are Forms Controls or ActiveX Controls on the worksheet?

  6. #6
    Registered User
    Join Date
    08-11-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Trouble getting excel controls from a worksheet

    each control has been embedded?? into the worksheet.

    When I click on a control, I see a formula in the formula bar that says (all the checkboxes have this formula)

    Formula: copy to clipboard
    Please Login or Register  to view this content.

    Could that be a source of the problem?

    I'm guessing/assuming that they are Form controls based on the EMBED argument, but is there any other way to check, given that I didn't create this template in the first place?

    I should state that this workbook was created at least 10 years ago, so I'm not sure which version of excel it was written in.

    The other thing is that I've tried to explicitly reference the checkbox by using
    Please Login or Register  to view this content.
    and get an error, "method or data member not found"
    Last edited by bigtrev2; 05-03-2013 at 06:44 AM.

  7. #7
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: Trouble getting excel controls from a worksheet

    This will pass the name and value of all Active X Checkboxes (and I included some other types just in case), by worksheet, to array, which you can then repurpose for Access as applicable. If you really just want checkbox values only, simplify the If/Then by removing the additional control types from consideration.

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    08-11-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Trouble getting excel controls from a worksheet

    Thanks for your suggestion,

    So I've tried it and I loop through the oleControls and when it gets to the with oleControl section it fails with an "Object doesn't support this property or method" error (438).

    I'm going to ask a stupid question: have you got this working from access? If so, I wonder if there is another reference I need to enable in access to make this work.

    Thanks for your help so far.

  9. #9
    Registered User
    Join Date
    08-11-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Trouble getting excel controls from a worksheet

    OK Guys I worked out the problem!!

    If you look at the following code, i needed to used the .Object property to get to the .Value of the control.

    Thanks for your help!!

    Trev

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1