Coding Object Name Changes Through A Loop

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Would someone be lomd enough to provide a solution to a hurdle I have encountered in my code?
Here is a snippet of that code. It works, no errors, but is failing to provide the finished results I seek/

Rich (BB code):
With MultiPage4
        arrCtrls = Array("CU2", "CU3", "CU4", "CU5", "CU6")
        For i = LBound(arrCtrls) To UBound(arrCtrls)
            valLookup = arrCtrls(i)
            st9 = WorksheetFunction.VLookup(valLookup, drng, 12, False)
            Set ws_st = swb.Worksheets(st9)
            .tb_cu2_nm.Value = WorksheetFunction.index(ws_lists.Range("BR2:BR25"), WorksheetFunction.Match(valLookup, ws_lists.Range("BT2:BT25"), 0))
        Next i
    
    End With

I have a userform with 5 textboxes, tb_cu2_nm, tb_cu3_nm, tb_cu4_nm, tb_cu5_nm and tb_cu6_nm.
The code above uses an index/match formula to seek the name associated withthe particular lookup value from the array ... either CU2, CU3, CU4, CU5 or CU6.
Once that value is acquired, it is to populate the appropriate textbox (ie tb_cu2_nm with CU2, tb_cu3_nm with CU3 and tb_cu4_nm with CU4 and so on)
I've managed to figure it out for tb_cu2_nm, but I don't know how to change the object value in blue to reflect the object change. (ie to tb_cu3_nm etc).

Anyone?
 
Last edited:

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
I'm experimenting with this ... not sure if I'm on the right track/

Rich (BB code):
    Dim tb1 as object
    With MultiPage4
        With .Pages(0)
            arrCtrls = Array("CU2", "CU3", "CU4", "CU5", "CU6")
            arrdest = Array("tb_cu2_nm", "tb_cu3_nm", "tb_cu4_nm", "tb_cu5_nm", "tb_cu6_nm")
            For i = LBound(arrCtrls) To UBound(arrCtrls)
                valLookup = arrCtrls(i)
                st9 = WorksheetFunction.VLookup(valLookup, drng, 12, False)
                Set ws_st = swb.Worksheets(st9)
                Set tb1 = arrdest(i)
                .tb1 = WorksheetFunction.index(ws_lists.Range("BR2:BR25"), WorksheetFunction.Match(valLookup, ws_lists.Range("BT2:BT25"), 0))
            Next i
        End With
    End With

The line in red stops the code with an "Object required" error.
 
Upvote 0
just guessing, but think I'd try something along the lines of
Code:
.Controls(".tb_" & arrCtrls(i) & "_nm").value =
 
Last edited:
Upvote 0
Hi NoSparks ...
I appreciate your reply. I just had to remove the period from ".tb_" to get it to work. Fantastic! Thank you!
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,399
Latest member
alchavar

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