PC Review


Reply
Thread Tools Rate Thread

How do I disable cell editing in vba?

 
 
DRICE
Guest
Posts: n/a
 
      28th Mar 2008
I am trying to programmaticly (vba) prevent users from editing any cell in
one specific column without haveing to 'protect' the entire worksheet.
 
Reply With Quote
 
 
 
 
Mike H
Guest
Posts: n/a
 
      28th Mar 2008
Hi,

Right click the sheet tab, view code and paste this in. 3=Column C so change
to suit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Column = 3 Then
MsgBox "keep out of there"
Target.Offset(0, 1).Select
End If
End Sub

A word of caution, this fails completely if the user doesn't enable macros
and isn't particularly secure if they do.

Mike

"DRICE" wrote:

> I am trying to programmaticly (vba) prevent users from editing any cell in
> one specific column without haveing to 'protect' the entire worksheet.

 
Reply With Quote
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      28th Mar 2008
You might consider something like the following. Copy/Paste the code below
into the code window for the worksheet you want to block entries on. Change
the two occurrences of the number 6 (used for Column F in my example) to the
number corresponding to the column you want to block entries on. What the
code will do is let the user change the value in the column, but then it
will advise the user he can't change the existing value and then replace
that entered value with the value that was in the cell originally.

Dim OldValue As Variant

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 6 Then
On Error GoTo Whoops
Application.EnableEvents = False
MsgBox "Values in this column cannot be changed!"
Target.Value = OldValue
End If
Whoops:
Application.EnableEvents = True
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Column = 6 Then OldValue = Target.Value
End Sub

Rick


"DRICE" <(E-Mail Removed)> wrote in message
news:2B5FC1F5-4CC0-4B73-A7C3-(E-Mail Removed)...
>I am trying to programmaticly (vba) prevent users from editing any cell in
> one specific column without haveing to 'protect' the entire worksheet.


 
Reply With Quote
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      28th Mar 2008
I forgot to mention... same cautions as Mike gave you in his posting, "This
fails completely if the user doesn't enable macros and isn't particularly
secure if they do."

Rick


"Rick Rothstein (MVP - VB)" <(E-Mail Removed)> wrote in
message news:O$(E-Mail Removed)...
> You might consider something like the following. Copy/Paste the code below
> into the code window for the worksheet you want to block entries on.
> Change the two occurrences of the number 6 (used for Column F in my
> example) to the number corresponding to the column you want to block
> entries on. What the code will do is let the user change the value in the
> column, but then it will advise the user he can't change the existing
> value and then replace that entered value with the value that was in the
> cell originally.
>
> Dim OldValue As Variant
>
> Private Sub Worksheet_Change(ByVal Target As Range)
> If Target.Column = 6 Then
> On Error GoTo Whoops
> Application.EnableEvents = False
> MsgBox "Values in this column cannot be changed!"
> Target.Value = OldValue
> End If
> Whoops:
> Application.EnableEvents = True
> End Sub
>
> Private Sub Worksheet_SelectionChange(ByVal Target As Range)
> If Target.Column = 6 Then OldValue = Target.Value
> End Sub
>
> Rick
>
>
> "DRICE" <(E-Mail Removed)> wrote in message
> news:2B5FC1F5-4CC0-4B73-A7C3-(E-Mail Removed)...
>>I am trying to programmaticly (vba) prevent users from editing any cell in
>> one specific column without haveing to 'protect' the entire worksheet.

>


 
Reply With Quote
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      28th Mar 2008
Building off of your concept, code like this should be able to be used to
return the user to the cell they were in prior to trying to go into the
forbidden column rather than just moving them over to the column next to the
forbidden column...

Dim OldCell As Range

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim PreviousCell As Range
Const ForbiddenColumn As Long = 1
If OldCell Is Nothing Then
Set OldCell = Cells(1, 1 - (ForbiddenColumn = 1))
End If
Set PreviousCell = OldCell
If Target.Column = ForbiddenColumn Then
MsgBox "You are not allowed in this column!"
PreviousCell.Select
End If
Set OldCell = Target
End Sub

As written, if the user attempts to go into the forbidden column right away,
he/she will be returned to A1, unless Column A is the forbidden column, in
which case he/she will be returned to B1. After that, he/she will be
returned to the previously occupied cell. We could probably fix this minor
flaw by putting OldCell in a Module, and using the Workbook_SheetActivate
event from the Workbook to store the currently active cell into the OldCell
variable. I didn't test that out; it just seems like that ought to work.

