Application.Run problem when calling _click sub in userform from class module

rplazzotta

New Member
Joined
Oct 28, 2021
Messages
41
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Can anyyone tell me why the following Class (URLEvents) code raises error 1004?

Option Explicit
Public WithEvents ThisURL As MSForms.Image
Private SubName As String

Private Sub ThisURL_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If Button = 2 Then 'right-click
' Do the inputbox stuff here then run the relevant _click sub to open the URL; work in progress
Else 'left-click
'MsgBox "Mainform.IsL1 is " & MainForm.IsL1 'works because boolean variable IsL1 is declared Püblic in MainForm
SubName = "MainForm." & ThisURL.Name & "_Click()"
Application.Run SubName '**** This raises error 1004,
'even though all the _Click subs are declared Public in MainForm
'BUT: if I hardcode the name of the Sub in MainForm, it works
'e.g. MainForm.GoogleEN.Click() works !
End If

End Sub

In MainForm's header:
Dim mURLEvents As Collection
Dim cURLEvents As URLEvents

In MainForm's initialize code (excerpt) :
Dim oCtrl As MSForms.Control
Set mURLEvents = New Collection
For Each oCtrl In Me.Controls
If TypeName(oCtrl) = "Image" And ((oCtrl.Top > 418 And oCtrl.Top < 442) Or Left(oCtrl.Name, 6) = "Google") Then
Set cURLEvents = New URLEvents
Set cURLEvents.ThisURL = oCtrl
mURLEvents.Add cURLEvents
End If
...
Next
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
You cannot run code in a userform using Run. I can't think of any good reason for needing to offhand.
 
Upvote 0
Because I have about 20 image controls in MainForm that already have _click subs, but I want to replace them with _mouseup to handle different behaviour depending on whether the user left-clcks or right-clicks them. So, rather than writing a _mouseup for each of them, I thought I could write a single _mouseup in my Class module.
Would Call SubName work?
 
Upvote 0
This doesn't sound like great design to me, but anyway. You could make the click event routines public, then call them using:

Code:
CallByName ThisURL.Parent, ThisURL.Name & "_Click", VbMethod

in the class module.
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,897
Members
449,097
Latest member
dbomb1414

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