Is There An Easier Way?

WorkMC

Board Regular
Joined
Jul 21, 2005
Messages
88
I'm using the code below to fill details in on a number of labels within a UserForm. Can the code be made any easier?

Private Sub UserForm_Initialize()
Application.ScreenUpdating = False
Label1.Caption = Sheets("Data").Range("C1").Value
Label2.Caption = Sheets("Data").Range("D1").Value
Label30.Caption = Sheets("Data").Range("E1").Value
Label3.Caption = Sheets("Data").Range("F1").Value
Label4.Caption = Sheets("Data").Range("G1").Value
Label5.Caption = Sheets("Data").Range("H1").Value
Label6.Caption = Sheets("Data").Range("I1").Value
Label7.Caption = Sheets("Data").Range("K1").Value
Label8.Caption = Sheets("Data").Range("L1").Value
Label9.Caption = Sheets("Data").Range("M1").Value
Label10.Caption = Sheets("Data").Range("N1").Value
Label11.Caption = Sheets("Data").Range("O1").Value
Label12.Caption = Sheets("Data").Range("P1").Value
Label13.Caption = Sheets("Data").Range("Q1").Value
Label14.Caption = Sheets("Data").Range("R1").Value
Label15.Caption = Sheets("Data").Range("S1").Value
Label16.Caption = Sheets("Data").Range("T1").Value
Label17.Caption = Sheets("Data").Range("U1").Value
Label18.Caption = Sheets("Data").Range("V1").Value
Label19.Caption = Sheets("Data").Range("W1").Value
Label20.Caption = Sheets("Data").Range("X1").Value
Label21.Caption = Sheets("Data").Range("Y1").Value
Label22.Caption = Sheets("Data").Range("Z1").Value
Application.ScreenUpdating = True
End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

iggydarsa

Well-known Member
Joined
Jun 28, 2005
Messages
1,647
try:

For i = 1 To 22
Controls("Label" & i).Caption = Sheets("Data").Cells(1, i + 2)
Next i
 

staticbob

Well-known Member
Joined
Oct 7, 2003
Messages
1,079
That would work, if you make the columns consistent. There is a discrepancy with Label30 using E1, and nothing using J1. The code above assumes that labels 1 - 22 will always take the text from columns C - Y in that order, Label1 - C, Label2 - D, Label22 - Y etc etc.

Just clean up the label names and the above suggestion should work well !

Bob
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,504
Messages
5,832,111
Members
430,111
Latest member
Francis Xavier

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
Top