Adnan
Adnan

Reputation: 165

Run a macro by button when a cell is selected in a range

I want to assign a macro to a button, but the code run in that condition when any cell is selected in a Range of ActiveSheet("A:A"). Otherwise it prompt me a msg box "cell is not selected".

Upvotes: 0

Views: 1067

Answers (2)

Adnan
Adnan

Reputation: 165

This code is also works for me:

Sub PrintPreview()
    If Intersect(ActiveCell, Range("A:A")) Is Nothing Then
        MsgBox "No cell is selected"
    Else
        'write code here
    End If
End Sub

Upvotes: 1

Variatus
Variatus

Reputation: 14373

If ActiveCell.Column = 1 Then
    ' Do your stuff
Else
    MsgBox "No cell is selected"
End If

Upvotes: 2

Related Questions