I am working on a handheld scanner to scan (QR code) and (barcode) to output information on cells for sticker printing.
A = If Target.Address = "$L$9" And Target.Value <> ""
B = If Target.Address = "$H$9" And Target.Value <> Or If Target.Address = "$L$9" And Target.Value <> "" Then
The QR code will output five information on cells (H9,I9,J9,K9,L9) while barcode only one (H9).
I use Worksheet_Change(ByVal Target As Range) to trigger an action when there is value on the cells.
When I scan, the information seems to output accordingly for QR code when I use A and it'll do the rest of the code but its not work on barcode, and so to make them both working I change it to B and the result is vice versa.
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Target.Addres = "$L$9" And Target.Value <> "" Then
'...
End If
Application.EnableEvents = True
End Sub
You should add error handling to get past the workaround you'll have to do every time you hit an error: resetting the value of Application.EnableEvents.
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Goto Handler
Application.EnableEvents = False
<your code>
Exit_Proc:
Application.EnableEvents = True
Exit Sub
Handler:
MsgBox "Hit an error: " & Err.Description
Goto Exit_Proc
End Sub
Related
In a worksheet I have two drop-down lists (cells C7 and C68) which each have a dependent drop-down in the cell below. I have a code (below) which will clear the cell of the dependent drop-down if I change the selection in the above list (so that the lists do not mis-match), however I can only get this to work for the one drop-down in the sheet. How can I amend this to that it works if I alter either of the cells with the "Parent" list?
.
Existing code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$7" Then
If Target.Validation.Type = 3 Then
Application.EnableEvents = True
Target.Offset(1, 0).Value = ""
End If
End If
exitHandler:
Application.EnableEvents = True
Exit Sub
I suggest using only 1 dropdown list (which user actually selects) and then 2nd "linked cell" is using vlookup from some kind of data transformation list.
All fixed - for anyone who also has this problem, the correct code was:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$7" Or Target.Address = "$C$68" Then
If Target.Validation.Type = 3 Then
Application.EnableEvents = False
Target.Offset(1, 0).Value = ""
End If
End If
exitHandler:
Application.EnableEvents = True
Exit Sub
End Sub
In my Change Event, I need to trap a cell's old value, so I use an Application.Undo. However I have a macro that makes changes, which obviously clears the Undo Stack, but still relies on the rest of the Change Event procedures.
Can't rely on the Selection Change Event because if the user is already on the cell, this Event doesn't fire.
Can't use a hidden/mirror sheet as my full application builds these sheets based on a Master Template. I'd have to duplicate the info for as many sheets that the user creates from the Master.
And using On Error Resume Next is kludgy code.
Is there a way to either check the Undo Stack or determine if the Change Event was caused by user interaction or my macro.
Here's a quick demo to show the issue:
Sheet1 Code-Behind:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim NewValue As String
Dim OldValue As String
On Error GoTo ErrorHandler
Application.EnableEvents = False
If Not Intersect(Target, Me.Range("F3")) Is Nothing Then
NewValue = Target.Value
Application.Undo
OldValue = Target.Value
Target.Value = NewValue
MsgBox "Changed from: " & OldValue & " to " & NewValue
End If
ErrorHandler:
If Err.Number > 0 Then MsgBox Err.Description
Application.EnableEvents = True
End Sub
Standard Module Code:
Option Explicit
Sub MacroTest()
'clears undo stack
Sheet1.Range("F3").Value = "Macro Run"
End Sub
Skip the step after "NewValue = Target.Value". Add the logic in between these lines:
NewValue = Target.Value
On Error Resume Next ' Add this line of code
Application.Undo
I need help for generating the macro that basically gives the value "200000" based on a drop down menu in a cell. This drop down menu has two defined values in it(120 and 480). If other value in the drop down menu is selected then, I should have the freedom of writing any value that I want. The code which I came up with is below
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("$G$11")) Is Nothing Then
Range("$B$20:$R$25,$Z$20:$AM$25").ClearContents
End If
If Target.Cells.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("$G$11")) Is Nothing Then
Range("$F$16:$Q$16,$R$15:$U$16,$V$16:$AA$16,$AB$15:$AM$16").ClearContents
End If
If Range("I16") = 120 Or Range("I16") = 480 Then
Range("F16") = 200000
Else
Range("F16") = ""
End If
exitHandler:
Application.EnableEvents = True
Exit Sub
End Sub
However, I have another macro which clears all the contents in the cells due to which the above code is causing an error. Any help is much appreciated.
Make sure you're not re-triggering your event handler from within.
Also worth adding an error handler to make sure events aren't left turned off.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim v
On Error GoTo exitHandler
If Target.Cells.CountLarge > 1 Then Exit Sub
If Not Intersect(Target, Me.Range("G11")) Is Nothing Then
Application.EnableEvents = False
Me.Range("B20:R25,Z20:AM25,F16:Q16,R15:U16,V16:AA16,AB15:AM16").ClearContents
End If
If Not Intersect(Target, Me.Range("I16")) Is Nothing Then
v = Target.Value
Application.EnableEvents = False
Me.Range("F16").Value = IIf(v = 120 Or v = 480, 200000, "")
End If
exitHandler:
Application.EnableEvents = True
End Sub
Basically you just need to disable events before clearing cells so that the Change code is not triggered.
I'm not sure how the second bit of code relates so may need some adjustment.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("$G$11")) Is Nothing Then
Application.EnableEvents = False
Range("$B$20:$R$25,$Z$20:$AM$25").ClearContents
Range("$F$16:$Q$16,$R$15:$U$16,$V$16:$AA$16,$AB$15:$AM$16").ClearContents
If Range("I16") = 120 Or Range("I16") = 480 Then 'presumably belongs elswhere as just cleared I16 above?
Range("F16") = 200000
Else
Range("F16").Clear
End If
End If
Application.EnableEvents = True
exitHandler:
Application.EnableEvents = True
Exit Sub
End Sub
I want to clear the contents of the cell after clicking the ok button in a message pop up window.
When the pop up window disappears, after clicking ok button umpteen times, the script terminates by throwing the below error
Run time error '-2147417848(80010108)':
Method 'Range of object'_Worksheet'Failed
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Set KeyCells = Range("N4:O4")
If Not Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
If (Range("E9") = "" Or Range("F9") = "") Then
MsgBox "Reopen Date and Closed Date should not be populated before calculating the Benefit Begin Date and Maximum Benefit Date"
Sheets("Calculation Tool").Range("N4").Clear ----->Code written to clear the cells
Else
If (Range("N4") = "" Or Range("O4") = "") Then
Set b1 = Sheets("Calculation Tool").CommandButton22
b1.Enabled = False
Else
Set b1 = Sheets("Calculation Tool").CommandButton22
b1.Enabled = True
End If
End If
End If
End Sub
I wanted to tell #BigBen that his suggestion worked for me, but my low rep won't allow me to comment. The answer field is the only way of expression for me!
So I might as well formulate a valid answer, here it goes. :)
So I had the same problem within a Worksheet_Change event macro, in this casual event macro:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim RangeToCheck As Range
Set RangeToCheck = ActiveSheet.Range("O3:O32")
(above is the line that triggered randomly that Run time error '-2147417848(80010108)' you encountered; on with the script)
If Not Application.Intersect(Target, RangeToCheck) Is Nothing Then
Target.Value = VBA.Replace(Target.Value, ".", ",")
Debug.Print Target.Address, Target.Value
Else
Debug.Print "Not in RangeToCheck"
End If
End Sub
Following BigBen's link, I found that the following code works fine :
Private Sub Worksheet_Change(ByVal Target As Range)
Dim RangeToCheck As Range
On Error GoTo enableEventsOn:
Application.EnableEvents = False
Set RangeToCheck = ActiveSheet.Range("O3:O32")
Application.EnableEvents = True
On Error GoTo 0
If Not Application.Intersect(Target, RangeToCheck) Is Nothing Then
Target.Value = VBA.Replace(Target.Value, ".", ",")
Debug.Print Target.Address, Target.Value
Else
Debug.Print "Not in RangeToCheck"
End If
enableEventsOn:
Application.EnableEvents = True
End Sub
I have created a form that will give the user the choice to pick from 7 different options whcih will all be default blank. When they click the cell next to the option it will change from blank to "yes" and when clicked again it will remove the text and so on. The issue I have its the cell that is clickable from blank to "yes" is merged between R33 and S33. The code works on the cell R33 alone but not when I merge them. Can you help me out with this please?
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Application.EnableEvents = False
If Target.Cells.Count = 1 Then
If Not Intersect(Target, Range("R33")) Is Nothing Then
Select Case Target.Value
Case ""
Target.Value = "yes"
Case "yes"
Target.Value = ""
End Select
Range("A1").Select
End If
End If
Application.EnableEvents = True
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
On Error GoTo Worksheet_SelectionChange_Error
Application.EnableEvents = False
Debug.Print Target.Cells.Count 'just FYI, remove it later
If Not Intersect(Target.Cells(1), Range("R33").MergeArea) Is Nothing Then
Select Case Target.Cells(1)
Case "yes"
Target.Cells(1) = vbNullString
Case vbNullString
Target.Cells(1) = "yes"
End Select
End If
Range("A1").Select
Application.EnableEvents = True
On Error GoTo 0
Exit Sub
Worksheet_SelectionChange_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ")
Application.EnableEvents = True
End Sub
The merged cells are a bit evil in Excel, but if you play around a bit you can achieve what you need:
Target.Cells.Count is equal to the number of merged cells, thus it is never 1. I have deleted it;
Target.Cells(1) is the way to refer the first cell of the MergedArea;
Range("R33").MergeArea is a good way to check the intersect;
As sometimes while executing _SelectionChange event, you may get an error and then leave the Application.EnableEvents = False, it is a good practice to use an Error Catcher, which sets it back to True;