How to Dynamically Select Complete Range in Excel Using VBA Macro?

  • Thread starter Thread starter coolcp
  • Start date Start date
  • Tags Tags
    Function Range
Click For Summary
To select a complete range in Excel dynamically, converting the data range into a table is recommended, as it allows for easy reference. If a table is not feasible, defining a named range using the OFFSET function can create a dynamic range based on the number of filled cells in a specific column and row. In VBA, this can be implemented by declaring a Range variable and setting it to the named range. For data sourced from multiple workbooks, the CurrentRegion property can be used to select the range starting from a specific cell, assuming there are no empty cells in the relevant rows or columns. This approach provides flexibility when dealing with varying data sizes across different sheets.
coolcp
Messages
1
Reaction score
0
Hi,

How do we select the complete range in the excel file. For example, I hv files with differnt rows and cols and i wanted to write a code to copy the data frm these datasheets to another..so can't use the lines like Range(A1:AJ50).Select..I require somthing generic to change the range depending upon the data in the sheet..please let me know if you know of any such command.

Also, need a smilar thing for pivot table..need to change sourcedata range R1C1 in the following command dynamically to cover the complete excel sheet..ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _"Sheet1!R1C1:R12C15"

Thanks
Candra
 
Technology news on Phys.org
The best way is to convert your range into a table and refer to that.

Failing that, defining a named range as
Code:
=OFFSET(SheetName!$A$1,0,0,COUNTA(SheetName!$A:$A),COUNTA(SheetName!$1:$1))
wlll get you a rectangular range assuming that none of the relevant cells in column A or row 1 are blank, and in VBA you can get a reference using whichever of the following applies:
Code:
' Naturally you are using Option Explicit, so variables must be declared before use.
Dim myRange as Range

' For workbook-scope names
Set myRange = ThisWorkbook.Names("NameOfRange").RefersToRange

'For worksheet-scope names
Set myRange = ThisWorkbook.Worksheets("SheetName").Names("NameOfRange").RefersToRange

'If you need to refer to a named range in a different workbook, replace "ThisWorkbook" with a 
'suitable Workbook object.

If you are in the unfortunate situation where you have to pull data from multiple workbooks created by other people which don't follow a standard template, then you may be able to use
Code:
Set myRange = Workbooks("WorkbookName").Worksheets(1).Range("A1").CurrentRegion
assuming the data always starts at A1 on the first worksheet and none of the relevant cells in column A or row 1 are empty.
 
  • Like
Likes Greg Bernhardt
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

Replies
1
Views
3K