Logo 
Search:

MS Office Forum

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds

Setting UserForm captions via VBA loop

  Asked By: Waldemar    Date: Oct 27    Category: MS Office    Views: 1351
  

Hope someone can explain what I'm doing wrong here...

I'm trying to set the "caption" of a series of labels on a UserForm
via an incrementing loop. The labels have the names lT_Legend'n'
(where 'n' is an integer defined by a loop where i = iRetCodeMin to
iRetCodeMax).

I'm getting a R/T Error 91: "Object Variable or With Block variable
not Set".

Code (with i As Integer; sName As Object; sString As String & s As
String) I'm trying to use is as follows:

For i = iRetCodeMin To iRetCodeMax
s = "iT_Legend" & i
sName = s '<<<< yields R/T Err.91: Obj. Var. or With Block var.
not Set
sString = Application.WorksheetFunction.VLookup _
(i, Range("RetCodes_Lookup"), 3, False)
sName.Caption = sString
Next

I'm sure it's a silly error on my part (especially given my limited
knowledge of programming & VBA). Any help would be greatly
appreciated.

Share: 

 

3 Answers Found

 
Answer #1    Answered By: Zane Thompson     Answered On: Oct 27

Not sure but try...

s = "iT_Legend" & CStr(i)

.. that is, change the integer  to a string  explicitly.

Let us know.

 
Answer #2    Answered By: Constance Reid     Answered On: Oct 27

To adjust your macro the least, try something on these lines:

For i = iRetCodeMin To iRetCodeMax
s = "iT_Legend" & i
Set sName = UserForm1.Controls(s)
sString = Application.WorksheetFunction.VLookup _
(i, Range("RetCodes_Lookup"), 3, False)
sName.Caption = sString
Next i

If it's only the caption  you're changing you can dispense with
creating an object  (sName) and the variable  's' thus:

For i = iRetCodeMin To iRetCodeMax
sString = Application.WorksheetFunction.VLookup _
(i, Range("RetCodes_Lookup"), 3, False)
UserForm1.Controls("iT_Legend" & i).Caption = sString
Next i

perhaps even shorter by dispensing with sString:

For i = iRetCodeMin To iRetCodeMax
UserForm1.Controls("iT_Legend" & i).Caption _
= Application.WorksheetFunction.VLookup _
(i, Range("RetCodes_Lookup"), 3, False)
Next i

 
Answer #3    Answered By: Este Ferrrari     Answered On: Oct 27

The object  (sName) extra variables (sString & s) where all feeble attempts on my
part to try to get the code  to work.

Your final, simplified suggestion was exactly what I was trying to accomplish!

 
Didn't find what you were looking for? Find more on Setting UserForm captions via VBA loop Or get search suggestion and latest updates.




Tagged: