简体   繁体   中英

Changing properties of multiple controls in VBA userform permanently

I have a rather large multipage userform in which I want to make changes to some of the tags. I wrote the following bit of code, only to realize that it only changed said properties during run time - whereas I need them to be changed permanently.

Sub ephemeralNameCh()
   Dim p As Integer, lenTag As Integer, lastShMulti As Integer
   Dim Ctrl As Control

   lastShMulti = uf_Screening.MultiPage1.Count - 1              'Last pg in multipage uf

   For p = 0 To lastShMulti
      For Each Ctrl In uf_Screening.MultiPage1.Pages(p).Controls
         'If tags needs to be change, do so
         If Ctrl.Tag <> "" And Left(Ctrl.Tag, 3) <> "P10" And Left(Ctrl.Tag, 3) <> "P11" Then
            If Left(Ctrl.Tag, 2) <> "P0" Then
               lenTag = Len(Ctrl.Tag)
               'New tag
               Ctrl.Tag = "P0" & Right(Ctrl.Tag, lenTag - 1)
            End If
         End If
      Next Ctrl
   Next p
End Sub

From going through lots of somewhat similar questions I've gathered I probably need to use

With ThisWorkbook.VBProject.VBComponents("uf_Screening").Designer

But I have not been able to implement it, despite trying several variations. This one

Sub permanentNameCh()
   Dim p As Integer, lenTag As Integer, lastShMulti As Integer
   Dim Ctrl As Control

   lastShMulti = uf_Screening.MultiPage1.Count - 1              'Last pg in multipage uf

   With ThisWorkbook.VBProject.VBComponents("uf_Screening").Designer
      For p = 0 To lastShMulti
         For Each Ctrl In uf_Screening.MultiPage1.Pages(p).Controls
            'If tags needs to be change, do so
            If Ctrl.Tag <> "" And Left(Ctrl.Tag, 3) <> "P10" And Left(Ctrl.Tag, 3) <> "P11" Then
               If Left(Ctrl.Tag, 2) <> "P0" Then
                  lenTag = Len(Ctrl.Tag)
                  'New tag
                  .MultiPage1.Pages(p).Controls(Ctrl.Name).Tag = "P0" & Right(Ctrl.Tag, lenTag - 1)
               End If
            End If
         Next Ctrl
      Next p
   End With
End Sub

results in Runtime Error 91: Object variable or with block variable not set .

Any ideas on how to make this work?

Yes, as you've discovered, you'll need to use the Designer property to make permanent changes. Try the following code...

Option Explicit

Sub permanentNameCh()
   Dim p As Integer, lenTag As Integer
   Dim Ctrl As Control

   With ThisWorkbook.VBProject.VBComponents("uf_Screening").Designer.MultiPage1
        For p = 0 To .Pages.Count - 1
            For Each Ctrl In .Pages(p).Controls
                'If tags needs to be change, do so
                If Ctrl.Tag <> "" And Left(Ctrl.Tag, 3) <> "P10" And Left(Ctrl.Tag, 3) <> "P11" Then
                   If Left(Ctrl.Tag, 2) <> "P0" Then
                      lenTag = Len(Ctrl.Tag)
                      'New tag
                      Ctrl.Tag = "P0" & Right(Ctrl.Tag, lenTag - 1)
                   End If
                End If
            Next Ctrl
        Next p
   End With
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM