Show userform if A11:A29 or G8 is clicked - excel

I used to have the following code that worked to bring up a user form (calendar)when A11:A29 or G8 was clicked:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Application.Intersect(Target, Range("A11:A29")) Is Nothing Then DatePickerForm.Show
If Not Application.Intersect(Target, Range("G8")) Is Nothing Then DatePickerForm.Show
End Sub
I wound up having to scrap that user form due to issues with MS MonthView Control 6.0. I moved to a different calendar found here. The site suggests using double click anywhere on the sheet to show the userform. This works as designed but does so for any cell whereas I would like to limit it to only cells A11:A29 and G8, either by double clicking or clicking once. I tried modifying the code on the page to:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim ARange As Range
Dim GRange As Range
Set ARange = Range("A11:A29")
Set GRange = Range("G8")
If Not Application.Intersect(Target, ARange) Is Nothing Then DatePickerForm.Show
If Not Application.Intersect(Target, GRange) Is Nothing Then DatePickerForm.Show
End Sub
I get run-time error 91, object variable or with block not set. Debugging highlights the "If isdate" line below:
Private Sub UserForm_Activate()
If IsDate(Target.Value) Then
Calendar1.Value = Target.Value
End If
Call MoveToTarget
End Sub

It is because Target is Range object familiar only inside the specific Worksheet code module it is placed (like a local variable) , oo it's not recognized inside the DatePickerForm form module.
Use:
Private Sub UserForm_Activate()
If IsDate(ActiveCell.Value) Then Calendar1.Value = ActiveCell.Value
End If
Also, there is a more efficient way to see if Target falls inside multiple Range, use Union to merge multiple Ranges into one Range, see code below:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim UnionRange As Range
Set UnionRange = Application.Union(Range("A11:A29"), Range("G8"))
If Not Application.Intersect(Target, UnionRange) Is Nothing Then DatePickerForm.Show
End Sub

Related

Merging multiple Worksheet_SelectionChange

I have got no problem in running one Private Sub Worksheet_SelectionChange , but when i add multiple Worksheet_SelectionChange events it is not running. Later, i came to know that it is not possible running different worksheet selection change events in the same sheet.
I am having four different Private Sub Worksheet_SelectionChange events, trying to merge them with the help of various sites but none worked to me, as per my understanding.
Could i get some help,
1.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim cells1 As Range
Set cells1 = ActiveSheet.Range("B1:B27")
If Not (Intersect(Target, cells1) Is Nothing) Then
ActiveSheet.Range("B30").Value = Target.Value
End If
End Sub
2.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim cells2 As Range
Set cells2 = ActiveSheet.Range("C1:C27")
If Not (Intersect(Target, cells2) Is Nothing) Then
ActiveSheet.Range("C30").Value = Target.Value
End If
End Sub
3.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim cells3 As Range
Set cells3 = ActiveSheet.Range("S1:S27")
If Not (Intersect(Target, cells3) Is Nothing) Then
ActiveSheet.Range("S30").Value = Target.Value
End If
End Sub
4.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim cells4 As Range
Set cells4 = ActiveSheet.Range("T1:T27")
If Not (Intersect(Target, cells4) Is Nothing) Then
ActiveSheet.Range("T30").Value = Target.Value
End If
End Sub
I appreciate your help.
Thank you.
You can use a switch (select case) within your change event to allow options for which will occur.
Mock-up:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Row > 27 then Exit Sub
Select Case Target.Column
Case 2, 3, 19, 20
Cells(30,Target.Column).Value = Target.Value
End Select
End Sub
I have added the Exit Sub check for if the row > 27, as your ranges are 1:27, for each of the Columns. This replaces the Intersect() check.
You perform the same action based on the Target.Column, so that is the only other parameter to verify and utilize.

VBA if a specific columns and rows are selected, Type Mismatch error

I am trying to write a code where sub activates when I select a specific column and specific row but gives me a type mismatch error on the following code:
If Selection.Worksheet.Columns("E:L") And Selection.Worksheet.Rows("16:99999") Then
You will see that the keyword Target always returns the Range Object of the cell that fired the Worksheet_Change Event. So, armed with this knowledge we can now use the Intersect Method to take some action when/if specified cell is changed. For example;
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("E12:L54787")) Is Nothing Then
MsgBox "Hello"
End If
End Sub
Write the code in the corresponding worksheet:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If (Not Intersect(Target, Columns("E:L")) Is Nothing) And _
(Not Intersect(Target, Rows("16:99999")) Is Nothing) Then
Debug.Print Target.Address
End If
End Sub
Selection.Change documentation
Application.Intersect method documentation

VBA Worksheet Change is not working properly?

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

Excel : How do I call the "active cell" to a cell

I have Excel 2013
Currently i have a code that takes any cell i click on and shows its contents to another cell as well.
My digging through the internet came up with the code below.
How do i call this to a cell with a custom formula instead of editing the code every time i want to change the location (b15) in this case?
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
If Not Intersect(Target, ActiveSheet.UsedRange) Is Nothing Then
Range("B15").Value = Target.Value
End If
End Sub
Thank you in advance.
In a standard module include:
Public Function ShowMe() As Variant
Application.Volatile
ShowMe = ActiveCell.Value
End Function
and in the worksheet code area include:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.Calculate
End Sub

Excel Worksheet_Change Event not working

I am trying to write a macro where changing any column
should automatically save worksheet.
My Excel sheet expands till G25.
I tried this but its not working:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("G25")) Is Nothing Then
ActiveWorkbook.Save
End Sub
I have save it under ThisWorkBook.
Any help is appreciated.
Under ThisWorkbook that handler is called Workbook_SheetChange and it accepts two arguments, Sh (of type Object) and Target (a Range). So, your code won't work there.
If you put your code in the sheet you want (Sheet1, for instance) instead of in the ThisWorkbook, put the End If and change the range to "A1:G25" (representing a square from row 1 column A to row 25 column 25), it should work.
This did:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("A1:G25")) Is Nothing Then
MsgBox "changed"
End If
End Sub
For completeness, under ThisWorkbook, this will work:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("A1:G25")) Is Nothing Then
MsgBox "changed"
End If
End Sub
The other common reason why an Event doesn't work is that EnableEvents has been set to False
I would code your problem like this as
typically you need to work with the range of interest that is being tested. So creating a variable rng1 below serves both as an early exit point, and a range object to work with. On a sheet event Target.Worksheet.Range("A1:G25") will work but it is lengthy for it's actual use
If you do have a range to manipulate then making any further changes to the sheet will trigger a recalling of the Change event and so on, which can cause Excel to crash. So it is best to disable further Events, run your code, then re-enable Events on exit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng1 As Range
Set rng1 = Intersect(Target, Range("A1:G25"))
If rng1 Is Nothing Then Exit Sub
Application.EnableEvents = False
'work with rng1
Application.EnableEvents = True
End Sub

Resources