I want to call a Macro (Rpt1) in Module1 by double-clicking a cell in Sheet2. I'm not certain how to do this. I seem to have the double click event executing from Sheet2 but I don't know how to call the macro I have in Module1.
You need to paste this code into your SHEET code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Const clickADDRESS As String = "A1" '<--- or whatever cell you want it to be
If Not Intersect(Target, Me.Range(clickADDRESS)) Is Nothing Then
Call Rpt1
End If
End Sub
which will call the macro in module code (my example)
Sub Rpt1()
MsgBox "This worked"
End Sub
By sheet code here's an illustration:
Related
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
Very novice at VBA so please bear with me.
Im trying to delete a table if a cell (B2) in another sheet changes.
Currently I have:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Worksheets("sheet2").Range("B2")) Is Nothing Then
Range("B21:D30").ClearContents
End If
End Sub
I've tried many variations, indirect, and different syntax but none work.
Update:
I should also mention that B2 on sheet2 will be changing based on the user selecting a group of radio buttons which are linked to sheet2!B2. I.e. I am not directly changing the value of B2 from sheet2. In fact sheet2 will be eventually hidden.
To trap the events in the Sheet2 of the hidden workbook (Let's call it Book2), you need to create a class to manage the _SheetChange event capture.
Let's say you want to capture the events in Book2.Sheet2 from Book1. Do this
1. Insert a class module (Let's call it Class1) and paste this code there
Code
Private WithEvents hiddenWb As Workbook
Public Property Set Workbook(wb As Workbook)
Set hiddenWb = wb
End Property
Public Property Get Workbook() As Workbook
Set Workbook = hiddenWb
End Property
Private Sub hiddenWb_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Sh.Name = "Sheet2" Then
If Not Intersect(Target, Sh.Range("B2")) Is Nothing Then
MsgBox "Range B2 was chnaged"
End If
End If
End Sub
2. In a module paste this code
Code
Option Explicit
Dim cWb As New Class1
Sub Sample()
'~~> Set a reference to the hidden workbook
Set cWb.Workbook = Workbooks("Book2")
'~~> Change the value of the cell B2
cWb.Workbook.Sheets("Sheet2").Cells(2, 2).Value = "Blah Blah"
End Sub
Screenshots
Testing
Run the procedure Sample() from Book1
Intresting Read
Events And Event Procedures In VBA by Charles H. Pearson
This has to be written in the Sheet2 module:
Private Sub Worksheet_Change(ByVal Target As Range)
With Me
If Not Intersect(Target, .Range("B2")) Is Nothing Then
.Parent.Worksheets("Sheet1").Range("B21:D30").ClearContents
End If
End with
End Sub
Change "Sheet1" to fit your worksheet name.
I wrote the following code in my worksheet and all ranges are in this same sheet.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("DistMatrix")) Is Nothing Then
Dim out1() As Double
out1 = OutStat(bucket(Target), Range("RegScale"))
FwdOut = outright(bucket(Target), Range("RegScale"))
Call NewScatter(FwdOut, out1)
End If
End Sub
I want to run a called sub if I choose a cell in the range DistMatrix.
This is partly working. I have to click on a cell in the range as if I want to write in it and then afterwards pick another one for the called sub to run.
I want however the sub to run as soon as I pick the cell. I don't want to go have to double click it as if it were to edit it and then pick another one for it to run.
You can use Worksheet_SelectionChange instead.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("DistMatrix")) Is Nothing Then
Dim out1() As Double
out1 = OutStat(bucket(Target), Range("RegScale"))
FwdOut = outright(bucket(Target), Range("RegScale"))
Call NewScatter(FwdOut, out1)
End If
End Sub
I'm working in Excel. I'd like to press a button to cancel the value of the cell at the left of the button itself.
So the user experience should be the following:
When the user presses "button1", the cell at its left became 0. The same for "button2" and "button3"
How can I do?
By using a Macro?
Assign this macro to any and all buttons, and it'll delete the info. in the cell directly to the left.
Sub test()
Dim btnRow&, btnCol&
btnRow = ActiveSheet.Shapes(Application.Caller).TopLeftCell.Row
btnCol = ActiveSheet.Shapes(Application.Caller).TopLeftCell.Column
Cells(btnRow, btnCol).Offset(0, -1).ClearContents
End Sub
Or, thanks to #Rory, this can be:
Sub test()
Activesheet.Shapes(Application.Caller).TopLeftCell.offset(0,-1).ClearContents
End Sub
Note: Make sure your shapes are well placed, as this uses wherever the top left of the shape is to determine the row/column. This macro reduces the need to run three different ones, depending on where, and minimizes any If/Then type statements as it uses the Caller to determine which shape, which determines where it is.
May this help...
'Add three buttons on the sheet
'And imaging that you want to delete B4 B5 B6
'You can discover which button is, by double cliking on the desing mode
'on your excel
Private Sub CommandButton1_Click() 'This is the button on the cell C4
Range("B4").ClearContents
'Here B4 is the range of the cell you want to delete...
'This could be acomplish just pressing the button DELETE
'Same by the two other buttons... just adding 1 to the number
'B4...B5...B6
'With this you only can delete de contents of the cell...
End Sub
Private Sub CommandButton2_Click() 'This is the button on the cell C5
Range("B5").ClearContents
End Sub
Private Sub CommandButton3_Click() 'This is the button on the cell C6
Range("B6").ClearContents
End Sub
Hyperlink method is what I've done before. Like it, because it look like it can click.
Add below code on the Worksheet module, when a hyperlink clicked, it will trigger the sub
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
If LCase(Target.Range.Text) Like "delete*" Then
ActiveCell.Offset(0, -1).ClearContents
End If
End Sub
You can use below code to generate as much as hyperlink you want.
Sheets(XXX).Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _
"", TextToDisplay:="Delete"
If your using command button
Option Explicit
Sub Button1_Click()
Range("A1") = 0
End Sub
or assign to worksheet
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("B1")) Is Nothing Then _
Range("A1") = 0
End Sub
Sheet 1 of my workbook contains (besides other data) a list of the other worksheets in column A. I wish to be able to click on any cell in column A5:A50 and go to the appropriate worksheet listed in that cell. My Sheet1 code is:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Me.Range("A5:A50")) Is Nothing Then SelectWorksheet
End Sub
and Module2 is:
Sub SelectWorksheet()
Dim strName As String
strName = Sheet1.Range("Target").Text (Error occurrs here: "Method 'Range' of object 'Worksheet' failed")
Sheets(strName).Select
End Sub
How do I get this to work as I expect? I know I could just click on the appropriate worksheet tab but I'm trying to learn how to code in VBA. Thanks. By the way, how do I get my post to show the code as typed in the question box?
Like this. You probably want to use the _SelectionChange event instead of the _Change event. Or you may find it necessary to use both events to trigger it. In any case here is how you pass the variable to another subroutine/module:
Sub Worksheet_SelectionChange(byVal Target as Range)
'Some code...
'
Call OtherMacro(Target)
'
End Sub
And then in your other macro, declare a range variable as required argument, like so:
Sub SelectWorksheet(rng as Range)
'
Dim strName as String
' at this point you can work with the "rng" variable, because it's been received from the other subroutine
strName = rng.Value
Sheets(strName).Activate
'
End Sub
You would need to add additional test to make sure user has not selected multiple cells, etc., but this should get you started.
Why not pass the sheet name from the cell to the sub?
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Me.Range("A5:A50")) Is Nothing _
And Target.Cells.Count = 1 Then SelectWorksheet (Target.Value)
End Sub
Sub SelectWorksheet(strName As String)
Sheets(strName).Select
End Sub
I've also done a check to make sure that only one cell is in the selection.