d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help > Vba Crashing
Add Reply New Topic New Poll
Member
Posts: 11,643
Joined: Dec 18 2006
Gold: 340.00
Oct 14 2015 04:18pm
So I am writing a simple code that essentially conditionally locks a cell whenever the cell next to it is changed to a certain value. When the cell locks, I want to change its value to "Not Applicable". However, when I change one line of code to read cell.value = "Not Applicable", Excel crashes.

Code
Private Sub Worksheet_Change(ByVal Target As Range)

Dim a As Integer

a = ActiveCell.Row

ActiveSheet.Unprotect Password:="secret"

If Range("D" & a) = "x" And (Range("E" & a) = "y" Or Range("E" & a) = "z") Then
Range("F" & a).Locked = False
Range("F" & a).Interior.ColorIndex = 2
ElseIf Range("D" & a) = "w" And (Range("E" & a) = "y" Or Range("E" & a) = "z") Then
Range("G" & a).Locked = False
Range("G" & a).Interior.ColorIndex = 2
Else
'Range("F" & a).value = "Not applicable" <<<<< This one line breaks Excel and causes it to crash. Everything else works fine
Range("F" & a).Locked = True
Range("F" & a).Interior.ColorIndex = 15
Range("G" & a).Locked = True
Range("G" & a).Interior.ColorIndex = 15
End If

ActiveSheet.Protect Password:="secret"

End Sub


The cell that I am trying to change the value of has a dropdown based on another list but that list contains the value "Not applicable" so it should not be an issue as far as I know?
Thank you!
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Oct 14 2015 06:08pm
well for one, based on the documentation, .value is supposeod to be capitalized (.Value)

2nd, and I am not sure how much the compiler will help you here, but

Code
expression .Value(RangeValueDataType)


it appears to accept a RangeValueDataType. Not sure if it will accept a string or not (never used this before)

here is the page I was looking at:

https://msdn.microsoft.com/EN-US/library/office/ff195193.aspx
Member
Posts: 11,643
Joined: Dec 18 2006
Gold: 340.00
Oct 14 2015 10:02pm
Quote (Eep @ Oct 14 2015 07:08pm)
well for one, based on the documentation, .value is supposeod to be capitalized (.Value)

2nd, and I am not sure how much the compiler will help you here, but

Code
expression .Value(RangeValueDataType)


it appears to accept a RangeValueDataType. Not sure if it will accept a string or not (never used this before)

here is the page I was looking at:

https://msdn.microsoft.com/EN-US/library/office/ff195193.aspx


The V would capitalize... Hand typed it on jsp since I had to remove it from vba :P but thanks! Will check out the page as soon as I can (out and on my phone atm)
Member
Posts: 11,643
Joined: Dec 18 2006
Gold: 340.00
Oct 15 2015 01:06pm
Figured it out! I had to set EnableEvents to false within the macro. When the value of the cell was changed, it reactivated the macro in an infinite loop. Not sure how I didn't catch that earlier :huh:
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Oct 15 2015 06:28pm
glad you figured it out....I am not terribly familiar with the excel stuff in .net
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll