I have the following question:
The code you see below has been pasted in an excel object
Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Application.ScreenUpdating = False
Dim DAY, other As Range
Set DAY = Range("b4:af4")
If Not Intersect(DAY, Range(Target.Address)) Is Nothing Then
ActiveCell.Copy
Sheets("SP Analysis").Activate
Range("b2").PasteSpecial Paste:=xlPasteValues
'ElseIf Not Intersect(other, Range(Target.Address)) Is Nothing Then
End If
End Sub
The macro runs but it doesn't copy the activecell in the sheet SP Analysis.
If I change the code with the following:
Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) Application.ScreenUpdating = False Dim DAY, other As Range Set DAY = Range("b4:af4")
If Not Intersect(DAY, Range(Target.Address)) Is Nothing Then Call TEST
'ElseIf Not Intersect(other, Range(Target.Address)) Is Nothing Then End If End Sub
with macro TEST doing
sub test
ActiveCell.Copy
Sheets("SP Analysis").Activate
Range("b2").PasteSpecial Paste:=xlPasteValues
end sub
The command does what is supposed to do.
Question is why? what's the difference between one method and the other?
And, how can I have the command working in an excel object rather than having to call a macro?
Thank you
Try this. Use Target rather than ActiveCell although in this case I don't know why the latter wouldn't work. And we can transfer the value directly without having to activate anything.
Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Application.ScreenUpdating = False
Dim DAY As Range, other As Range 'need to specify type for each variable, otherwise Variant
Set DAY = Range("b4:af4")
If Not Intersect(DAY, Target) Is Nothing Then
Cancel=True
Sheets("SP Analysis").Range("b2").Value = Target.Value 'use target
End If
Application.ScreenUpdating = True 'turn it back on
End Sub
Related
I have simple macros for clearing cells on "Sheet1", which have drop down lists.
Sub reset1()
Range("D20:E21").ClearContents
Range("D8:E9").ClearContents
Range("D6:E7").ClearContents
End Sub
Sub reset2()
Range("D20:E21").ClearContents
Range("D8:E9").ClearContents
End Sub
Then I call these macros on "Sheet1" if the cell values change
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$D$4" Then
Call reset1
End If
If Target.Address = "$D$6" Then
Call reset2
End If
End Sub
This code is written on the "Sheet1".
Normally it works but sometimes reset1() doesn't work.
I should then save and reopen the excel or run the macro manually.
Should I better modify some codes?
First problem is that with Range("D20:E21") it is not clear in which worksheet that range should be. Always specify the worksheet like Worksheets("Sheet1").Range("D20:E21").
Second problem is that if you .ClearContents in a Worksheet_Change event this is a cell change and triggers another Worksheet_Change event and so on. So it is recommended to disable events Application.EnableEvents = False before changing cells in Worksheet_Change event.
Third problem is that if you test Target.Address = "$D$4" and you copy paste a range where D4 is included your code will not run even if your cell D4 changed. Therefore you always need to work with Intersect.
Option Explicit
Sub Reset1(ByVal ws As Worksheet)
ws.Range("D20:E21,D8:E9,D6:E7").ClearContents
' alternative:
' Union(ws.Range("D20:E21"), ws.Range("D8:E9"), ws.Range("D6:E7")).ClearContents
End Sub
Sub Reset2(ByVal ws As Worksheet)
ws.Range("D20:E21,D8:E9").ClearContents
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
On Error Goto ENABLE_EVENTS ' in any case an error happens make sure events are enabeld again
If Not Intersect(Target, Me.Range("D4")) Is Nothing Then
Reset1 Me ' here we tell Reset1 to take `Me` as worksheet. Me refers to the worksheet `Target` is in.
End If
If Not Intersect(Target, Me.Range("D6")) Is Nothing Then
Reset2 Me
End If
ENABLE_EVENTS:
Application.EnableEvents = True
If Err.Number Then
Err.Raise Err.Number
End If
End Sub
I'm making a macro on VBA that is attached to a data validation list in excel. When a certain option is selected from the list I want it to run a macro assigned to that selection. However I am repeatedly getting the error 'Compile error. Argument is not optional.' I understand I need to add parameters after calling the macro but anything I enter results in 'Object required' or 'expected )'
This code is in my worksheet. The line 'Case "Fifteen": Macro1' is the one with the error.
Private Sub Worksheet_change(ByVal Target As Range)
If Not Intersect(Target, Range("P4")) Is Nothing Then
Select Case Range("P4")
Case "Fifteen": Macro1
End Select
End If
End Sub
The following code is located in a module - It is used to copy values from the cells in one worksheet to another.
Sub Macro1(ByVal Target As Range)
Dim r1 As Range, r2 As Range
Set r1 = Sheets("Calculator").Range("C18:D19")
Set r2 = Sheets("Answers").Range("I14:J15")
If Intersect(Target, r1) Is Nothing Then Exit Sub
Application.EnableEvents = False
r2.Value = r1.Value
Application.EnableEvents = True
End Sub
Any idea's what the Argument parameters should be? I thought it would be along the lines of Case "Fifteen":Macro1("C18:D19") or Case "Fifteen":Macro1(r1), but no luck was had.
The code in the module works when it is on its own so I don't think there would be any issues with it.
Any help is greatly appreciated. I looked around and couldn't find an answer.
Thanks for the help. By removing the intersect lines it now works as intended. The final code is below.
Private Sub Worksheet_change(ByVal Target As Range)
Select Case Range("P4")
Case "Fifteen": Macro1
End Select
End If
End Sub
Sub Macro1()
Dim r1 As Range, r2 As Range
Set r1 = Sheets("Calculator").Range("C18:D19")
Set r2 = Sheets("Answers").Range("I14:J15")
Application.EnableEvents = False
r2.Value = r1.Value
Application.EnableEvents = True
End Sub
I am attempting to update a column (E8:E508) with the contents of another reference column (G8:G508) each time the reference column changes, using the following code:
Private Sub Worksheet_Calculate()
Dim Rng As Range
Set Rng = Range("G8:G503")
If Not Intersect(Rng, Range("G8:G503")) Is Nothing Then
Range("E8:E503") = Range("G8:G503").Value
End If
End Sub
The code works as intended, but appears to be running over and over again and eventually crashes Excel.
Range("E8:E503") = Range("G8:G503").Value
triggers another calculation, which triggers your event handler, etc etc.
To prevent that endless cycle you need to temporarily disable events before doing that (and then re-enable after)
Private Sub Worksheet_Calculate()
Application.EnableEvents = False
Range("E8:E503").Value = Range("G8:G503").Value
Application.EnableEvents = True
End Sub
try this
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
Dim Rng As Range
Set Rng = Range("G8:G503")
If Not Intersect(Rng, Target) Is Nothing Then
Range("E8:E503") = Rng.Value
End If
Application.EnableEvents = True
End Sub
I have a code running on my sheet. This code contains two subroutines. But I want to run this code in all my sheets and I was wondering what would be the best approach.
The total code that is running is the following:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim Rng As Range
Set Rng = Range(Cells(3, 6), Cells(500, 7))
Dim Intersection
Set Intersection = Application.Intersect(Target, Rng)
If Target.Cells.Count = 1 Then
If Not Intersect(Target, [B2]) Is Nothing Then _
Range("E:E").Find(vbNullString, [E3], , , , xlNext).Select
End If
If Not Intersection Is Nothing Then
If IsNumeric(Selection.Value) And Selection.Value <> "" Then
If (GetAsyncKeyState(vbKeyRButton)) Then 'right mouse button
Selection.Value = (Selection.Value + 1)
Cells(Selection.Row, 1).Select
End If
End If
End If
End Sub
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
Dim Rng As Range
Set Rng = Range(Cells(3, 6), Cells(500, 7))
Dim Intersection
Set Intersection = Application.Intersect(Target, Rng)
If Not Intersection Is Nothing Then
Cancel = True
End If
End Sub
If anyone could give me tips, it would be much appreciated!
Put your code in module. You can follow below link.
http://www.contextures.com/xlvba01.html
It is more complicated that just putting your code in another location. If you put your code in a module (which is the right move) you will need to tell it to run on other sheets too. You're code can be written to apply to any sheet in any open or closed workbook from a Worksheet objects code module, too. It is all about how it is written.
Are you using works like Me or ActiveSheet in your code? This is red flag that no matter where you place it the result will probably not be what you are looking for.
If you want a change event to be effective for all the sheets in a workbook, put the code in the ThisWorkbook module and use the Workbook_SheetChange event. This event will fire when any cell is changed in any sheet.
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
End Sub
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