Solved

vba update labels in FormBox

Posted on 2011-02-15
6
522 Views
Last Modified: 2012-05-11
Hello Experts,

I have this code to populate the labels in a form box.

Private Sub UserForm_Initialize()
   Dim i As Integer
   Dim ctrl As Control
   
   i = 2
   For Each ctrl In Me.Controls
      If ctrl.Name = "Label" & i Then
         ctrl.Caption = ThisWorkbook.Sheets("Data").Range("A" & i + 17)
         i = i + 1
      End If
   Next
     
   i = 2
   For Each ctrl In Me.Controls
        If ctrl.Name = "TextBox" & i Then
        ctrl.ControlSource = ThisWorkbook.Sheets("Data").Range("B" & i + 17)
        i = i + 1
        End If
   Next
   
End Sub

But when I import new changes to affected cell range on the Sheets("Data"), the Form Box does not reflect these changes when I initialize it.

How can I get the FormBox to populate the label names when changes have been made?

Thanks
0
Comment
Question by:mike637
  • 2
  • 2
  • 2
6 Comments
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 34898114
That code will only run when the form is loaded. Any changes you make thereafter will not be reflected unless you unload and reload it - is that what you are doing?
0
 

Author Comment

by:mike637
ID: 34898392
rorya,

Yes, I have a button with an assigned macro to open it upon demand.  However, when It opens - it does not load the new changes from the "Data" sheet.

meck637
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 34898441
Are you sure your form is getting unloaded and reloaded, and not just hidden and reshown? If it is the latter, you need to use the Activate event instead.
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 45

Accepted Solution

by:
patrickab earned 500 total points
ID: 34899157
mike637,

Try the attached file - code below

Patrick
Private Sub CommandButton1_Click()
UserForm1.Hide
Unload UserForm1
End Sub

Private Sub UserForm_Initialize()
   Dim i As Integer
   Dim ctrl As Control
   
   i = 2
   For Each ctrl In Me.Controls
      If ctrl.Name = "Label" & i Then
         ctrl.Caption = ThisWorkbook.Sheets("Data").Range("A" & i + 17)
         i = i + 1
      End If
   Next
     
   i = 2
   For Each ctrl In Me.Controls
        If ctrl.Name = "TextBox" & i Then
            ctrl.ControlSource = "Data!" & "B" & i + 17
            i = i + 1
        End If
   Next
   
End Sub

Open in new window

mike637-01.xls
0
 

Author Closing Comment

by:mike637
ID: 34899852
Thank you for being the experts you are!!
0
 
LVL 45

Expert Comment

by:patrickab
ID: 34900510
mike637 - Thanks for the grade - Patrick
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The System Center Operations Manager 2012, known as SCOM, is a part of the Microsoft system center product that provides the user with infrastructure monitoring and application performance monitoring. SCOM monitors:   Windows or UNIX/LinuxNetwo…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question