Excel VBA: Add user form controls to selected worksheet

时间:2017-05-22 07:19:56

标签: excel vba excel-vba

I don't know how to code this and I also don't know how to explain this but I wish it is understandable.

I have a user form and I want the controls on that user form be added to a selected worksheet. I think I should loop through all the controls then add its labels to a selected worksheet without having to specify what cell this label will be added to every time. How should I code this?

I'm not really sure if my explanation is understandable so please bear with me. Thank you.

1 个答案:

答案 0 :(得分:0)

如果您想要控件的名称,请尝试:

Dim c As Control
For Each c In UserForm1.Controls
    i = i + 1
    Worksheets("Sheet1").Range("A" & i).Value = c.Name
Next

编辑: 对于标签标题。

Dim c As Control
For Each c In UserForm1.Controls
    If TypeName(c) = "Label" Then
    i = i + 1
    Worksheets("Sheet1").Range("A" & i).Value = c.Caption
    End If
 Next