Range object containing all cells of a worksheet - vba

I am looking for a command that creates a Range Object containing all cells of a given worksheet.
Sheets(1).Range("A1:AAZ1000000") would be a safe bet, but you never know how many rows or columns you might encounter so something like
Sheets(1).Range("All") would be better.

Sheets(1).Cells returns a Range object and when called with no parameters returns a range encompassing all the cells in the sheet.

Sheets(1).EntireRow.EntireColumn is one way

Related

Excel formula into all cell array

Im trying to fill in a formula in a lot of cells using a VBA. The workbook I'm working with looks like this:
I'm trying to fill in a formula starting in B3. I first tried to define a range and insert formula, but my problem is that the range is never the same. Some data sets I have more columns and others i have more rows.
Is there a way to make a VBA that defines the range as all columns with content in Row1 and all rows with content in A?
The formula that I'm trying to inset is like this: =INDEX(Sheet1!$N:$N;MATCH(Sheet3!$A:$A&Sheet3!B$1;Sheet1!$R:$R;0))
I hope someone can help me with my problem.
you could create a dynamic named range that can be used in VBA
Use the below to define the range
=OFFSET(Sheet1!$B$3,0,0,COUNTA(Sheet1!$A:$A)-1,COUNTA(Sheet1!$1:$1)-1)
Changing Sheet1 to that of your sheetname
Assuming that there areno blank columns or rowsin your data set etc.

How to count Blanks in N'th column in a ListObject table

I want to count Empty cells (Blanks) in the fourth column only. I tried the following code.
ActiveSheet.ListObjects(1).ListColumns(4).DataBodyRange.SpecialCells(xlCellTypeBlanks).Count
In my case, I have a table with only one row and 4'th column has non-empty cell. I get the wrong value with my code. The code above returns 3 Blanks instead of zero - 3 blanks could be total number of blanks in table.
I do not know the specifics behind this bug but it seems closely related to a behavior that used to show up when you tried to reference the visible cells below the header in an AutoFilter range when the filtered range showed no visible cells. In that case, it returned all non-visible rows. While not completely the same, this miscount seems closely related. To the best of my knowledge, that bug has been corrected in Office service packs.
Use the Excel Application object to call a worksheet's COUNTBLANK function to achieve the correct result.
With Worksheets("Sheet1")
Debug.Print Application.CountBlank(.ListObjects(1).ListColumns(4).DataBodyRange)
End With

Accessing Certain Column in a Range

I am trying to access column 2 in a specific range, and then counting the cells with constants in them. Right now what I have is accessing the range, but not a specific column in the range, but rather the entire range... thus counting too many constants.
X = Range("MAIN_LIST").Cells.SpecialCells(xlCellTypeConstants).Count
Is there a way for me to access a specific column in this range? I tried to do this somehow with:
X = Range("MAIN_LIST").Column(2).Cells.SpecialCells(xlCellTypeConstants).Count
but am getting a syntax error. Anyone know the specific syntax to do this?
Thanks.
Try just:
Range("MAIN_LIST").Columns(2).SpecialCells(xlCellTypeConstants).Count
The method is Columns, not Column.
http://msdn.microsoft.com/en-us/library/office/ff837125(v=office.14).aspx
Also, while it's not an error: Using the method Cells is redundant/unnecessary, the Range object itself already is comprised of its Cells, and likewise any particular column in that range is also comprised of its Cells :)

What range is calculated in Excel when using VBA .Calculate?

If I use:
Sheets("Sheet1").Calculate
Is every cell on Sheet1 calculated, or only cells in the UsedRange?
I'm writing a subroutine that recalculates a variable number of rows and I'm ultimately trying to determine if my calculation will be faster using:
Sheets("Sheet1").Range("VariableNumberOfRows").Calculate
Or
Sheets("Sheet1").UsedRange.Calculate
There are other rows outside the variable number of rows (and obviously inside the UsedRange), but these do not contain formulas therefore doesn't matter if they're included in the calculation.
If there's a technical article on what .Calculate specifically calculates (which cells, types of formulas) that would helpful as well.
Thanks!
Clearly the answer is no, it does not calculate cells outside the used range. There would be nothing there, so there is nothing to calculate. Excel tracks the cells used (it has to in order to report the .UsedRange).
Use .UsedRange if you want all cells on the sheet that contain formulas to be calculated, or specify a smaller range if you only want that range calculated.

Extending the number of reference cells passed to .Range function in Excel VBA

I am using worksheet.range("Relevantcells") to copy cells in a sheet. I have non-contiguous cells and hence it only lets me put in 60 specific cells.
How can I extend this to many more cells that I want to read from?
Instead of having one large non-contiguous range, you could create an array of smaller ranges in code and then copy each range iteratively.
You'll likely take a performance hit over copying the ranges en masse, but this approach should be more flexible.