Pass a string variable to a user form using property procedures

prabhatsoni

New Member
Joined
May 25, 2013
Messages
5
Hi Guys

I want to display a string to users. Normally I would do that using message boxes. But that would not give me enough control over the font size, and I want to display using font size 48.
I could have used global variables, but that is not a good coding way. The other way of doing that is using property procedures. I could pass a string variable to a Label control on the form and then could format it. Can any one help me in doing that ?

Thanks guys


Prabhat Soni
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Sounds like you already know what to do. I'm not sure what you are missing. Is the string hardcoded or is a user variable? Lets assume you create a user form and insert a single label name "Label1" (by default) insert the following code
Code:
 Private Sub UserForm_Initialize()
With Label1
    .Caption = "YourStringHere"
    .Font = "Tahoma"
    .Font.Bold = True
    .Font.Size = 48
    
    
End With

End Sub
 
Last edited:
Upvote 0
If you want to set the properties of a label you can do it like this.
Code:
UserForm1.Label1.Caption = "My very big message!!!")
UserForm1.Label.Font.Size = 48
UserForm1.Show
 
Upvote 0
Thanks guys
solution proposed by bstory84: how would you pass string variable (I am brand new to the vba - registered two days back)
Norie's solution is concise and compact.
Thanks a lot.
But guys I am trying to learn property procedures. I have tried to go through literature, but somehow they did not make sense to me. I should confess that I was not even able to decipher a few examples that they have on the net.
Could you guys help me ?

ONce again thanks a lot.

Prabhat Soni
 
Upvote 0
Prabhat

bstory's code is probably better, using With can avoid errors and sore fingers.
 
Upvote 0
You could use something like this:
Code:
Public Property Get Message() As String

   Message = Me.Label1.Caption

End Property

Public Property Let Message(ByVal sMessage As String)

   Me.Label1.Caption = sMessage

End Property
 
Upvote 0
@probhatsoni Depends on how the string is being populated. At what point or where is the string going to be stored or entered?
 
Upvote 0
Sorry bstory84
I was out of town.
It is like this:
I am generating numbers for crossing out in a game of bingo meant for young kids. The numbers are generated randomly but while displaying them I actually display them in a little equation form (like showing 7-4 in place of showing 3). Thus the equation generated in converted into a string and displayed in a form.

With your solution, how do you propose I should go about.


Thanks

Prabhat Soni
 
Upvote 0
Friends,
As per RoryA's suggestion, I wrote the code and it works. But there is slight problem. The OK button should unload the form, which it is not doing. Can any one find out what is wrong in the code.

The Macro is
Code:
' Regular procedure in a regular code module
Sub Testi()
  Dim lResponse As Long   ' pass to form
  Dim sResponse As String  ' get from form
  Dim Ufrm As UserForm1

  'ask user for response
  sResponse = InputBox("String ?")
  
  ' start up the form
  Set Ufrm = New UserForm1
  With Ufrm
    .Label1 = sResponse
    ' show the form
    .Show

  End With
End Sub


The code behind the form is :
Code:
Private Sub OKButton1_Click()
Unload UserForm1
End Sub

Private Sub UserForm_Click()
End Sub

Public Property Get Message() As String

   Message = Me.Label1.Caption

End Property

Public Property Let Message(ByVal sMessage As String)

   Me.Label1.Caption = sMessage

End Property

Thanks guys

Prabhat Soni
 
Upvote 0
Hi there,

Not that there is anything wrong with a new instance of the form, but I'm not seeing exactly why not just show the form? Anyways, if you were to show the form, then the code would work. With a new instance of the form, you'll want to change the command button's code to 'Unload Me', as the Me keyword is understood by the class or object module inwhich it resides.

Hope that helps,

Mark
 
Upvote 0

Forum statistics

Threads
1,215,903
Messages
6,127,651
Members
449,394
Latest member
fionalofthouse

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top