Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select all cells with data

Tags:

excel

vba

I have some data in an Excel Worksheet. I would like to select all the cells which contain data.

For example, for a worksheet with data in cells A1, A2, A3, B1, B2, B3, C1, C2, and C3, how can I select just this 3x3 grid, and not the entire sheet?

I am looking for something like ActiveSheet.SelectUsedCells.

like image 896
Marius Avatar asked Apr 30 '09 08:04

Marius


2 Answers

Here you go:

Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select

Or if you don't necessarily start at A1:

Range("C6").Select  ' Select a cell that you know you populated'
Selection.End(xlUp).Select
Selection.End(xlToLeft).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
like image 113
RichieHindle Avatar answered Nov 11 '22 17:11

RichieHindle


You might also want to look at the CurrentRegion property. This will select a contiguous range that is bounded by empty cells, so might be a more elegant way of doing this, depending on the format of your worksheet.

For example:

Range("A1").CurrentRegion.Select
like image 29
Lunatik Avatar answered Nov 11 '22 16:11

Lunatik