将所有对象移回 excel VBA 用户表单中的原始位置

Move back all objects to original position in excel VBA Userforms

场景

我有一个 excel VBA 用户窗体,它周围有许多组合框、文本框、标签、复选框和按钮。当我单击某个按钮时,这些对象会将其位置更改为新位置。

我需要的

我需要在单击按钮时将所有这些移回原始位置,而不使用我之前使用的定位技术。有什么方法可以让我用一个命令将所有对象移回原来的位置吗?

我试过的

我尝试了 userform_initialize 但它不起作用

简而言之,您无法轻松做到这一点。 "resets" 最后一个动作是 Application.Undo 的唯一内置 Excel 方法。来自 MSDN documentation:

This method undoes only the last action taken by the user before running the macro, and it must be the first line in the macro. It cannot be used to undo Visual Basic commands.

正如 Jeffrey Weir 指出的那样;把原来的位置放在Tag 属性

Private Sub UserForm_Initialize()
Dim ctrl As MSForms.Control
    For Each ctrl In Me.Controls
        ctrl.Tag = ctrl.Top & "|" & ctrl.Left
    Next
End Sub

并回到原来的位置:

Private Sub CommandButton1_Click()
'button to go back, adjust name to your button
Dim ctrl As MSForms.Control
    For Each ctrl In Me.Controls
        ctrl.Top = Split(ctrl.Tag, "|")(0)
        ctrl.Left = Split(ctrl.Tag, "|")(1)
    Next
End Sub