+ Reply to Thread
Results 1 to 5 of 5

Using For/Next Loop To Generate 4 CommandButtons On A UserForm Results In A Runtime Error 91

  1. #1
    Donna
    Guest

    Using For/Next Loop To Generate 4 CommandButtons On A UserForm Results In A Runtime Error 91

    Folkes,
    My loop generates the buttons Ok but when I try and debug.print a
    position property of the button I get :-
    'Run-time error 91
    Object Variable or with block variable not set'

    I cannot see what is wrong but it must be something to do with the way
    I am using Set and With within the loops. Can anybody point me in the
    right direction.

    Thanks for your help...again!

    Here's my code:-

    Option Explicit
    Option Base 1
    Sub delete()

    Dim TempForm, Capn(), NewCommandButton() As Variant ' UserForm
    Dim NewCommandButton1 As MSForms.CommandButton ' RC Variant
    Dim NewCommandButton2 As MSForms.CommandButton ' RV Variant
    Dim NewCommandButton3 As MSForms.CommandButton ' LC Variant
    Dim NewCommandButton4 As MSForms.CommandButton ' LV Variant
    Dim i As Integer, LeftPos As Integer

    Set TempForm = ThisWorkbook.VBProject.VBComponents.Add(3)

    'Create and Postion the Variant Buttons
    i = 1: Capn = Array("A", "B", "C", "D")
    NewCommandButton = Array("NewCommandButton1", "NewCommandButton2",
    "NewCommandButton3", "NewCommandButton4")
    LeftPos = 15
    For i = 1 To UBound(Capn)
    Set NewCommandButton(i) =
    TempForm.Designer.Controls.Add("forms.commandbutton.1")
    With NewCommandButton(i)
    .Caption = Capn(i): .Top = 6: .Left = LeftPos: .Width = 25:
    ..Height = 17
    End With
    LeftPos = LeftPos + 30
    Next i

    Debug.Print NewCommandButton1.Left '********OK Until
    Here**********
    End Sub

  2. #2
    Bob Phillips
    Guest

    Re: Using For/Next Loop To Generate 4 CommandButtons On A UserForm Results In A Runtime Error 91

    Donna,

    You haven't changed the names of the buttons, so they are still called
    CommandButton1, etc.

    --

    HTH

    RP
    (remove nothere from the email address if mailing direct)


    "Donna" <[email protected]> wrote in message
    news:[email protected]...
    > Folkes,
    > My loop generates the buttons Ok but when I try and debug.print a
    > position property of the button I get :-
    > 'Run-time error 91
    > Object Variable or with block variable not set'
    >
    > I cannot see what is wrong but it must be something to do with the way
    > I am using Set and With within the loops. Can anybody point me in the
    > right direction.
    >
    > Thanks for your help...again!
    >
    > Here's my code:-
    >
    > Option Explicit
    > Option Base 1
    > Sub delete()
    >
    > Dim TempForm, Capn(), NewCommandButton() As Variant ' UserForm
    > Dim NewCommandButton1 As MSForms.CommandButton ' RC Variant
    > Dim NewCommandButton2 As MSForms.CommandButton ' RV Variant
    > Dim NewCommandButton3 As MSForms.CommandButton ' LC Variant
    > Dim NewCommandButton4 As MSForms.CommandButton ' LV Variant
    > Dim i As Integer, LeftPos As Integer
    >
    > Set TempForm = ThisWorkbook.VBProject.VBComponents.Add(3)
    >
    > 'Create and Postion the Variant Buttons
    > i = 1: Capn = Array("A", "B", "C", "D")
    > NewCommandButton = Array("NewCommandButton1", "NewCommandButton2",
    > "NewCommandButton3", "NewCommandButton4")
    > LeftPos = 15
    > For i = 1 To UBound(Capn)
    > Set NewCommandButton(i) =
    > TempForm.Designer.Controls.Add("forms.commandbutton.1")
    > With NewCommandButton(i)
    > .Caption = Capn(i): .Top = 6: .Left = LeftPos: .Width = 25:
    > .Height = 17
    > End With
    > LeftPos = LeftPos + 30
    > Next i
    >
    > Debug.Print NewCommandButton1.Left '********OK Until
    > Here**********
    > End Sub




  3. #3

    Re: Using For/Next Loop To Generate 4 CommandButtons On A UserForm Results In A Runtime Error 91

    Bob,
    But should I have to rename them?
    If I was just generating the one commandbutton without the loop I could
    then use the debug.print without changing the name so why do I need to
    now?
    Donna


  4. #4
    Tushar Mehta
    Guest

    Re: Using For/Next Loop To Generate 4 CommandButtons On A UserForm Results In A Runtime Error 91

    From what I can tell the NewCommandButton=Array() statement is
    redundant since the Set NewCommandButton(i)=...replaces the previous
    content.

    That leaves one immediate operational problem. When you refer to
    Debug.Print NewCommandButton1.Left
    where exactly did you initialize the NewCommandButton1 variable?
    Either do so, or use NewCommandButton(1)

    --
    Regards,

    Tushar Mehta
    www.tushar-mehta.com
    Excel, PowerPoint, and VBA add-ins, tutorials
    Custom MS Office productivity solutions

    In article <[email protected]>,
    [email protected] says...
    > Folkes,
    > My loop generates the buttons Ok but when I try and debug.print a
    > position property of the button I get :-
    > 'Run-time error 91
    > Object Variable or with block variable not set'
    >
    > I cannot see what is wrong but it must be something to do with the way
    > I am using Set and With within the loops. Can anybody point me in the
    > right direction.
    >
    > Thanks for your help...again!
    >
    > Here's my code:-
    >
    > Option Explicit
    > Option Base 1
    > Sub delete()
    >
    > Dim TempForm, Capn(), NewCommandButton() As Variant ' UserForm
    > Dim NewCommandButton1 As MSForms.CommandButton ' RC Variant
    > Dim NewCommandButton2 As MSForms.CommandButton ' RV Variant
    > Dim NewCommandButton3 As MSForms.CommandButton ' LC Variant
    > Dim NewCommandButton4 As MSForms.CommandButton ' LV Variant
    > Dim i As Integer, LeftPos As Integer
    >
    > Set TempForm = ThisWorkbook.VBProject.VBComponents.Add(3)
    >
    > 'Create and Postion the Variant Buttons
    > i = 1: Capn = Array("A", "B", "C", "D")
    > NewCommandButton = Array("NewCommandButton1", "NewCommandButton2",
    > "NewCommandButton3", "NewCommandButton4")
    > LeftPos = 15
    > For i = 1 To UBound(Capn)
    > Set NewCommandButton(i) =
    > TempForm.Designer.Controls.Add("forms.commandbutton.1")
    > With NewCommandButton(i)
    > .Caption = Capn(i): .Top = 6: .Left = LeftPos: .Width = 25:
    > .Height = 17
    > End With
    > LeftPos = LeftPos + 30
    > Next i
    >
    > Debug.Print NewCommandButton1.Left '********OK Until
    > Here**********
    > End Sub
    >


  5. #5

    Re: Using For/Next Loop To Generate 4 CommandButtons On A UserForm Results In A Runtime Error 91

    Thank you....I have changed to NewCommandButton(1) and all is now
    well.
    donna


+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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