Rick


"Mike H" <(E-Mail Removed)> wrote in message
news:54E00400-B497-4FB3-BB57-(E-Mail Removed)...
> Hi,
>
> Right click the sheet tab, view code and paste this in. 3=Column C so
> change
> to suit
>
> Private Sub Worksheet_SelectionChange(ByVal Target As Range)
> If Target.Column = 3 Then
> MsgBox "keep out of there"
> Target.Offset(0, 1).Select
> End If
> End Sub
>
> A word of caution, this fails completely if the user doesn't enable macros
> and isn't particularly secure if they do.
>
> Mike
>
> "DRICE" wrote:
>
>> I am trying to programmaticly (vba) prevent users from editing any cell
>> in
>> one specific column without haveing to 'protect' the entire worksheet.


 
Reply With Quote
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      28th Mar 2008
And, of course, to match your example, I should have set ForbiddenColumn to
3, not the 1 I used for testing the Column A problem.

Rick


"Rick Rothstein (MVP - VB)" <(E-Mail Removed)> wrote in
message news:(E-Mail Removed)...
> Building off of your concept, code like this should be able to be used to
> return the user to the cell they were in prior to trying to go into the
> forbidden column rather than just moving them over to the column next to
> the forbidden column...
>
> Dim OldCell As Range
>
> Private Sub Worksheet_SelectionChange(ByVal Target As Range)
> Dim PreviousCell As Range
> Const ForbiddenColumn As Long = 1
> If OldCell Is Nothing Then
> Set OldCell = Cells(1, 1 - (ForbiddenColumn = 1))
> End If
> Set PreviousCell = OldCell
> If Target.Column = ForbiddenColumn Then
> MsgBox "You are not allowed in this column!"
> PreviousCell.Select
> End If
> Set OldCell = Target
> End Sub
>
> As written, if the user attempts to go into the forbidden column right
> away, he/she will be returned to A1, unless Column A is the forbidden
> column, in which case he/she will be returned to B1. After that, he/she
> will be returned to the previously occupied cell. We could probably fix
> this minor flaw by putting OldCell in a Module, and using the
> Workbook_SheetActivate event from the Workbook to store the currently
> active cell into the OldCell variable. I didn't test that out; it just
> seems like that ought to work.
>
> Rick
>
>
> "Mike H" <(E-Mail Removed)> wrote in message
> news:54E00400-B497-4FB3-BB57-(E-Mail Removed)...
>> Hi,
>>
>> Right click the sheet tab, view code and paste this in. 3=Column C so
>> change
>> to suit
>>
>> Private Sub Worksheet_SelectionChange(ByVal Target As Range)
>> If Target.Column = 3 Then
>> MsgBox "keep out of there"
>> Target.Offset(0, 1).Select
>> End If
>> End Sub
>>
>> A word of caution, this fails completely if the user doesn't enable
>> macros
>> and isn't particularly secure if they do.
>>
>> Mike
>>
>> "DRICE" wrote:
>>
>>> I am trying to programmaticly (vba) prevent users from editing any cell
>>> in
>>> one specific column without haveing to 'protect' the entire worksheet.

>


 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      28th Mar 2008
One more (in case the user selects multiple cells):

Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Intersect(Target, Me.Range("C:C")) Is Nothing Then
Exit Sub
End If

Application.EnableEvents = False
Me.Range("a1").Select
Application.EnableEvents = True

End Sub



DRICE wrote:
>
> I am trying to programmaticly (vba) prevent users from editing any cell in
> one specific column without haveing to 'protect' the entire worksheet.


--

Dave Peterson
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Disable editing of one specific cell in one specific row in dataGr =?Utf-8?B?R2lkaQ==?= Microsoft C# .NET 1 28th Jan 2006 10:00 PM
Disable CommandBar buttons while editing the cell Vinit Microsoft Excel Programming 1 16th Aug 2005 08:03 AM
Disable "allow in cell editing" via Group Policy =?Utf-8?B?V2lsbGlhbSBSLg==?= Microsoft Outlook Contacts 1 28th Jul 2005 06:01 PM
disable cells editing Sam Microsoft Excel Misc 6 24th Apr 2005 05:43 PM
Disable editing combo box mike Microsoft Access Form Coding 1 7th Nov 2003 07:46 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:02 AM.