Windmill
Windmill

Reputation: 163

Excel vba determine If the Cell is selected in a table Then run script

I currently run code that loops though a column of data in a table and want to modify my code below that if the cell is selected run my script/macro

Dim tb As Listobject
Dim Currcell, i As Integer

Set tb = Worksheets("Sheet1").ListObjects("Table1")

For i = 1 To 60
    Currcell = tb.DataBodyRange.Cells(i, tb.ListColumns(1).Index)
    If Currcell = Selected Then
    ' Run My Script
End If
Next i

I know the code is crude but hopefully you get the idea "Currcell" suppose to represent if the cell is selected or not. Thanks inadvance

Upvotes: 1

Views: 4559

Answers (1)

Rory
Rory

Reputation: 34045

I'd suggest you change the logic to only loop through any selected cells in that column:

If Not Intersect(tb.DataBodyRange, Selection) Is Nothing Then
    For Each cell In Intersect(tb.ListColumns(1).DataBodyRange, Selection).Cells
        ' do something to cell
    Next cell
End If

Upvotes: 1

Related Questions