Getting Debug Error and don't know why

dougdrex

Board Regular
Joined
Sep 27, 2013
Messages
79
Using Excel 2010

I'm getting debug errors and can't figure out why. There is VBA code in the background, and I've searched through the code but don't see any obvious errors.

It seems like the debug error only comes up when the sheet is protected. Is there a problem with VBA running while a sheet is protected?

Thanks in advance for your insight!
Doug
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
It seems like the debug error only comes up when the sheet is protected. Is there a problem with <acronym title="visual basic for applications" style="border-width: 0px 0px 1px; border-bottom-style: dotted; border-bottom-color: rgb(0, 0, 0); cursor: help; color: rgb(51, 51, 51); background-color: rgb(250, 250, 250);">VBA</acronym> running while a sheet is protected?
It depends on exactly what the VBA code is trying to do, but if it is doing something like trying to update a cell in a Protected range, yes, that would cause errors.

Often times, if you hit the "Debug" button in the error window, it will take you right to the line of code that is having issues.
 
Upvote 0
You can unprotect/reprotect the sheet(s) in your code:

Code:
Sheets("Sheet1").Protect
  ' Your code here
Sheets("Sheet1").Unprotect

HTH,
 
Upvote 0
I used a range in the code. Some of the cells are locked, while others are not. I thought it would just be easier that way. Let me know if I need to alter the code to only include cells that are unprotected.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)[INDENT]If Not Intersect(Target, Target.Worksheet.Range("C17")) Is Nothing Then[/INDENT]
        If Range("C17") = "YES" Or Range("C17") = "NO" Or Range("C17") = "N/A" Or Range("C17") = vbNullString Then
            Range("C18:D26").Value = vbNullString[INDENT]        End If[/INDENT]
    End If
 
Upvote 0
The VBA is definitely updating values certain values, especially with this line:
Code:
Range("C18:D26").Value = vbNullString

You will need to unprotect the range before you can update it.
Smitty's post show show you can protect/unprotect ranges via VBA code.
Here is a link to some code that shows you to protect/unprotect in VBA when passwords are involved: Excel-VBA : Range - Lock/Unlock Specific Cells
 
Upvote 0
If C18:D26 are locked then:

Code:
ActiveSheet.Unprotect
  Range("C18:D26").Value = vbNullString
ActiveSheet.Protect
 
Upvote 0
Thank you both for your help. I'm learning about using VBA, and appreciate your valuable feedback.

Doug
 
Upvote 0

Forum statistics

Threads
1,214,521
Messages
6,120,018
Members
448,937
Latest member
BeerMan23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top