+ Reply to Thread
Results 1 to 6 of 6

adapt userform loop using control name containing variable for worksheet activex controls

Hybrid View

  1. #1
    Registered User
    Join Date
    02-22-2014
    Location
    canada
    MS-Off Ver
    Excel 2010
    Posts
    3

    adapt userform loop using control name containing variable for worksheet activex controls

    Hi all,

    First time posting - I have already gotten a lot of my questions answered here - thanks!

    The following code works great to save data from a userform to a database on a worksheet; where my control names are CTRL1, CTRL2 etc. But I haven't figured a way to adapt the code for activex controls I have put on another worksheet(sheet 6) in the same workbook. Any ideas how to loop thru? It does not recognise any combinations I've tried using say: sheet6.OleoObject("CTR" & i).value.
    TIA

    Sheets("DATABASE").Select
        Range("A" & rw).Activate
        'MsgBox "ACELL IS:" & ActiveCell
    
    
    
    'Start loop to add data back to worksheet from userform
    i = 1
    For i = 1 To cntrl_num ' number of controls on userform
        
        col = i 'equals column address
        'and variable rw equals row address
        'chosen = Cells(rw, 1)
        If Me.Controls("CTRL" & i).Value = Cells(rw, col).Value Then
            'MsgBox "skipping VALUE" & Me.Controls("CTRL" & i)
            Else: Cells(rw, col).Value = Me.Controls("CTRL" & i).Value
        End If
        'Me.Controls("CTRL" & i).Enabled = False
    Next i
    
    MsgBox "Record is now updated."

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: adapt userform loop using control name containing variable for worksheet activex contr

    How can I test your code without a sample file with userform ?
    If solved remember to mark Thread as solved

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

    Re: adapt userform loop using control name containing variable for worksheet activex contr

    Try OLEObjects.
    If posting code please use code tags, see here.

  4. #4
    Registered User
    Join Date
    02-22-2014
    Location
    canada
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: adapt userform loop using control name containing variable for worksheet activex contr

    Oops- typo there
    Norrie, have tried that - code below shows what I've tried and the errors.................
    Sub testvaluereturn()
    
    
    With Sheet6
        MsgBox .ctr2.Value 'works - returns value
    End With
    
    MsgBox Sheet6.OLEObjects("ctr" & 2) '"object doesn't support this property or method"
    MsgBox Sheet6.OLEObjects(ctr2) ' "runtime error 1004 method of OleObject failed
    MsgBox OLEObjects.ctr2 '"object required"
    
    i = 2
    With Sheet6
       MsgBox ("ctr" & i) ' returns ctr2-not value
       MsgBox("ctr" & i).Value  ' invalid qualifier
       
    End With
    
    End Sub

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

    Re: adapt userform loop using control name containing variable for worksheet activex contr

    The error here is caused by not referring to any property of the control.
    MsgBox Sheet6.OLEObjects("ctr" & 2)

    This will work.
    MsgBox Sheet6.OLEObjects("ctr" & 2).Object.Value

  6. #6
    Registered User
    Join Date
    02-22-2014
    Location
    canada
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: adapt userform loop using control name containing variable for worksheet activex contr

    Thank you! You solved it like that!
    I'm learning slowly but this one had me stumped for sure. The loop works perfectly now. Hope this helps someone else along the way.
    Final code below - saves updated record, clears out controls and disables controls:

    'Start loop to add data back to worksheet from sheet
    i = 1
    
    For i = 1 To cntrl_num ' number of controls on sheet
        
        col = i 'equals column address
        'and variable rw equals row address
        
        If Sheet6.OLEObjects("ctr" & i).Object.Value = Cells(rw, col).Value Then
        ' do nothing
        Else: Cells(rw, col).Value = Sheet6.OLEObjects("ctr" & i).Object.Value
        End If
        Sheet6.OLEObjects("ctr" & i).Object.Value = ""
        Sheet6.OLEObjects("ctr" & i).Object.Enabled = False
    Next i

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. copying the datas along with the activex controls and pasting in another worksheet
    By share knowledge in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-14-2011, 01:30 PM
  2. Programatically change the caption of all Worksheet (ActiveX) controls
    By redseujac in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-04-2011, 01:11 PM
  3. declare variable to hold pointer to ActiveX control
    By dlh in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-11-2010, 07:40 PM
  4. Unable to Display a Userform from an ActiveX Control
    By shivboy in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-10-2007, 03:05 PM
  5. Error 459 on callup of UserForm with ActiveX control
    By pagates in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-19-2006, 09:10 AM

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