VBA script for checkbox not working - excel

I have a problem in Excel 2013. Yesterday I put the following code in Excel by rightclicking the tab of my worksheet (Alt-F11) :
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("D3:T42")) Is Nothing Then
Application.EnableEvents = False
If Target.Value = ChrW(&H2713) Then
Target.ClearContents
Cancel = True
Else
Target.Value = ChrW(&H2713)
Cancel = True
End If
End If
Application.EnableEvents = True
End Sub
This code is supposed to add a checkmark in the defined cells after double clicking. While this code worked fine yesterday, it now does not work anymore. I have tried everything but just do not get it to work. Any ideas ?
PS I would like to use such a code since a sheet with many form checkboxes makes it very slow (at least in my case)
Regards, Arno

Some error in one test may have made EnableEvents keep false eternally.
Run a single sub with Application.EnableEvents = true and test it again.
If that is false, no event will be thrown at all, no clicks, no double clicks, nothing will work.
I suggest you add an On error goto statement, to avoid that kind of problem:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
On Error goto 1
If Not Intersect(Target, Range("D3:T42")) Is Nothing Then
Application.EnableEvents = False
If Target.Value = ChrW(&H2713) Then
Target.ClearContents
Cancel = True
Else
Target.Value = ChrW(&H2713)
Cancel = True
End If
End If
on error goto 0
1 Application.EnableEvents = True
End Sub

Related

Type mismatch, conflicting macros

The first VBA script I have is working fine,
Private Sub Worksheet_Change(ByVal Target As Range)
If Not (Application.Intersect(Target, Range("C9:G9,C15:G15,C21:G21,C27,G27")) _
Is Nothing) Then
With Target
If Not .HasFormula Then
Application.EnableEvents = False
.Value = UCase(.Value)
Application.EnableEvents = True
End If
End With
End If
End Sub
This makes all text entered in a cell capitalize after entered by user
Then I have a macro button to clear certain cells
Sub inputcaps()
Range("C9", "G9").Value = ""
End Sub
After pressing this macro it works fine, but I do get a "Run-time error '13': Type mismatch" error and the first script stops working and I have to restart the excel sheet.
How can I fix this??
The debug takes me to .Value = UCase(.Value) from the first script
thank you
You need to do following this code:-
Not More Issue in code simple First EnableEvents = false and before finished march write EnableEvents = true
Like this Code :
Sub inputcaps()
Application.EnableEvents = False
Range("C9", "G9").Value = ""
Application.EnableEvents = True
End Sub
vba excel mayurpathak

How do I enable the check box again?

I have VBA code which checks the value of a cell and should enable/disable a checkbox depending on the value:
Private Sub CheckBox2_Click()
If Range("A4").Value = "ZJ3" Then
CheckBox2.Enabled = True
ElseIf Range("A4").Value = "ZJ2" Then
CheckBox2.Enabled = False
CheckBox2.Value = False
End If
End Sub
When I select ZJ2 in cell A4, the checkbox does what is intended, and disables the value and the entry of the checkbox.
When I change the value of ZJ3 it remains greyed out when it should enable.
It might be better to use the Worksheet_Change event to control whether the checkbox is enabled or not, something like the following:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Me.Range("A4")) Is Nothing Then Exit Sub
Select Case Me.Range("A4").Value
Case "ZJ3"
Me.CheckBox2.Enabled = True
Case "ZJ2"
Me.CheckBox2.Enabled = False
Me.CheckBox2.Value = False
End Select
End Sub

Macro that autofills the cell based on drop down menu in excel

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

How to output barcode scanner information in Excel

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

Cancel = True is not working in Workbook_BeforeClose

This clearly shows that value of cancel is true, and still my workbook closes after this! Why?
I want to cancel closing of the excel file if user input is incorrect.
I debug the code and found out that the value of Cancel was initially False, and it becomes True. But once the Sub ends, file is still closed.
(variable error is defined at workbook declaration space, so accessible within module.)
Private Sub Workbook_BeforeClose(Cancel As Boolean)
error = 0
check_for_error
If error = 1 Then
Cancel = True
Exit Sub
End If
Application.EnableEvents = False
Sheet1.Cells.ClearContents
Sheet2.Cells.Clear
Application.EnableEvents = True
End Sub
I want to cancel closing of file if error = 1.
Update:: Thanks everyone for the responses, but nothing seems to work for me! Following is how my current code looks.
Option Explicit
Dim errYes As Byte
Private Sub Workbook_BeforeClose(Cancel As Boolean)
check_freeze_panel
If errYes = 1 Then
Cancel = True
Exit Sub
End If
Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True
End Sub
errYes is returning 1, Cancel = True is being executed, and still my workbook closes.
I even tried commenting all the code, and just putting Cancel = True as suggested and guess what, it still closes!
Change the name of error this is a reserved word for an already existing function (see Error function) and must not be used as a variable name. And activate Option Explicit and declare all your variables.
Here is an example how you would do it correctly:
Option Explicit
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim ErrorsFound As Boolean
ErrorsFound = check_for_error
If ErrorsFound Then
Cancel = True
Exit Sub
End If
Application.EnableEvents = False
Sheet1.Cells.ClearContents
Sheet2.Cells.Clear
Application.EnableEvents = True
End Sub
Function check_for_error() As Boolean
'check and return
check_for_error = True 'errors found
End Function
Or even shorter:
Option Explicit
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Cancel = check_for_error
If Not Cancel Then
Application.EnableEvents = False
Sheet1.Cells.ClearContents
Sheet2.Cells.Clear
Application.EnableEvents = True
End If
End Sub
Function check_for_error() As Boolean
'check and return
check_for_error = True 'errors found
End Function
Try this. Set Error equal to your function call and make sure it returns the right data type.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
error = check_for_error
If error = 1 Then
Cancel = True
Exit Sub
End If
Application.EnableEvents = False
Sheet1.Cells.ClearContents
Sheet2.Cells.Clear
Application.EnableEvents = True
End Sub
Initially Cancel is equal False, means File Close will succeed.
For me the below code works fine. When I set error = 1 then closing is cancelled whether you use "Exit Sub" or not. When I set error <> 1 then cls
When I test your Code:
Option Explicit
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim error As Integer
error = 1
If error = 1 Then
Cancel = True
'Exit Sub
End If
End Sub
Just test this:
Write False and workbook should Close, and True should "stop" the closing.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Cancel = False
End Sub

Resources