Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default using VBA to go back to the original value before the cell was changed

I am trying to use VBA to do the following:

if there's a change to the worksheet,
if a cell is green
don't allow the change; go back to the original
value before the cell was changed.
else (cell is not green)
allo change

my problem is the "go back to the original value before
the cell was changed." any ideas how i might go about this?

i would just lock the fields and protect the sheet, but
the workbook is shared and i don't want to protect the
sheet in the shared workbook because then i can't insert
rows without unprotecting the workbook, etc.

thanks.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 620
Default using VBA to go back to the original value before the cell was changed

Mika,

Here you go

Dim myVar

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count = 1 Then
Application.EnableEvents = False
On Error GoTo ws_exit
If Target.Interior.ColorIndex = 10 Then
Target.Value = myVar
MsgBox "Change cancelled", vbInformation
End If
ws_exit:
Application.EnableEvents = True
End If
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
myVar = Target.Value
End Sub


--

HTH

Bob Phillips

"mika." wrote in message
...
I am trying to use VBA to do the following:

if there's a change to the worksheet,
if a cell is green
don't allow the change; go back to the original
value before the cell was changed.
else (cell is not green)
allo change

my problem is the "go back to the original value before
the cell was changed." any ideas how i might go about this?

i would just lock the fields and protect the sheet, but
the workbook is shared and i don't want to protect the
sheet in the shared workbook because then i can't insert
rows without unprotecting the workbook, etc.

thanks.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 39
Default using VBA to go back to the original value before the cell was changed

Hi,



First, in the even Selection _Change store what in the target cell in a
global variable. Then, if they could not change the cell put it back.





Private varBefore As Variant



Private Sub Worksheet_Change(ByVal Target As Range)

Dim MyCell As Range

For Each MyCell In Selection

If MyCell.Interior.ColorIndex = 35 Then

MyCell.Value = varBefore

MsgBox "You can't change this fields"

End If

Next

GoTo ending



End Sub



Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Target.Value = varBefore

End Sub






--
JP

http://www.solutionsvba.com


"mika." wrote in message
...
Hi John,

I don't think I understand what you said: are you advising
me to take what I have in my worksheet, copy it into
another sheet, and then if the change should not be made,
use the other sheet to get the right value back in the
cell?

This is what I have so far:

Private Sub Worksheet_Change(ByVal Target As Range)

For Each cell In Selection
If cell.Interior.ColorIndex = 35 Then
GoTo error
End If
Next
GoTo ending

error: MsgBox ("You cannot change these fields
'Code to not change the fields

ending:
End If

...Could you please explain what you might put in
the 'Code to not change the fields part?

Thanks so much.



-----Original Message-----
mika,

1) Store the previous value [and possibly cell address]

as a named range, or
2) If this is too insecure, use a Hidden Sheet.

Use the following Event to store the values away at the

end of
SelectionChange, ready to be restored if you need them.

Private Sub Worksheet_SelectionChange(ByVal Target As

Range)

End Sub

regards,

JohnI


"mika." wrote in message
...
I am trying to use VBA to do the following:

if there's a change to the worksheet,
if a cell is green
don't allow the change; go back to the original
value before the cell was changed.
else (cell is not green)
allo change

my problem is the "go back to the original value before
the cell was changed." any ideas how i might go about

this?

i would just lock the fields and protect the sheet, but
the workbook is shared and i don't want to protect the
sheet in the shared workbook because then i can't insert
rows without unprotecting the workbook, etc.

thanks.



.



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Going back to original location Té Excel Discussion (Misc queries) 1 August 27th 09 03:06 AM
Excel shortcut key has changed from original setting Rich Excel Discussion (Misc queries) 2 October 21st 08 05:33 PM
the cell refs at the top changed to numbers, how go back lettters kathy Excel Discussion (Misc queries) 2 December 6th 07 04:22 PM
How do i recover original excel sheet that was changed and saved mcf Excel Discussion (Misc queries) 2 January 31st 07 08:33 AM
Changed column width keeps reverting back to original size David Boone Excel Discussion (Misc queries) 3 May 29th 05 01:22 PM


All times are GMT +1. The time now is 04:26 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"