prevent selection highlighted cells

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,429
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
hi experts

I no know if what I'm asking it , it's possible I would prevent select any cell is highlighted if add message box " you can't select highlighted cells" it will a great

note : if I select the cells are highlighted and locate within them empty cell or not color cells it should prevent after show message should select the cells are filled without contain color
a.xlsx
ABCDEFGHIJKLMN
4
5FFFF
6AABBFF
7AA
8DD
9DDAAAA
10
11DDDD
12CCDD
13
14FFFF
15FFF
16
DD
 
if that is the case try this code:
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
tr = Target.Rows.Count
If Target.Count > 1 Then
 For Each cell In Target
   If cell.Interior.Pattern > 0 Then
   MsgBox ("you can't select highlighted cells")
   Target.Offset(tr + 1, 0).Select
   Exit For
   End If
 Next cell
Else
    If Target.Interior.Pattern > 0 Then
     Target.Offset(1, 0).Select
     MsgBox ("you can't select highlighted cells")
    End If
End If
End Sub
Note that if they select All cells, that will result in an overflow error.

If the answers to my previous questions are "Yes", this code will avoid that issue.
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Dim ct As Double
    Dim cell As Range
    
'   Count cells selected
    On Error GoTo err_chk
    ct = Selection.Count
    On Error GoTo 0
    
    For Each cell In Target
        If cell.Interior.Pattern > 0 Then
            cell.Offset(1, 0).Select
            MsgBox ("you can't select highlighted cells")
            Exit For
        End If
    Next cell
    
    Exit Sub
    
err_chk:
    If Err.Number = 6 Then
        Range("A1").Select
        MsgBox "You cannot select all cells"
    Else
        MsgBox Err.Number & ": " & Err.Description
    End If
    
End Sub
 
Upvote 0

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
@offthelip it doesn't work with condition formatting
That is why I asked the question.
Manual formatting and conditional formatting work differently, and are identified differently in VBA.
 
Upvote 0
so it's impossible do that together
I never said that. I said it is "different".

We should be able to use the same conditions you are using in your conditional formatting in the VBA code.
What is your Conditional Formatting rule?
Are there more than one?
Are they always the same?
 
Upvote 0
To work with CF you can change Joe's code like
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Dim ct As Double
    Dim cell As Range
    
'   Count cells selected
    On Error GoTo err_chk
    ct = Selection.Count
    On Error GoTo 0
    
    For Each cell In Target
        If cell.DisplayFormat.Interior.Pattern > 0 Then
            cell.Offset(1, 0).Select
            MsgBox ("you can't select highlighted cells")
            Exit For
        End If
    Next cell
    
    Exit Sub
    
err_chk:
    If Err.Number = 6 Then
        Range("A1").Select
        MsgBox "You cannot select all cells"
    Else
        MsgBox Err.Number & ": " & Err.Description
    End If
    
End Sub
 
Upvote 0
I never said that. I said it is "different".

We should be able to use the same conditions you are using in your conditional formatting in the VBA code.
What is your Conditional Formatting rule?
Are there more than one?
the rule is =ISNUMBER(FIND("TOTAL",$O1))
now there is no more than one
Are they always the same?
I don't understand it
 
Upvote 0
@Fluff that's awesome , can you mod after show the message then select the filled cells without highlighted as post#9
 
Upvote 0
How about
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
   Dim cell As Range, Rng As Range
   Dim Flg As Boolean
  
   For Each cell In Target
      If cell.DisplayFormat.Interior.Pattern > 0 Then Flg = True
      If cell.DisplayFormat.Interior.Pattern = -4142 And cell.Value <> "" Then
         If Rng Is Nothing Then Set Rng = cell Else Set Rng = Union(Rng, cell)
      End If
   Next cell
   If Not Rng Is Nothing Then
      Rng.Select
   ElseIf Flg Then
      Range("A1").Select
   End If
   If Flg Then MsgBox "you can't select highlighted cells"
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,984
Messages
6,122,601
Members
449,089
Latest member
Motoracer88

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