Prevent the process of entering by code

YasserKhalil

Well-known Member
Joined
Jun 24, 2010
Messages
852
I have two columns A and B...
First, I want a code in worksheet change which control the process of entering data in columns A and B.
The values have to be entered either in A1 or B1 and so on.
I want to prevent entering values in both cells just a cell of the two
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Try putting this in the sheet's code module
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    With Target
        If .Cells.Count = 1 And .Column < 3 Then
            With .EntireRow.Cells(3 - .Column)
                If CStr(.Value) <> vbNullString Then .Select
            End With
        End If
    End With
End Sub

Or you could change this line
Code:
If CStr(.Value) <> vbNullString Then .Offset(2 - .Column, 0).Select
 
Last edited:
Upvote 0
Why do you want it in the Worksheet_Change event, it works better in SelectionChange.

The code I posted works on the principle of "if you can't select a cell, you can't enter anything into that cell". Any routine based on that priciple would fail in the Change event.

The Change event is the wrong place for code like this. It will only be triggered after the cell has changed value. Code in the Change event might correct improper entry, but it cannot prevent improper data entry.
 
Upvote 0
Why do you want it in the Worksheet_Change event, it works better in SelectionChange.

The code I posted works on the principle of "if you can't select a cell, you can't enter anything into that cell". Any routine based on that priciple would fail in the Change event.

The Change event is the wrong place for code like this. It will only be triggered after the cell has changed value. Code in the Change event might correct improper entry, but it cannot prevent improper data entry.
you are right
thanks again for your great help

I want the code to include the range A2:B20 only as A1 and B1 will be headers..
 
Upvote 0

Forum statistics

Threads
1,224,568
Messages
6,179,595
Members
452,927
Latest member
whitfieldcraig

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