Search code examples
excelvbauserform

Userform with variable size


In my program a string gets created and its size varies a lot.

This string should then be shown in a separate window.

Unfortunately the regular Msgbox (limited to 1024 signs I believe) is too small for my needs.

Now I created a Userform for the issue but of course it looks silly when there is a giant Userform and only two values inside, but next time I need the size since there are 1000 values in the string.

I noticed that the regular Msgbox changes size depending on how long the string inside it is.

How can I create a userform that changes its size depending on its content? I only found information on how to make it full screen, which isn't helpful for this case.

The only real code that I can show you is something generic like this:

Sub Ausgabe1()

Dim Werte As Variant
Dim x As Integer

For i = 0 to X
Werte(i) = i
Next i

MsgBox Werte

End Sub

Solution

  • You can vary the value of the width property. How I would do it is to create the userform with a text box and give the userform a displaytext property.

    Property Let DisplayText(s as String)
    Dim x as Long
    x = Len(s)
    If x > 0 Then 
       Me.Textbox1.Width = x*.7
       Me.Width = Me.Width * 100/x
    Else
       Me.Textbox1.Width = 75      ' (edited)
       Me.Width = 100
    End If
    
    End Property
    

    And then I'd call it like this

    Load Userform1
    With Userform1
       .DisplayText = "your message here"
       .Show
    End With
    

    You'll have to play with the numbers in the code to get them to fit your screen resolution etcx