Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with VB?

Status
Not open for further replies.

pappyb

Technical User
Apr 30, 2009
7
US
Hello all. I hope this is in the right forum, but I'm having the hardest time understanding what I'm doing wrong with the following code. It's in an excel spreadsheet. If I leave the sheet unprotected, it works just fine.

If I protect the sheet and enter something in column b I get the following error:
Unable to set the NumberFormat property of hte Range Class

Can someone please help me to understand what I'm doing wrong? Thanks all!

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
With Target
If .Count > 1 Then Exit Sub
If Not Intersect(Range("B3:B100"), .Cells) Is Nothing Then
Application.EnableEvents = False
If IsEmpty(.Value) Then
.Offset(0, -1).ClearContents
Else
With .Offset(0, -1)
.NumberFormat = "mm/dd/yy"
.Value = Date
End With
End If
Application.EnableEvents = True
End If
End With
End Sub

 
>I hope this is in the right forum

Nope ...

Try here: forum707
 
Thanks for the heads up!


I will post my question there.
 
Protecting a worksheet makes it read-only, so of course you can't modify it (either as a user or through code).
 
You can use the VbFileAttribute commands to unwrite protect the file so you can do what you need to do then write protect it after you have finished.

SetAttr app.path & "/file.xml", VbFileAttribute.vbNormal
do events

your code here

do events
SetAttr app.path & "/file.xml", VbFileAttribute.vbReadOnly


Think this will work, been a few years since I used VB6
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top