Setting Label Captions equal to cell values

madeofcarbon

New Member
Joined
Apr 4, 2011
Messages
3
I'm getting an error for "could not find the specified object"

Basically I have labels with names A1-A18 and I want to put the values from a worksheet cells D4-D22

PartN = "130SM0003"
ExcelColumn = "D"
DosingColumn = "A"
DosingRow = 1
ExcelRow = 4

Do
CP.Controls(DosingColumn & DosingRow).Caption = Worksheets(PartN).Range(ExcelColumn & ExcelRow)
DosingRow = DosingRow + 1
ExcelRow = ExcelRow + 1
Loop Until DosingRow = 18

Any Help is appreciated.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Do you know the value of dosingRow it fails at?

Might you be better off using a for .. next loop anyway? I don't think the caption in A18 would be updated here in any case.

Not sure if that's the problem tho

See if this works any better

Code:
    PartN = "130SM0003"
    ExcelColumn = "D"
    DosingColumn = "A"
    
    For i = 1 To 18
        CP.Controls(DosingColumn & i).Caption = Worksheets(PartN).Range(ExcelColumn & i + 3).Value
    Next i
 
Upvote 0
I'm still getting the same error.

If i take the loop out and run the code, it does put the correct value into label "A1" but once i put the loop back in it shows error
 
Upvote 0
Have you double checked the label names?

Perhaps there's a leading/trailing space in one of them, or some other typo.
 
Upvote 0
I just went thru the loop manually by setting i = 1 and running the code. A1 contained the correct value.

Then i = 2 > run code > A2 was correct value etc.

That all worked fine. It is just when I go to add the loop that i receive the error.
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,834
Members
452,947
Latest member
Gerry_F

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