+ Reply to Thread
Results 1 to 8 of 8

VBA form controls Controlsource syntax

  1. #1
    Registered User
    Join Date
    11-04-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Office 365
    Posts
    15

    VBA form controls Controlsource syntax

    In a large (10,000 code lines) Excel project (XLS format under Excel 2007) I have several Userforms with textbox, option buttons and checkboxes linked to named cells via the ControlSource property.

    I have been unable, despite hours of Google searches, to find a clear definition of the syntax rules for this property, which is a string value providing a reference to the linked worksheet cell.

    Assuming the linked cell is named "name1" I have some controls with the ControlSource defined in the format "name1", and others defined as "=name1". Both methods seem to work, but I am not sure whether the "=" character is required or whether it should be omitted. I am setting the ControlSource value in the properties table in design mode and the quote marks are not typed in.

    Does anyone know what the correct syntax is?

    I also have some cases where two forms each have a control linked to the same cell, although only one form is shown at a time. Is this likely to cause problems?

    Geoff

  2. #2
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: VBA form controls Controlsource syntax

    The "=" is not required.
    You can check this by manually typing a named range into a ControlSource property... The "=" always gets dropped.
    Elegant Simplicity............. Not Always

  3. #3
    Registered User
    Join Date
    11-04-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Office 365
    Posts
    15

    Re: VBA form controls Controlsource syntax

    Thanks for the quick response.

    In my case the "=" is retained as in the screen dump:
    ControlSourceScrnDump.jpg

    Hence my confusion - I would have expected the VB editor to complain but it seems to allow either syntax.
    Geoff

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: VBA form controls Controlsource syntax

    Geoff

    Are you having any problems with the forms?

    Are all the named ranges valid?
    If posting code please use code tags, see here.

  5. #5
    Registered User
    Join Date
    11-04-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Office 365
    Posts
    15

    Re: VBA form controls Controlsource syntax

    I am fairly certain all names are valid. I have been having some very obscure problems at times where a formula in a cell is deleted or replaced by a value. It mainly happens after I change the worksheet structure by inserting or deleting rows, so I thought it might be a reference in a Controlsource of a control somewhere that could be to blame, so I did an audit with some code to list all controls and their controlsources where relevant, and realised I had used both methods. I seem to recall that earlier I had a problem just entering the range name but it worked if I put in the "=". But now I can't seem to distinquish between the two cases, both seem OK but presumably one is the "correct" way. I have not been able to track cells losing their formulas to any specific ControlSource references - it is almost like a control remembers an absolute reference that is not updated when the definition of the name is changed as a result of inserting rows. Very frustrating.

    Geoff

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: VBA form controls Controlsource syntax

    Geoff

    a formula in a cell is deleted or replaced by a value
    That's one of the problems of using ControlSource, the kind of 2-way thing between the linked cell and the control

    If that sort of thing is happening it might be worth not using ControlSource.

    You can easily control what gets writtent to cells/controls with a few lines of code.

  7. #7
    Registered User
    Join Date
    11-04-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Office 365
    Posts
    15

    Re: VBA form controls Controlsource syntax

    Yes, I agree, it seems a little fragile. Might be worth going back to code to set the values on open and save on an OK button click. But ControlSource is convenient for a form with a lot of option settings (configuration settings for the main program). Perhaps I can set the range name in the tag parameter and automate the initial population of the controls with settings and similarly automate the settings storage back in the worksheet on closing.

    Thanks for your help.

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: VBA form controls Controlsource syntax

    Geoff

    I've used Tag in the past as a kind of cell link, and other things.

    Seems to be a bit of a forgotten property.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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