How to Deselect Cells from Selected Range with VBA

,

This post will guide you how to deselect cells from a selected range of cells in your worksheet. How do I quickly deselect cells from a selected range of cells with VBA code in Excel.

Normally, if you want to deselect a cell or multiple cells from a selection, you just need to hold down the CTRL key and click on the cells you want to deselect. If you want to unselect a range of selected cells, you need to hold down the CTRL key and drag the range you want to deselect. This post will explain that how to deselect cells from a selection with VBA Macro quickly in Excel.

1. Deselect Cells from Selected Range with VBA

You can use an Excel VBA Macro to quickly achieve the same result of deselecting one or multiple cells from a selected range of cells. Just do the following steps:

Step1: open your excel workbook and then click on “Visual Basic” command under DEVELOPER Tab, or just press “ALT+F11” shortcut.

Get the position of the nth using excel vba1

Step2: then the “Visual Basic Editor” window will appear.

Step3: click “Insert” ->”Module” to create a new module.

convert column number to letter3

Step4: paste the below VBA code into the code window. Then clicking “Save” button.

deselect cells from selected range1
Sub DeSelectCells()
    Set myRange = Application.Selection
    Set myRange = Application.InputBox("Select one Range:", "DeSelectCells", myRange.Address, Type:=8)
    Set DeleteRng = Application.InputBox("select one cell or range of cells that you want to deselect", "DeSelectCells", Type:=8)
    Dim LastRange As Range
    
    For Each myCell In myRange
        If Application.Intersect(myCell, DeleteRng) Is Nothing Then
            If LastRange Is Nothing Then
                Set LastRange = myCell
            Else
                Set LastRange = Application.Union(LastRange, myCell)
            End If
        End If
    Next
    LastRange.Select
End Sub

Step5: back to the current worksheet, then run the above excel macro. Click Run button.

deselect cells from selected range2

Step6: Select one Range that you want to deselect cells, such s:A1:D7

deselect cells from selected range3

Step7: select one cell or range of cells that you want to deselect.

deselect cells from selected range4

Step8: Let’s see the last result:

deselect cells from selected range5

2. Video: Deselect Cells from Selected Range with VBA

This video will demonstrate you how to deselect cells from a selected range of cells with a VBA Macro in your worksheet.

Leave a Reply