Shorter way of writing?

Eric Carolus

Board Regular
Joined
Sep 17, 2012
Messages
128
Office Version
  1. 2016
Platform
  1. Windows
Hi folks.

Is there a shorter way of writing the following?

VBA Code:
frmallclasses.lblc1.Caption = Worksheets("Combinations1").Range("C4").Value
frmallclasses.lblc2.Caption = Worksheets("Combinations1").Range("E4").Value
frmallclasses.lblc3.Caption = Worksheets("Combinations1").Range("G4").Value
frmallclasses.lblc4.Caption = Worksheets("Combinations1").Range("I4").Value
frmallclasses.lblc5.Caption = Worksheets("Combinations1").Range("K4").Value
frmallclasses.lblc6.Caption = Worksheets("Combinations1").Range("M4").Value
frmallclasses.lblc7.Caption = Worksheets("Combinations1").Range("O4").Value
frmallclasses.lblc8.Caption = Worksheets("Combinations1").Range("Q4").Value
frmallclasses.lblc9.Caption = Worksheets("Combinations1").Range("S4").Value
frmallclasses.lblc10.Caption = Worksheets("Combinations1").Range("U4").Value
frmallclasses.lblc11.Caption = Worksheets("Combinations1").Range("W4").Value
frmallclasses.lblc12.Caption = Worksheets("Combinations1").Range("U4").Value
frmallclasses.lblc13.Caption = Worksheets("Combinations1").Range("AA4").Value
frmallclasses.lblc14.Caption = Worksheets("Combinations1").Range("AC4").Value
frmallclasses.lblc15.Caption = Worksheets("Combinations1").Range("AE4").Value
frmallclasses.lblc16.Caption = Worksheets("Combinations1").Range("AG4").Value
frmallclasses.lblc17.Caption = Worksheets("Combinations1").Range("AI4").Value
frmallclasses.lblc18.Caption = Worksheets("Combinations1").Range("AK4").Value
frmallclasses.lblc19.Caption = Worksheets("Combinations1").Range("AM4").Value
frmallclasses.lblc20.Caption = Worksheets("Combinations1").Range("AO4").Value
frmallclasses.lblc21.Caption = Worksheets("Combinations1").Range("AQ4").Value
frmallclasses.lblc22.Caption = Worksheets("Combinations1").Range("AS4").Value
frmallclasses.lblc23.Caption = Worksheets("Combinations1").Range("AU4").Value
frmallclasses.lblc24.Caption = Worksheets("Combinations1").Range("AW4").Value
frmallclasses.lblc25.Caption = Worksheets("Combinations1").Range("AY4").Value
frmallclasses.lblc26.Caption = Worksheets("Combinations1").Range("BA4").Value
frmallclasses.lblc27.Caption = Worksheets("Combinations1").Range("BC4").Value
frmallclasses.lblc28.Caption = Worksheets("Combinations1").Range("BE4").Value

It refers to data being written from a sheet called Combinations1 to labels on a form called frmallclasses.
Thank you.

Crow
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hi,
untested & I have only had quick glance at your code but one way maybe

VBA Code:
    Dim i       As Long, c As Long
    Dim ws      As Worksheet
    
    Set ws = ThisWorkbook.Worksheets("Combinations1")
    
    c = 3
    For i = 1 To 28
        Me.Controls("lblc" & i).Caption = ws.Cells(4, c).Text
        c = c + 2
    Next i

Note: I have replaced the hard coded frmallclasses with the Me keyword as allows you to refer to the object without specifying the name. However, this change does assume that the code is in your Userforms code page.

Dave
 
Upvote 0

Forum statistics

Threads
1,215,390
Messages
6,124,667
Members
449,178
Latest member
Emilou

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