+ Reply to Thread
Results 1 to 5 of 5

OLE Object Array Declaration Syntax Problem

Hybrid View

  1. #1
    Registered User
    Join Date
    10-20-2011
    Location
    london
    MS-Off Ver
    Excel 2007
    Posts
    98

    OLE Object Array Declaration Syntax Problem

    I have 6 toggle buttons

    Toggle1
    Toggle2
    Toggle3
    etc

    I am wondering if its possible to have them as an array?

    option base 1
    
    dim Tog(6) as(???) '<< Not sure on this bit
    Tog(1)= Toggle1 
    Tog(2)= Toggle2
    Tog(3)= Toggle3 
    #etc
    
    for i = 1 to 6
    select case Tog(i)
    case true
    range("A" & i ) = "YAY"
    case false
    range("B" & i ) = "nay"
    end select
    Next i
    
    end sub
    Thanks for an insight

    Beat

  2. #2
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: OLE Object Array Declaration Syntax Problem

    Possibly:
    Dim toggles(0 To 1) As MSForms.ToggleButton
    
    Set toggles(0) = Me.ToggleButton1
    Set toggles(1) = Me.ToggleButton2

  3. #3
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: OLE Object Array Declaration Syntax Problem

    Sure, you can if you want to. If we keep the code the same as you presented it, it would be:
        Dim togs(1 To 6) As MSForms.ToggleButton
        Dim i As Long
        
        Set togs(1) = Toggle1
        Set togs(2) = Toggle2
        Set togs(3) = Toggle3
        Set togs(4) = Toggle4
        Set togs(5) = Toggle5
        Set togs(6) = Toggle6
        
        For i = LBound(togs) To UBound(togs)
            
            If togs(i).Value Then
                Range("A" & i).Value = "YAY"
            Else
                Range("B" & i).Value = "nay"
            End If
            
        Next i

    There are various other options available to you too. For example, you could use the Userform's controls collection:
        Dim i As Long
        
        For i = 1 To 6
            If Controls("ToggleButton" & CStr(i)).Value Then
                Range("A" & i).Value = "YAY"
            Else
                Range("B" & i).Value = "nay"
            End If
        Next i
    Hope that helps,

    Colin

    RAD Excel Blog

  4. #4
    Registered User
    Join Date
    10-20-2011
    Location
    london
    MS-Off Ver
    Excel 2007
    Posts
    98

    Re: OLE Object Array Declaration Syntax Problem

    Superb thanks,

    ill give it a try

  5. #5
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: OLE Object Array Declaration Syntax Problem

    in a userform:

    Private sn
    
    Private Sub UserForm_Click()
        MsgBox sn(0) & vbLf & sn(1) & vbLf & sn(2) & vbLf & sn(3)
    End Sub
    
    Private Sub UserForm_Initialize()
        ReDim sn(Controls.Count - 1)
        
        For j = 0 To UBound(sn)
            Set sn(j) = Me("checkbox" & j + 1)
        Next
    End Sub



+ 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