Compatability error 2003 - 2016 macro in Excel - excel

Quick query about some VBA. I have recently moved from Excel 2003 to 2016 (365), and there are issues with transferring code.
I have a cell you input a number in. When you press enter after editing the cell, it adds that value to a cell 2 cells to the right, and keeps a tally. It then erases the value in the original cell.
Sub CasesChecked()
If Sheets("Work Return").Range("F13") = "" Then
Sheets("Work Return").Unprotect "adminstats"
Sheets("Work Return").Range("F13") = Sheets("Work Return").Range("D13")
Sheets("Work Return").Range("D13") = ""
Sheets("Work Return").Protect "adminstats"
Else
Sheets("Work Return").Unprotect "adminstats"
Sheets("Work Return").Range("F13") = Sheets("Work Return").Range("F13") + Sheets("Work Return").Range("D13")
Sheets("Work Return").Range("D13") = ""
Sheets("Work Return").Protect "adminstats"
End If
End Sub
The code above is found in Module1, and in Sheet1 there is the code below. This confirms the cell change through an intersect:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("D13")) Is Nothing Then
CasesChecked
Else
If Not Intersect(Target, Target.Worksheet.Range("D17")) Is Nothing Then
CasesChecked2
Else
End If
End If
End Sub
Unfortunately, I get the dreaded "run-time error '-2147417848 method range of object _worksheet failed" and can't quite figure out why. The code is simple, I don't know where it falters.
Any advice would be hugely appreciated
Thank you,
Ryan

I would first make this change and try again:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("D13")) Is Nothing Then
Application.EnableEvents = False
CasesChecked
Application.EnableEvents = True
Else
If Not Intersect(Target, Target.Worksheet.Range("D17")) Is Nothing Then
Application.EnableEvents = False
CasesChecked2
Application.EnableEvents = True
Else
End If
End If
End Sub
EDIT#1:
In the original code, Worksheet_Change() responded to user-initiated changes. But it also responded to changes made by CasesChecked().
This created a malicious loop. The new code avoids this.

Related

Excel VBA Function throws an error when changes the cell

I have been using this code which copies the range and paste the range as picture but when i change the concerns cell it throws an error that is Error 1004, Microsoft Excel cannot paste the data.
Any help will be appreciated.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("C5:P5")) Is Nothing Then
Application.CutCopyMode = TRUE
ActiveSheet.Pictures.Delete
Worksheets("Pivot").Range("FC3:FP35").Copy
With Worksheets("Map")
.Activate
.Range("C8").Select
.Pictures.Paste
End With
Application.CutCopyMode = FALSE
End If
End Sub
You need to turn off events Application.EnableEvents = False before changing cells and turn them on after. Make sure they get turned on in any case of an error in this event or you will not be able to fire any other events in your Excel instance. So error handling in this event is a must have. • You might benefit from reading
How to avoid using Select in Excel VBA.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("C5:P5")) Is Nothing Then
On Error Goto ERR_ENABLE_EVENTS
Application.EnableEvents = False
Me.Pictures.Delete
Worksheets("Pivot").Range("FC3:FP35").Copy Destination:=Worksheets("Map").Range("C8").Paste
End If
ERR_ENABLE_EVENTS:
Application.CutCopyMode = False
Application.EnableEvents = True
If Err.Number <> 0 Then
Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext
End If
End Sub

Excel VBA Target.Address being modified and causing Error 13 type mismatch

Prototypical post: New to VBA, unable to resolve an issue after having read multiple posts/websites, and now turning to all the fantastic people here who's posts have gotten me this far.
I have a worksheet with data validation in column C (list; forced-choice Yes/No options). If user selects "No" in C7, then C9:C11 need to automatically and immediately populate as "No." I have gotten this to work by the following:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$7" And Target.Value = "No" Then
Range("$C$9").Value = "No"
Range("$C$10").Value = "No"
Range("$C$11").Value = "No"
End If
End Sub
I also have a text box on the same worksheet (i.e., Sheet5) that when clicked fires a macro which clears the contents of C6:C7. This Reset macro is in a module under General.
Sub C_B_Reset()
Sheet5.Range("C6:C7").ClearContents
End Sub
Individually, these work fine, but when both exist it results in Type 13 error at the Target.Address after the Reset macro is fired. After firing the Reset macro, the "If Target.Address" portion resolves to the range referenced in the Reset macro (i.e., C6:C7). Because "If Target.Address" expects a single, absolute cell reference (e.g., $C$7), it is throwing the mismatch code because it instead is resolving to (C6:C7) when the mouse is hovered over it.
Even if the Reset macro is completely deleted, the same issue happens if the following is used in the Target.Address code:
Range("$C$9:$C$11").Value = "No"
The Target.Address then resolves to "$C$9:$C$11" and throws the Type 13 mismatch error.
It appears that if "Range" is used to refer to a range of cells in any other macro, it automatically gets assigned as Target.Address. However, this doesn't happen if Range only refers to single cells (which is why there are separate lines for C9 to C11 in the Worksheet_Change code).
I'm sure I'm using incorrect terminology, but I hope I explained it well enough, because I sure would appreciate some help.
Thanks for taking a look,
"Excel VBA Target.Address being modified and causing Error 13 type mismatch"
Target.Address isn't the problem here... Target is the cell(s) that were changed, so Target.Address will be $C$6:$C$7 when you clear both C6 and C7.
The main problem is this:
... And Target.Value = "No" ...
This will fail with a Type Mismatch error when Target is a multi-cell range, because then Target.Value is a 2D Variant array, which you can't compare to "No".
Also, the normal approach is to use Intersect instead of considering Target.Address.
If you're only concerned about C7, then perhaps write like this:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("C7")) Is Nothing Then
If Me.Range("C7").Value = "No" Then
On Error GoTo SafeExit
Application.EnableEvents = False ' Avoid re-triggering the event
Me.Range("C9:C11").Value = "No"
End If
End If
SafeExit:
Application.EnableEvents = True
End Sub
Consider:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$7" And Target.Value = "No" Then
Application.EnableEvents = False
Range("$C$9").Value = "No"
Range("$C$10").Value = "No"
Range("$C$11").Value = "No"
Application.EnableEvents = True
End If
End Sub
and:
Sub C_B_Reset()
Application.EnableEvents = False
Sheet5.Range("C6:C7").ClearContents
Application.EnableEvents = True
End Sub
EDIT#1:
Try this event macro instead:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$7" Then
If Target.Value = "No" Then
Application.EnableEvents = False
Range("$C$9").Value = "No"
Range("$C$10").Value = "No"
Range("$C$11").Value = "No"
Application.EnableEvents = True
End If
End If
End Sub

