VBA: range selection - vba

I would like to ask, if I need to select an entire row, start from the 3rd row as a range in VBA, instead of the below line, any better ways to go about it? Many thanks.
Range("C3:C1048576").Select

Assuming your working with an Excel Worksheet, this line will do it
ActiveWorkbook.Worksheets(1).Rows(3).Select

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 select only cells with values on different sheets, without switching over to that sheet?

I currently have a macro that copies and paste data from one sheet to another sheet, based on a certain criteria. I have over 15 sheets that it does this.
Now what I want to do is select all the data in the new sheets, and turn it into a table.
I know I can use ActiveSheet, but I would need to do that for every single sheet. Is there a way I can select only the data in other sheets, without that being my active sheet?
I am able to select all my data in an active sheet with this:
ActiveSheet.Range("B1:M1", ActiveSheet.Range("B1:M1").End(xlDown)).Select
I tried doing something like below, but just selects the last cell with data in it. If I remove the .End(xlDown), then it will select B1:M1.
Application.Goto Sheets("SheetName").Range("B1:M1").End(xlDown)
Any idea if there is a way to copy my data in those ranges without switching tabs?
Thank you!
You can avoid Select/Selection/Active/ActiveXXX, like follows:
With Sheets("SheetName")
.Range("B1:M1", .Range("B1:M1").End(xlDown)).Copy Destination:= someotherrange
End With
Where someotherrange is a valid range reference where to paste copied data into

Range object containing all cells of a worksheet

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

Grouping and ungrouping a specified range with excel VBA

i have a range of cells (A60 TO Y70) . I would like to group and ungroup the columns of these cells as they are way too broad and thus complexed to view. Is it possible to ungroup and group a specified range with the use of VBA. There are many more ranges of such thus this function would be really useful for me! Thank you:)
You can simply use the group function:
Range("a60", "y70").Group
or ungroup them:
Range("a60", "y70").Ungroup

How do I select specific cells in VBA?

Basically I need to select specific cells on an excel sheet in vba. For example the code should be able to select a5, a10, a15. I do not want the cells in between them, just the ones I listed. Is there a specific function that will do this? It seems like .Range could only take start cell and ending cell.
You'd use this: Range("A5,A10,A15").Select
To add additional cells, just use more commas.
Alternately, you can utilize the Union method to join multiple range objects into a single Range object.
Note that it is generally not a good idea to select cells in VBA as nearly everything can be done without selection. This is a frequent mistake made by people new to VBA due to generated macros (Record Macro) recreating what you do rather than the result you want.