+ Reply to Thread
Results 1 to 5 of 5

Store userform control sizes in array to use later

Hybrid View

  1. #1
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Store userform control sizes in array to use later

    Hi All

    This link shows how to resize a Userform and all it's controls...
    How would one adapt the size change to reset to initial open size...
    https://www.rondebruin.nl/mac/mac022.htm

    Edit...So saving the Initial Userform's width & height on open can work for the Userform itself...
    How would one go and save the initial controls ... save into array??

    So this could be quite a task...
    Get initial default sizes...And then reset to initial sizes...How can i save each controls sizes to an array to call on in the reset code...
    With UserForm1
        W = .Width
        H = .Height
        x = 1
        For Each ctrl In .Controls
            Debug.Print ctrl.Name
            ReDim Preserve CtrlArr(x)
            With ctrl
                On Error Resume Next
                CtrlArr(x) = .Name & "," & .Top & "," & .Left & "," & .Width & "," & .Height & "," & .Font.Size
                On Error GoTo 0
            End With
            x = x + 1
        Next ctrl
    End With
    For i = 0 To UBound(CtrlArr)
        Debug.Print CtrlArr(i)
    Next i
    End Sub
    Attached Files Attached Files
    Last edited by Sintek; 12-18-2018 at 03:07 PM.
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  2. #2
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Store userform control sizes in array to use later

    Okay, so this seems to work...Anyone else have a simpler solution...
    Private Sub UserForm_Initialize()
    With UserForm1
        W = .Width
        H = .Height
        x = 1
        For Each ctrl In .Controls
            ReDim Preserve CtrlArr(x)
            With ctrl
                On Error Resume Next
                CtrlArr(x) = .Name & "," & .Top & "," & .Left & "," & .Width & "," & .Height & "," & .Font.Size
                On Error GoTo 0
            End With
             x = x + 1
        Next ctrl
    End With
    End Sub
    Reset to original size....

    Private Sub CommandButton6_Click()
    With UserForm1
        .Width = W
        .Height = H
        i = 1
        For Each ctrl In .Controls
            Debug.Print ctrl.Name
            With ctrl
                .Top = Trim(Split(CtrlArr(i), ",")(1))
                .Left = Trim(Split(CtrlArr(i), ",")(2))
                .Width = Trim(Split(CtrlArr(i), ",")(3))
                .Height = Trim(Split(CtrlArr(i), ",")(4))
                On Error Resume Next
                .Font.Size = Trim(Split(CtrlArr(i), ",")(5))
                On Error GoTo 0
            End With
            i = i + 1
        Next
    End With
    End Sub
    Attached Files Attached Files

  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,259

    Re: Store userform control sizes in array to use later

    Hello Sintek,

    This might be...
    Global Dict As Object
    
    Sub SaveControlSizes()
        
        Dim Ctrl    As Object
        Dim Key     As Variant
        Dim Sizes   As Variant
        
            Set UF = UserForm1
            
            If Dict Is Nothing Then
                Set Dict = CreateObject("Scripting.Dictionary")
                Dict.CompareCmode = vbTextCompare
            End If
            
            For Each Ctrl In UF.Controls
                Key = Ctrl.Name
                If Not Dict.Exists(Key) Then
                    ReDim Sizes(4)
                        Sizes(0) = Ctrl.Top
                        Sizes(1) = Ctrl.Left
                        Sizes(2) = Ctrl.Width
                        Sizes(3) = Ctrl.Height
                        Sizes(4) = Ctrl.Font.Size
                    Dict.Add Key, Sizes
                End If
            Next Ctrl
        
    End Sub
    
    Sub RestoreControlSizes()
    
        Dim Ctrl    As Object
        Dim Key     As Variant
        Dim Sizes   As Variant
        
            Set UF = UserForm1
            
            If Dict Is Nothing Then
                MsgBox "The Control sizes have not been saved.", vbOKOnly + vbExclamation, "Resize Error"
                Exit Sub
            End If
    
            For Each Ctrl In UF.Controls
                Key = Ctrl.Name
                If Dict.Exists(Key) Then
                    Sizes = Dict(Key)
                    With Ctrl
                        .Name = Key
                        .Top = Sizes(0)
                        .Left = Sizes(1)
                        .Width = Sizes(2)
                        .Height = Sizes(3)
                        .Font.Size = Sizes(4)
                    End With
                End If
            Next Ctrl
            
    End Sub
    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
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Store userform control sizes in array to use later

    Leith..You are a legend...thanks so much for the alternative...A much cleaner solution it is...

  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,259

    Re: Store userform control sizes in array to use later

    Hello sintek,

    Welcome you are!

+ 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. VBA Code to resize sheets works fine except for inserted controls
    By PFDave in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-22-2016, 05:21 AM
  2. Resize userform and adjust controls on initialize
    By SierraKilo78 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-02-2014, 02:48 PM
  3. [SOLVED] Reset Userfom Controls from Worksheet
    By NamiSama in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-21-2014, 05:59 PM
  4. Replies: 0
    Last Post: 09-23-2014, 04:44 PM
  5. ActiveX Controls on Sheet Resize
    By GTVic in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-18-2008, 10:18 PM
  6. How to reset all controls (Drop Down list on a sheet)
    By tariq in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-11-2007, 08:06 AM
  7. [SOLVED] Reset controls
    By Slukes in forum Excel General
    Replies: 0
    Last Post: 03-30-2005, 02:06 PM

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