+ Reply to Thread
Results 1 to 4 of 4

Error 91 when counting and adding commandButton

Hybrid View

  1. #1
    Registered User
    Join Date
    04-18-2014
    Location
    Brno
    MS-Off Ver
    Excel 2003
    Posts
    5

    Error 91 when counting and adding commandButton

    Hello,
    i have Userform1 with a few CommandButtons. When i run my VBA code, it return Error 91 - object variable or with block variable not set

    What would i like to do:
    1. I would like to count number of controls on Userform1
    2. I would like to do something with CommandButton1 whitch is placed on Userform1 (change Caption, position ...)

    Please tell me why the Error 91 occurs in this VBA code and how to repair it?

    Dim Button As CommandButton
    
    Debug.Print UserForm1.Controls.Count
    Set Button = ThisWorkbook.VBProject.VBComponents("UserForm1").Designer("CommandButton1") - here is error 91 object variable or with block variable not set
    
    With Button
        .Caption = "New button"
        .Width = 72
        .Height = 24
        .Left = 12
        .Top = 58
    End With
    Thanks for reply
    Last edited by selector; 04-18-2014 at 06:01 AM.

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Error 91 when counting and adding commandButton

    Hi selector,

    Try the following code tested using Excel 2003. However, to actually use the Command Button is a lot more complicated. To do that you probably need a 'Class Module'. See the following (excellent) example, which will probably make your head spin several times.
    http://stackoverflow.com/questions/1...g-vba-on-excel

    Lewis


    Sub CreateDynamicCommandButton()
    
      Dim Button As CommandButton
    
      'Display the Userform - not necessary if UserForm is already activated
      UserForm1.Show False
    
    
      Set Button = UserForm1.Controls.Add("Forms.CommandButton.1")
    
      Debug.Print UserForm1.Controls.Count
      With Button
          .Caption = "New button"
          .Width = 72
          .Height = 24
          .Left = 12
          .Top = 58
      End With
    End Sub

  3. #3
    Registered User
    Join Date
    04-18-2014
    Location
    Brno
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Error 91 when counting and adding commandButton

    Thank you for your answer.
    I find another way to resolve my problem. This code does not return error message and do exactly what i need:

    'Counting number of controls on UserForm1
    For Each novy In ThisWorkbook.VBProject.VBComponents("UserForm1").Designer.Controls
        numberOfButtons = numberOfButtons + 1
    Next novy
    
    'Change some properties of CommandButton1
    Set Button = ThisWorkbook.VBProject.VBComponents("UserForm1").Designer("CommandButton1")
    
    With Button
        .Caption = "New button"
        .Width = 72
        .Height = 24
        .Left = 12
        .Top = 58
    End With
    Last edited by selector; 04-20-2014 at 04:48 AM.

  4. #4
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Error 91 when counting and adding commandButton

    I apologize for misreading your original question. I thought you wanted to add a Control dynamically, not change it's properties.

    The following may help you in the future.

    Lewis

    You can change CommandButton1 properties as follows:
    Sub ModifyCommandButton1A()
      
      UserForm1.Show False
    
      With UserForm1.CommandButton1
        .Caption = Format(Now(), "hh:mm:ss")
        .Width = 72
        .Height = 24
        .Left = 12
        .Top = 58
      End With
      
    End Sub
    Another way similar to yours is:
    Sub ModifyCommandButton1B()
      
      Dim myButton As Control
      Dim cCntrl As Control
      Dim i As Integer
      
      UserForm1.Show False
      
      i = 0
      For Each cCntrl In UserForm1.Controls
        If cCntrl.Name = "CommandButton1" Then
          Set myButton = cCntrl
          Exit For
        End If
      Next cCntrl
      
    
      Debug.Print myButton.Name
     
      With myButton
        .Caption = Format(Now(), "hh:mm:ss")
        .Width = 72
        .Height = 48
        .Left = 12
        .Top = 58
      End With
      
    End Sub


    You may find the following routine useful:
    Sub LoopThruUserForm1Controls()
    
      Dim cCntrl As Control
      Dim i As Integer
      Dim sData As String
      
       UserForm1.Show False
      
        i = 0
        For Each cCntrl In UserForm1.Controls
          i = i + 1
          sData = Format(Format(i, "####"), "@@@@  ") & _
                  Format(TypeName(cCntrl), "!@@@@@@@@@@@@@@") & _
                  Format(cCntrl.Name, "!@@@@@@@@@@@@@@@@") & _
           "H=" & Format(Format(cCntrl.Height, "0.00   "), "@@@@@@@@@") & _
           "W=" & Format(Format(cCntrl.Width, "0.00   "), "@@@@@@@@@") & _
           "T=" & Format(Format(cCntrl.Top, "0.00   "), "@@@@@@@@@") & _
           "L=" & Format(Format(cCntrl.Left, "0.00   "), "@@@@@@@@@")
          Debug.Print sData
          
       Next cCntrl
    End Sub
    And finally, if you want to see if a control exists:
    Sub TestUserFormControlExists()
      
      Dim i As Integer
      Dim bExists As Boolean
      Dim sControlName As String
    
      UserForm1.Show False
      
      For i = 1 To 3
        sControlName = "CommandButton" & i
        bExists = DoesUserForm1ControlExist(sControlName)
        MsgBox "UserForm Control '" & sControlName & "' EXISTS = " & bExists
      Next i
      
      UserForm1.CommandButton2.Visible = False
      sControlName = "CommandButton2"
      bExists = DoesUserForm1ControlExist(sControlName)
      MsgBox "Invisible UserForm Control '" & sControlName & "' EXISTS = " & bExists
      
    End Sub
    
    Public Function DoesUserForm1ControlExist(sControlName As String) As Boolean
      'This returns TRUE if control exists on the userform
        
      Dim cCntrl As Control
        
      Dim iError As Long
      
      On Error Resume Next
      Set cCntrl = UserForm1.Controls(sControlName)
      iError = Err.Number
      On Error GoTo 0
        
      If iError = 0 Then
        DoesUserForm1ControlExist = True
      End If
        
    End Function

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. neede help for creating commandbutton during runtime error
    By aaronkoh in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 01-03-2013, 09:41 AM
  2. Commandbutton in userform to execute code based on a previous commandbutton choice?
    By michaeljoeyeager in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-20-2012, 03:28 PM
  3. Commandbutton Error When Cancelled
    By Bluewhistler in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-19-2010, 04:29 AM
  4. VBA code for adding a commandbutton
    By ben_sumner in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-09-2007, 04:06 AM
  5. Adding commandbutton to sheet
    By ckoch in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-01-2006, 05:10 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1