make only a range selectable

cyote101

Active Member
Joined
Dec 11, 2004
Messages
315
How do i make it so only a range a2:b3 is able to be selected, i mean by using a code for the page, without having to "Protect the sheet"
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
You can protect (and unprotect) the sheet as needed with code...would that solve your problem? You could use any combination of criteria in an IF statement to decide when to protect/unprotect, with or without a password.
 
Upvote 0
First run the following code. This will collor all your workbook grey except range("a2:b3"). You can do it manually too
Code:
Sheet1.Cells.Interior.ColorIndex = 15 ' where sheet1 place your sheet
Range("A2:B3").Interior.ColorIndex = xlNone

After at worksheet event place code:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Interior.ColorIndex = 15 Then Range("a2").Activate
End Sub
 
Upvote 0
This is the code i'm currently using. but it does not work if the page is protected were you can select any cell

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Count > 1 Then Exit Sub
If Target.Row < 10 Then Exit Sub
If Target.Row > 28 Then Exit Sub
If Target.Row Mod 2 <> 0 Then Exit Sub
Application.ScreenUpdating = False
ActiveSheet.Unprotect
Cells(Target.Row, 4).Copy



Range("$D$4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
Application.ScreenUpdating = True

End Sub

How do i make it so this code works even when the whole page is protected and your not able to select any cells
 
Upvote 0
Why don't you try the selectionchange instead of before double click:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Count > 1 Then
    ElseIf Target.Row < 10 Then
    ElseIf Target.Row > 28 Then
    ElseIf Target.Row Mod 2 <> 0 Then
    Exit Sub
Else
ActiveSheet.Unprotect
Application.ScreenUpdating = False
Cells(Target.Row, 4).Copy
Range("d4").PasteSpecial Paste:=xlPasteValues
Application.ScreenUpdating = True
ActiveSheet.Protect
End If
End Sub
 
Upvote 0
To prevent selection of any cells(s) in A2:B3 :-

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim cell As Range
Application.EnableEvents = False
For Each cell In Target
    If Intersect(cell, [A2:B3]) Is Nothing Then
        [A2].Select
        Exit For
    End If
Next
Application.EnableEvents = True
End Sub

In addition, to prevent dragging data from any cells(s) in A2:B3 to cells outside of A2:A3 :-

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
Application.EnableEvents = False
For Each cell In Target
    If Intersect(cell, [A2:B3]) Is Nothing Then
        Application.Undo
        [A2].Select
        Exit For
    End If
Next
Application.EnableEvents = True
End Sub
 
Upvote 0
How about this? Right click on the sheet tab and select View code. Once you are in VBA screen Press F4. In the Properties Window make a change next to Scroll Area. Change it to a2:b3. Save your workbook so that the change to that sheet will still be in effect the next time you open the workbook. Hey no code and you didn't protect the sheet either. Someone can select a range by putting something like A2:E14 in the address bar and pressing enter. This also effects a named range that can be selected from the address bar. If a range is selected in this manner then it is possible to edit the cells if you tab through the cells. However most people wouldn't think of this. You could actually use it to your advantage. You would know how to edit the named ranges, but others wouldn't know most likely. If you need to skip around the page then hold down the Ctrl key while selecting each cell in the order you would like to tab through them. Now with the cells still selected type a name for them in the address bar and press enter. You could create several named ranges for different sections of your sheet. When you want to edit that section just choose its name from the address bar pull down and tab through or press enter. Remember the cells can be noncontiguous and in any order imaginable.
 
Upvote 0
You can't drag data outside of that range. I forgot to mention you can tab in reverse by pressing Shift and tab at the same time, or Shift and Enter.
 
Upvote 0

Forum statistics

Threads
1,215,477
Messages
6,125,031
Members
449,205
Latest member
Eggy66

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