Update the name of text box with specific range

zinah

Active Member
Joined
Nov 28, 2018
Messages
353
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have 10 columns and each column has a different name. I have updated the Name Box as "Employee_Data" so I can reference this range in my macro. Based on these columns names, I need to update text boxes, i.e. Box 1 = "EE ID", Box 2= "EE Name", and so on.
I'm very new to macros, what I need to know is how to loop through the range "Employee_Data" and name each box with the name of each column?


Thanks!
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Try this.
All your textboxes should be called "TextBox" and the consecutive number from 1 to 10: "TextBox1", "TextBox2", "TextBox3", etc.

Code:
Private Sub UserForm_Activate()
    i = 1
    For Each col In Range("Employee_Data").Columns
        Me.Controls("TextBox" & i).Value = col.Cells(1, 1)
        i = i + 1
    Next
End Sub
 
Upvote 0
Hi,

Thank you for your respond, I got this message "Compile error: invalid use of Me keyword" and highlighted below row:
Me.Controls("TextBox" & i).Value = col.Cells(1, 1)
 
Upvote 0
You could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. If the workbook contains confidential information, you could replace it with generic data.
 
Upvote 0
@DanteAmor thank you so much for your usual support, you always the first to answer any inquiry I post here, I really appreciate that. I believe this macro worked perfectly fine:

Code:
Sub populate_EMPLOYEE_Info()    Set aSht = ActiveSheet
    Set rSht = Sheets("Role Scorecard")


    Dim shp As Shape
    Dim lblRng As Range, EmplRng As Range
    
        Set lblRng = Sheets("ref.").[LabelRange]
        Set EmplRng = Sheets("ref.").[EmplInfoRange]
        i = 0
        
    For lbl = lblRng.Row To (lblRng.Row + lblRng.Rows.Count - 1)
        i = i + 1
        
        For Each shp In ActiveSheet.Shapes
        If InStr(1, shp.Name, "Empl_" & i & "_Lbl") > 0 Then
            With shp
                .TextFrame2.TextRange.Characters.Text = Sheets("ref.").Cells(lbl, lblRng.Column).Value
                .TextFrame2.TextRange.Font.Bold = msoTrue
            End With
        End If
        
        If InStr(1, shp.Name, "Empl_" & i & "_Txt") > 0 Then
            With shp
                .TextFrame2.TextRange.Characters.Text = Sheets("ref.").Cells(lbl, EmplRng.Column).Value
                .TextFrame2.TextRange.Font.Bold = msoFalse
            End With
        End If
        
        Next shp
    Next lbl
End Sub
 
Upvote 0
I'm glad to help you. I appreciate your kind comments.
 
Upvote 0

Forum statistics

Threads
1,214,942
Messages
6,122,366
Members
449,080
Latest member
Armadillos

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