Compile error adding command button to Userform at runtime.

HighAndWilder

Active Member
Joined
Nov 4, 2006
Messages
392
Office Version
  1. 365
Platform
  1. Windows
Hi All.

I'm creating a number of controls at runtime on a Userform.

This has been working fine and now I get an error message.

Compile error. Method or data member not found.

I've tried rebuilding the Userform a number of times and it is still causing an error.

It is the following line that causes the error. The 'Add' is highlighted so I assume that that the 'Add' method cannot found.

Set ctrl = Me.Controls.Add("Forms.CommandButton.1", "cmd" & strButtonPrefix & i, 1)

I have the necessary library selected.

Any ideas anybody?

Thanks.

VBA Code:
Private Sub subCreateCommandButtonGrid()
Dim intLeft As Integer
Dim intHeight As Integer
Dim intWidth As Integer
Dim intCount As Integer
Dim intPerRow As Integer
Dim arrCaptions(1 To 26) As String
Dim i As Integer
Dim strButtonPrefix As String
Dim ctrl As Control
Dim intGap As Integer
Dim intCounter As Integer
Dim intOriginalLeft As Integer

    intLeft = 10
    intHeight = 35
    intWidth = 35
    intCount = 26
    intPerRow = 8
    strButtonPrefix="Alpha"

    For i = 1 To 26
        arrCaptions(i) = Chr(64 + i)
    Next i

    intOriginalLeft = intLeft
    
    intGap = 2

    For i = 1 To intCount
            
        Set ctrl = Me.Controls.Add("Forms.CommandButton.1", "cmd" & strButtonPrefix & i, 1)
        
        With ctrl
            .TOP = intTop
            .Left = intLeft
            .Height = intHeight
            .Width = intWidth
            .Caption = arrCaptions(i)
        End With
        
        If (i Mod intPerRow) = 0 Then
            intTop = intTop + intHeight + intGap
            intLeft = intOriginalLeft
        Else
            intLeft = intLeft + intWidth + intGap
        End If

    Next i

End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
I just tried your macro and it ran without any errors, creating the 26 command buttons for each letter of the alphabet.

I'm assuming that you're running this code within the code module for your userform, otherwise you would get an "Invalid use of Me keyword" compile error. Are you in fact running it within the userform code module?

By the way, as an aside, you forgot to declare the variable intTop. If you place the statement "Option Explicit" at the very top of your module, it will force you to declare all variables and avoid potential errors.
 
Upvote 0
I rebuilt the Userform and found the problem.

I had a Command Button named 'Controls' which of course triggered the error.
It should have been 'cmdControls' and was only temporary anyway to display a list of the forms controls in a message box.

In the following line it was looking for the 'Add' method of this control which of course did not exist.

Set ctrl = Me.Controls.Add("Forms.CommandButton.1", "cmd" & strButtonPrefix & i, 1)

It's a good job that I time to spare.
 
Upvote 0
That's great, I'm glad you were to sort it out.

Cheers!
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top