Link to home
Start Free TrialLog in
Avatar of jarrah10
jarrah10Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Extend Selected Cells in Excel VBA

Hi All,

Hopefully this is an easy answer, although searching Google and EE didn't help (maybe I was using the wrong search terms).

Anyway, using VBA I've ended up with a selection of cells from a Pivot Table. I would like to know how to extend the selection to include one more cell to the right please.

e.g. say I have A1to A10 selected, how would I programmatically extend the selection by one cell to the right so I end up with A1 to A11 selected?

I wish to add this extension selection to the PivotFields line below please.

    Dim pt As PivotTable
    Set pt = ActiveSheet.PivotTables(1)
    pt.PivotFields("Item").DataRange.Select

Open in new window

Many thanks
Avatar of Saqib Husain
Saqib Husain
Flag of Pakistan image

selection.resize(selection.rows, selection.columns+1).select
Or simply

selection.resize(, selection.columns+1).select

You have to add this line after the select statement given in the question.

Alternatively you can replace your select statement with this one
pt.PivotFields("Item").DataRange.resize(,pt.PivotFields("Item").DataRange.columns+1).Select
ASKER CERTIFIED SOLUTION
Avatar of Saqib Husain
Saqib Husain
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Norie
Norie

Or this:
Range(Selection, Selection.Offset(,1)).Select

Open in new window

Avatar of jarrah10

ASKER

Perfect! Thanks very much. :)