Autofill cells based on drop down list in excel

I am trying to creata a VBA that gives me automatic values based on drop down list in a form. The problem is that when I run the macro then it is causing an error and excel stops working. Any help in this case is most welcome.
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("$G$11") = "UD Sheet" Then
Rows("20:25").EntireRow.Hidden = False
Else
Rows("21:25").EntireRow.Hidden = True
End If
If Range("G12").Value = "Flex Tape" Then
Range("B20").Value = "None"
Else
Range("B20").Value = ""
End If
exitHandler:
Application.EnableEvents = True
Exit Sub
End Sub
First thing first, in your code, no need to put an Exit Sub before the End Sub.
The code will end after that line so this is a redundancy.
The next thing that you need to understand is that the Change Event will keep triggering if you will not disable it explicitly. So it means that when you hide a row on that Sheet, the Change Event will keep on happening since there will be changes that will happen on the Sheet. i.e. (Hiding Rows).
To do that you need to disable the EventsListeners of the application using the Application.EnableEvents = False. So the application can do a single thing based on that first event.
The next thing that you need to keep in mind is to track where the Changes occur and fire your program. Target is a Range Object that will return the Range where the specific change occurs on the Sheet.
In order to do that, you need to validate if you need to trigger the routine based on the target using the Intersect function.
The whole code is as follows:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Not Intersect(Target, Range("G11")) Is Nothing Then
If Range("$G$11") = "UD Sheet" Then
Rows("20:25").EntireRow.Hidden = False
Else
Rows("21:25").EntireRow.Hidden = True
End If
End If
If Not Intersect(Target, Range("G12")) Is Nothing Then
If Range("G12").Value = "Flex Tape" Then
Range("B20").Value = "None"
Else
Range("B20").Value = ""
End If
End If
Application.EnableEvents = True
End Sub

EXCEL : Circular reference issue

I want to check the value of the current cell
if it's null then generate a random number
else keep it as it is.
=IF(P4<>0,RANDBETWEEN(1,100),P4)
but when I open the sheet I get an issue of circular reference and the value of P4 is changing even though old value is not null
Put this code in the worksheet's private code sheet (right-click worksheet tab, View Code).
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If not intersect(Target, Range("P4")) Is Nothing Then
On Error GoTo safe_exit
Application.EnableEvents = False
If Range("P4") = vbNullString Then
Range("P4") = Application.RandBetween(1, 100)
End If
End If
safe_exit:
Application.EnableEvents = True
End Sub

Excel macro code for clearing formulas in cells does not work when the sheet is protected

After some googling I finally found some code where I could prevent users from placing formulas inside cells. It works great, that's until I protected the sheet. Can anyone tell me what I'm doing wrong? I'm really new to VB.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.EnableEvents = False
On Error Resume Next
Range("I39").SpecialCells(xlCellTypeFormulas).ClearContents
On Error GoTo 0
Application.EnableEvents = True
End If
End Sub
The entire code for my sub is as follows. I need to stop users from pasting in the cells and putting formulas in them.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("C26")) Is Nothing Then
Application.CutCopyMode = True
Application.EnableEvents = False
On Error Resume Next
Range("C26").SpecialCells(xlCellTypeFormulas).ClearContents
On Error GoTo 0
Application.EnableEvents = True
End If
End Sub
Here is a version that facilitates formula checking over a range of cells:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rNoFormulas As Range
Set rNoFormulas = Range("C26:I26")
If Intersect(Target, rNoFormulas) Is Nothing Then Exit Sub
If Target.HasFormula Then
Application.EnableEvents = False
Target.ClearContents
MsgBox "formulas not allowed in cell " & Target.Address
Target.Select
Application.EnableEvents = True
End If
End Sub
If you want to allow data entry in cell C26, but not formula entry, then use the Change Event:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rNoFormulas As Range
Set rNoFormulas = Range("C26")
If Intersect(Target, rNoFormulas) Is Nothing Then Exit Sub
If rNoFormulas.HasFormula Then
Application.EnableEvents = False
rNoFormulas.ClearContents
MsgBox "formulas not allowed in cell C26"
rNoFormulas.Select
Application.EnableEvents = True
End If
End Sub
If you just want to protect certain cells only, no vba code is need.
follow this step :
Open sheet that contains cells or columns that you want to protect, press ctrl while selecting those cells or column to be protect, then right click, choose format cells, choose protection tab and uncheck the locked option. those cells or column will not be locked although you have protected the sheet. default setting is all cells in the sheets is locked so you must choose which cells you want to unlock while protecting the sheet. you may record a macro if you still want to use vba. hope this help

Resources