Code to change caption of labels

Bandito1

Board Regular
Joined
Oct 18, 2018
Messages
239
Office Version
  1. 2016
Platform
  1. Windows
Hello all,

Im using the following code to change the caption of labels

Code:
rivate Sub Label5_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim stCaption As String


    stCaption = InputBox("Add new caption", , Label5)
        If stCaption = vbNullString Then Exit Sub
        Label5 = stCaption
End Sub

But when i close the userform and reopen it, the caption resets. The new value isn't stored.
Anyone knows how i can automatically save the new caption after i changed it?
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Hi Bandito1,

You need to store the value in a cell that can be referred to - I have used cell A1 of Sheet1 in the following example (change to suit):

Code:
Option Explicit
Private Sub UserForm_Initialize()

    'Sets the value for 'Labe15' from cell A1 of 'Sheet1'. Change to suit.
    Label5.Caption = Sheets("Sheet1").Range("A1").Value
    
End Sub
Private Sub UserForm_DblClick(ByVal Cancel As MSForms.ReturnBoolean)

    Dim stCaption As String
    
    stCaption = InputBox("Add new caption", , Label5)
    
    If stCaption = vbNullString Then Exit Sub
    
    'Sets the value for 'Labe15' from cell A1 of 'Sheet1'. Change to suit.
    With Sheets("Sheet1").Range("A1")
        .Value = stCaption
        Label5.Caption = .Value
    End With
    
End Sub

Regards,

Robert
 
Last edited:
Upvote 0
Hello Robert,

Thanks for your reaction. totally forget to reply.

It works perfectly, thank you very much!
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,271
Members
452,628
Latest member
dd2

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