Open UserForm from any worksheet - excel

I have a Workbook with the following code in Sheet1:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Excel.Range, Cancel As Boolean)
UserForm1.Show
End Sub
When I double click on a row, this opens my Userform but only if I'm on Sheet1.
How can get this to work for any active worksheet without adding it to every sheet?

If you want your code to be executed for the active sheet in your workbook, then you need to remove the code you have for the single sheet and do the following:
Open the VBA Editor and find the ThisWorkbook object under "Microsoft Excel Objects".
Edit it and add the following code inside it:
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
UserForm1.Show
End Sub
This means that the double-click event will be detected for all sheets in the workbook. If you leave your code in the sheet, the sheet event gets fired first and then the workbook one.

Related

User form doesn't open after double-click event in a cell that has a data validation list

I have a protected range in my sheet and when I double-click on a cell, it unlocks the protected sheet and runs the form.
Is there a workaround to run userform in a cell that has a data validation list?
When I want to call this script excel only shows a spinning cursor ->
https://ibb.co/tbJ9RHm
Here is my simple code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Worksheets("Sheet1").Range("H3:AJ1500")) Is Nothing Then
Target.Worksheet.Unprotect Password:="123"
UserForm1.Show
End If
End Sub

How do I Automatically Copy VBA Code Across all Worksheets?

I have a spreadsheet that receives a large amount of data from an ERP export. A macro splits the data into separate tabs (worksheets) based on values in a certain column (SalesPerson).
Within each worksheet, I want a macro that adds functionality to that sheet, i.e. the ability to double click on a row and delete it. My code works.....
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim resp As VbMsgBoxResult
If Not Intersect(Target, Columns("A")) Is Nothing Then
Cancel = True
resp = MsgBox(Prompt:="Delete data from row " & Target.Row & "?", Buttons:=vbYesNoCancel)
If resp = vbYes Then Range(Replace("I#:M#", "#", Target.Row)).ClearContents
End If
End Sub
....the question I have:
this macro has to reside in the worksheet level, not the module level. Is there a way to automatically paste this script into each worksheet that is created?
You should be able to use the Workbook event instead. It gives you a sheet parameter you can use to identify which sheet the event was triggered from.
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
End Sub
Naturally, this doesn't reside on the Sheet object, it can be found on the ThisWorkbook object in your VBA editor.

I have a function in excel VBA which takes input from user using textbox

I have written a macro that takes input from user and returns a cell data.
Is there any way that triggers the process to run from macro
That is the the inputbox opens up directly with one click without having to go to VBA and run it?
Put this in Worksheet code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Call showMsg
End Sub
Put this in your module:
Sub showMsg()
Dim data As String
data = InputBox("Hello", "Title")
End Sub
You can use other worksheet or workbooks events as well e.g
(It will run macro whenever you open file)
Private Sub Workbook_Open()
Call showMsg
End Sub

Execute a macro automatically when the linked cell of a combo box changes

I have a series of 2 combo boxes. I have a macro called Generate which will change the options in the second combo box based on the number the first combo box returns. However this requires the user to press a button to execute this macro. I would like this macro to execute automatically when the number in the first combo box's linked cell changes.
This is the code I have previously tried, however the change in the link cell which is B2 doesn't seem to trigger the event.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B2")) Is Nothing Then Generate
End Sub
As mentioned before, a Worksheet_Change event will only be triggered by physically changing a cell, and thus your linked cell won't have any effect.
If it's a cell in another wb that needs to trigger your Generate Sub, then I'd advise using a Worksheet_Change event for said wb.
In my own project, I have a sub in a regular module:
Dim AddNew As Workbook
Set AddNew = Workbooks("") 'change this
Set oWb.Workbook = AddNew
Then, in a class module:
Public WithEvents m_wb As Workbook
Public Property Set Workbook(wb As Workbook)
Set m_wb = wb
End Property
Public Property Get Workbook() As Workbook
Set Workbook = m_wb
End Property
Public Sub m_wb_SheetChange(ByVal Sh As Object, ByVal Target As Range)
'In here, you could trigger the Generate Sub if a specific cell changes
End Sub
The simplest solution was to give the first combo box an input range and also assign it the Generate macro

Excel VBA: call sub when change to another worksheet

Pretty basic question. I'm looking for a sub which will be triggered when the user changes the active worksheet to a certain worksheet (calles worksheet3).
Doing some research in the internet could not help me, unfortunately.
The event sub I am looking for should be very similar to the workbook_open sub. Obviously it is triggered everytime the workbook opens. So my question: is there a similar event sub which is triggered when I open a certain worksheet? Kind of like worksheet3_open?
You can use the Worksheet_Activate() event in the Worksheet3 code page.
Choose your worksheet by double clicking in the PRoject window:
From the drop down at the top, choose "Worksheet"
From the second drop down just to the right choose "Activate".
You'll have a new subroutine created called Worksheet_Activate() that will fire every time that tab is activated by the user.
You can then call your subroutine inside of that code so it runs as well.
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
End Sub
or
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
End Sub
Nothing on the net????? :)
Place the following code under your workbook:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
If Sh.Name = "worksheet3" Then
YourSub
End If
End Sub

Resources