You are here:

Excel/VBA Code to Select Range in a Non Contiguous Data Sheet

Advertisement


Question
QUESTION: Greeting from London,
I have a question for you. I tried to resolve it for half a day but still could not figure it out.
I have a dynamic spreadsheet in excel 2007.
The number of columns is about 15 but it varies every week.  Similarly, the number of rows are about 10,000 and they too change every week.
I want to select the range of cells in the Active Worksheet, say from Cell A3 to the last cell used in the entire sheet .

The following code will select the Rectangular Range of Cells
Range("A3").CurrentRegion.Select
But I am afraid I cannot use this because the data is not Contiguous; there are several instances of blank rows and may be one or more blank columns too.
Therefore I will not know how many rows and columns are in the sheet which varies continually.

If I use the offset command which as follows
Sub GetRange()          
Range ("A3").Select          
Range(ActiveCell.Offset(0, 0), ActiveCell.Offset(1048576, 16384)).Select
End sub          
I cannot use this because I will not know how many rows and columns are in the sheet which varies continually. I may have to write another code to Remove blank rows and  columns contained in the active sheet and that is  cumbersome.

I found this simple one below  on the internet but it is not working.

lastCol = ActiveSheet.Range("A3").End(xlToRight).Column
lastRow = ActiveSheet.Cells(1048576, lastCol).End(xlUp).Row
ActiveSheet.Range("a1", ActiveSheet.Cells(lastRow, lastCol)).Select

Can you please provide me with a simple code to sort this problem.
Thanks in advance
Joe

ANSWER: Joe,

here is code that will do what you describe:

Sub abc()
 Dim RealLastRow As Long
 Dim RealLastColumn As Long
 With Activesheet
  .Range("A1").Select
 On Error Resume Next
 RealLastRow = _
   .Cells.Find("*", [A1], , , xlByRows, xlPrevious).Row
 RealLastColumn = _
   .Cells.Find("*", [A1], , , xlByColumns, xlPrevious).Column
 .Range("A3", .Cells(RealLastRow, RealLastColumn)).Select
End Sub

That should select the rectangular area you describe.  

--
Regards,
Tom Ogilvy


---------- FOLLOW-UP ----------

QUESTION: I need to add End With to complete your code.
Can I add it to line above End Sub or some where else?

Thanks
Joe

Answer
Joe,

I added the Activesheet after I tested the code since you had used it as a qualifier in your code.

Here is the revision.


Sub abc()
Dim RealLastRow As Long
Dim RealLastColumn As Long
With ActiveSheet
 .Range("A1").Select
On Error Resume Next
RealLastRow = _
  .Cells.Find("*", [A1], , , xlByRows, xlPrevious).Row
RealLastColumn = _
  .Cells.Find("*", [A1], , , xlByColumns, xlPrevious).Column
.Range("A3", .Cells(RealLastRow, RealLastColumn)).Select
End With
End Sub

Sorry for the omission.


--
Regards,
Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.