How to Launch VBA Editor in Excel
To access the Microsoft Visual Basic window, go to the Developer tab and click on Visual Basic.
You can also open it by pressing Alt+F11 on your keyboard.
Go to the Insert tab and click on Module to open the code Module.
We will use the following dataset showing Products and Revenue Earned by various Sales Reps.
Method 1 – Using CutCopy Mode to Deselect
Steps:
- Go to the Microsoft Visual Basic code Module and enter the following code:
Sub SelectCutCopyMode()
Dim SelectedCell As Range
Set SelectedCell = Range("E1")
SelectedCell.Select
Application.CutCopyMode = False
Columns(SelectedCell.Column).Hidden = True
End Sub
Code Breakdown
Set SelectedCell = Range("E1")
SelectedCell.Select
Application.CutCopyMode = False
Columns(SelectedCell.Column).Hidden = True
The code declares a variable named SelectedCell and sets the cell E1 to it.
Application.CutCopyMode = False line sets the CutCopyMode to False.
The last line hides the column containing cell E1.
- Select a range that you need to deselect.
- Press F5 to run the code.
- Cell E1 will be selected, and column E will be hidden.
Method 2 – Using the .Protect Property
Steps:
- Select any cell or range in your worksheet.
- Open the VBA code Module and enter the following code:
Sub DeselectProtect()
Dim ws As Worksheet
Set ws = ActiveSheet
With ws
.EnableSelection = xlNoSelection
.Protect
End With
End Sub
Code Breakdown
Set ws = ActiveSheet
With ws
.EnableSelection = xlNoSelection
.Protect
End With
The code calls the Protect method to protect the active worksheet. This prevents changes to the worksheet.
- Run the code, and you will see that no cells have been selected. In the following figure, the active cell is B4, which is not selected.
Method 3 – Select Another Cell to Deselect a Specific Range or Column
Steps:
- Enter the following code in the VBA Module:
Sub SelectAnotherCell()
ThisWorkbook.Sheets("Another").Range("A1").Select
End Sub
Code Breakdown
ThisWorkbook.Sheets("Another").Range("A1").Select
This code selects the cell A1 of the worksheet named Another.
- B4 to B12 is our selected range. We want to select a cell outside this range to deselect this range.
- Run the VBA code, and the range B4:B12 is no longer selected. Instead, cell A1 is now selected.
Read More: Excel VBA to Select First Visible Cell in Filtered Range
Method 4 – Removing Active Cells from a Selection
Steps:
- Select the active cell, which is the first cell of the range.
- Remove the active cell from the selected range.
- Enter the following VBA code:
Sub DeselectActiveCell()
Dim rng As Range
Dim rngSelected As Range
For Each rng In Selection.Cells
If StrComp(rng.Address, ActiveCell.Address, _
vbBinaryCompare) <> 0 Then
If rngSelected Is Nothing Then
Set rngSelected = rng
Else
Set rngSelected = Application.Union(rngSelected, rng)
End If
End If
Next rng
If Not rngSelected Is Nothing Then
rngSelected.Select
End If
End Sub
Code Breakdown
For Each rng In Selection.Cells
If StrComp(rng.Address, ActiveCell.Address, _
vbBinaryCompare) <> 0 Then
If rngSelected Is Nothing Then
Set rngSelected = rng
Else
Set rngSelected = Application.Union(rngSelected, rng)
End If
End If
Next rng
- The code starts a loop that iterates over selected cells.
- If StrComp(rng.Address, ActiveCell.Address, vbBinaryCompare) <> 0 line compares the address of the current cell with the address of the active cell.
- Set rngSelected = rng sets the rngSelected variable for the current cell if it is the first cell that meets the criteria.
- Set rngSelected = Application.Union(rngSelected, rng) line uses the Union method of the Application object to combine the current cell with the previously selected cells.
The selected range is B4:D12 and the active cell is B4. We want to remove the active cell using the code.
- Run the VBA code, and you will find that the new active cell is C4.
Method 5 – Using Selection.Locked to Deselect
Steps:
- Enter the VBA code below:
Sub DeselectLocked()
Range("E1").Select
Selection.Locked = True
End Sub
Code Breakdown
Range("E1").Select
Selection.Locked = True
- The code first selects cell E1 in the active worksheet.
- Then, it locks the selected cell by Selection.Locked = True.
The range we want to deselect is B4:B12 in this example.
- Press F5 to run the VBA code to deselect the specified range.
Method 6 – Deselecting Specific Cells from a Selection
Steps:
- Enter the following code in the VBA code Module:
Sub DeselectSpecificCell()
Dim rng As Range
Dim selectedRange As Range
Dim deleteRange As Range
Dim remainingRange As Range
Dim xTitleId As String
xTitleId = "Deselect Cells"
Set selectedRange = Application.Selection
Set selectedRange = Application.InputBox("Select Range :", _
xTitleId, selectedRange.Address, Type:=8)
Set deleteRange = Application.InputBox("Deselect Cells", _
xTitleId, Type:=8)
For Each rng In selectedRange
If Application.Intersect(rng, deleteRange) Is Nothing Then
If remainingRange Is Nothing Then
Set remainingRange = rng
Else
Set remainingRange = Application.Union(remainingRange, rng)
End If
End If
Next
If Not remainingRange Is Nothing Then
remainingRange.Select
End If
End Sub
Code Breakdown
For Each rng In selectedRange
If Application.Intersect(rng, deleteRange) Is Nothing Then
If remainingRange Is Nothing Then
Set remainingRange = rng
Else
Set remainingRange = Application.Union(remainingRange, rng)
End If
End If
Next
- This code initiates a loop that iterates through the selected range.
- If Application.Intersect(rng, deleteRange) Is Nothing Then the line checks if the current cell intersects with other cells of the range named deleteRange.
- If it does, the cells are deselected. The cells that are not intersected are stored in the remaining range.
- Set remainingRange = Application.Union(remainingRange, rng) uses the Application.Union method to combine the existing remainingRange with the current cell after each loop.
- Run the code by pressing the F5.
- An Input Box will appear where you need to insert the whole range. We have selected the range B4:B12.
- Click OK.
- Another Input Box will pop up, where you must input the cells you want to deselect. Then press OK.
- The specified cells will be deselected.
Method 7 – Selecting a Cell Outside the Visible Range
Steps:
- Select a cell outside this range.
- Enter the code below:
Sub DeselectOutsideRange()
Dim rng As Range
Application.ScreenUpdating = False
Set rng = Application.ActiveWindow.VisibleRange
rng(rng.Cells.Count + 1).Select
Application.ScreenUpdating = True
End Sub
Code Breakdown
Application.ScreenUpdating = False
Set rng = Application.ActiveWindow.VisibleRange
rng(rng.Cells.Count + 1).Select
Application.ScreenUpdating = True
- The code sets the ScreenUpdating to False to disable screen updating.
- Set rng = Application.ActiveWindow.VisibleRange sets the range to the visible range of the worksheet.
- rng(rng.Cells.Count + 1).Select selects the next cell after the last visible cell in column A.
- The last line allows the screen to update normally after executing the code.
- Run the code. The newly selected cell is A18, which is the next cell after the last visible cell.
Read More: How to Select Visible Cells in Excel with VBA
How to Deselect a Shape in Excel VBA
In this example, we will use VBA to deselect a shape in an Excel worksheet. The VBA code is given below.
Sub DeselectShape()
Dim ws As Worksheet
Dim rng As Range
Dim cell As Range
Set ws = ThisWorkbook.Sheets("Shape") '
Set rng = ws.Range("D5:D12")
For Each cell In rng
If cell.Value > 5000 Then
cell.Interior.Color = RGB(0, 255, 0)
End If
Next cell
On Error Resume Next
ws.Shapes(Application.Caller).Select (False)
On Error GoTo 0
ActiveSheet.Range("A1").Select
End Sub
Code Breakdown
ws.Shapes(Application.Caller).Select (False)
On Error GoTo 0
ActiveSheet.Range("A1").Select
- The code first selects a shape on a worksheet.
- Application.Caller property returns the name or reference of the object that is called the currently executing macro.
- ActiveSheet.Range(“A1”).Select selects cell A1 in the active worksheet.
The rectangular shape is selected in the following figure. We want to deselect it using the above code.
Run the code and go back to the Excel sheet. The shape is deselected, and cell A1 is selected.
Things to Remember
- The first method is convenient as it selects a cell outside the specified range and then hides it.
- The second method is the best way to deselect cells in Excel.
- Remember to carefully consider the specific requirements of your task and choose the most appropriate method for deselecting cells or ranges in Excel VBA.
Download the Practice Workbook
Download this workbook to practice.