Trying to open userform & run code from worksheet but see a compile error message

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,899
Office Version
  1. 2007
Platform
  1. Windows
I currently have a command button on my worksheet that opens a userform.
Then once the userform is open i need to press another command button to run the code & see values in Listboxe & Textboxes.

Here is the code for Sheet command button
VBA Code:
Private Sub ConnectorUsed_Click()
 Dim lastRow As Long
     lastRow = Cells(Rows.Count, 6).End(xlUp).Row
     Range("A7:L" & lastRow).Sort Key1:=Range("D8:D" & lastRow), _
     Order1:=xlAscending, Header:=xlNo
     Range("A8").Select
  
     ConnectorForm.Show
    
End Sub

Here is the code for userform command button
Code:
Private Sub CheckConnectorsUsed_Click()
  Dim r As Range, f As Range, Cell As String, added As Boolean
  Dim sh As Worksheet
  Dim i As Long
  Set sh = Sheets("MCLIST")
  sh.Select
  With ListBox1
    .Clear
    .ColumnCount = 4
    .ColumnWidths = "100;170;70;10"
    
    Set r = Range("C8", Range("C" & Rows.Count).End(xlUp))
    Set f = r.Find(TextBox1.Value, After:=r.Cells(r.Count), LookIn:=xlValues, LookAt:=xlPart)
    
    If Not f Is Nothing Then
      Cell = f.Address
      Do
         If Len(Cells(f.Row, "L").Value) <> 0 Then
          .AddItem f.Value
          .List(.ListCount - 1, 1) = f.Offset(, 1).Value ' MODEL
          .List(.ListCount - 1, 2) = f.Offset(, 6).Value ' YEAR
          .List(.ListCount - 1, 3) = f.Offset(, 9).Value ' CONNECTOR USED
          .List(.ListCount - 1, 4) = f.Row
         End If
        Set f = r.FindNext(f)
      Loop While f.Address <> Cell
      .TopIndex = 0
    End If
    End With
    
    TextBox2.Text = ActiveSheet.Range("L1").Value ' BLACK
    TextBox3.Text = ActiveSheet.Range("L2").Value ' CLEAR
    TextBox4.Text = ActiveSheet.Range("L3").Value ' GREY
    TextBox5.Text = ActiveSheet.Range("L4").Value ' RED
    

End Sub


I have put the userform command button code AFTER ConnectorForm.Show in the sheet command button code thinking when i press the sheet button the form will open & the code will run but
i see the following issue.
What did i do wrong & how to fix it Thanks

EaseUS_2025_02_13_09_58_53.jpg
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
The userform commandbutton code should be in the userform, not in the worksheet code module.
 
Upvote 0
I agree so im trying to open form & run its code from worksheet button.

Is that possible
 
Upvote 0
If you want the code to run automatically when the form is loaded, it should be in the Userform_Initialize event of the form, not a button click, but given that it appears to search for a value entered in a textbox, and that textbox will be empty, I don't really see the point.
 
Upvote 0
Solution
Putting the code in the Initialize part worked & those empty Textboxes are the filled.
Thanks
 
Upvote 0

Forum statistics

Threads
1,226,516
Messages
6,191,496
Members
453,659
Latest member
thomji1

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