Home > Net >  Excel VBA - how to select column AND row of active cell?
Excel VBA - how to select column AND row of active cell?

Time:10-22

I have been struggling with VBA code that selects entire row and column of active cell. I tried many codes, but all of them failed. I don't want to make any changes to range, I need to just select it.

How selection after macro should look like: Selected column and row

Last version of code I came up with, still not working. Any ideas how to achieve it or fix it?



Private Sub CommandButton7_Click()

    Dim Column As range
       Set Column = ActiveCell.EntireColumn
    Dim Row As range
       Set Row = ActiveCell.EntireRow
    Dim rng As range
    
    With ActiveSheet
        Set rng = Union(.range(Column), .range(Row))
    End With
    
  rng.Select

    Unload Me
    
End Sub

CodePudding user response:

You were close, but this can done simply with:

With ActiveCell
    Union(.EntireRow, .EntireColumn).Select
End With

Or just a one-liner:

Union(ActiveCell.EntireRow, ActiveCell.EntireColumn).Select
  • Related