I found a similar solution to this question in c# How to Select all the cells in a worksheet in Excel.Range object of c#?
What is the process to do this in VBA?
I select data normally by using «ctrl+shift over arrow, down arrow» to select an entire range of cells. When I run this in a macro it codes out A1:Q398247930, for example. I need it to just be
.SetRange Range("A1:whenever I run out of rows and columns")
I could easily do it myself without a macro, but I’m trying to make the entire process a macro, and this is just a piece of it.
Sub sort()
'sort Macro
Range("B2").Select
ActiveWorkbook.Worksheets("Master").sort.SortFields.Clear
ActiveWorkbook.Worksheets("Master").sort.SortFields.Add Key:=Range("B2"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Master").sort
.SetRange Range("A1:whenever I run out of rows and columns")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
edit:
There are other parts where I might want to use the same code but the range is say «C3:End of rows & columns». Is there a way in VBA to get the location of the last cell in the document?
asked Jul 30, 2013 at 18:50
C. TewaltC. Tewalt
2,2612 gold badges29 silver badges47 bronze badges
I believe you want to find the current region of A1 and surrounding cells — not necessarily all cells on the sheet.
If so — simply use…
Range(«A1»).CurrentRegion
answered Jul 30, 2013 at 19:17
ExcelExpertExcelExpert
3522 silver badges4 bronze badges
1
You can simply use cells.select
to select all cells in the worksheet. You can get a valid address by saying Range(Cells.Address)
.
If you want to find the last Used Range
where you have made some formatting change or entered a value into you can call ActiveSheet.UsedRange
and select it from there. Hope that helps.
June7
19.5k8 gold badges24 silver badges33 bronze badges
answered Jul 30, 2013 at 19:11
chanceachancea
5,8083 gold badges28 silver badges39 bronze badges
2
you can use all cells as a object like this :
Dim x as Range
Set x = Worksheets("Sheet name").Cells
X is now a range object that contains the entire worksheet
answered Apr 15, 2015 at 11:47
you have a few options here:
- Using the UsedRange property
- find the last row and column used
- use a mimic of shift down and shift right
I personally use the Used Range and find last row and column method most of the time.
Here’s how you would do it using the UsedRange property:
Sheets("Sheet_Name").UsedRange.Select
This statement will select all used ranges in the worksheet, note that sometimes this doesn’t work very well when you delete columns and rows.
The alternative is to find the very last cell used in the worksheet
Dim rngTemp As Range
Set rngTemp = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
If Not rngTemp Is Nothing Then
Range(Cells(1, 1), rngTemp).Select
End If
What this code is doing:
- Find the last cell containing any value
- select cell(1,1) all the way to the last cell
answered Jul 30, 2013 at 20:32
Derek ChengDerek Cheng
5353 silver badges8 bronze badges
3
Another way to select all cells within a range, as long as the data is contiguous, is to use Range("A1", Range("A1").End(xlDown).End(xlToRight)).Select
.
answered Mar 29, 2019 at 19:38
I would recommend recording a macro, like found in this post;
Excel VBA macro to filter records
But if you are looking to find the end of your data and not the end of the workbook necessary, if there are not empty cells between the beginning and end of your data, I often use something like this;
R = 1
Do While Not IsEmpty(Sheets("Sheet1").Cells(R, 1))
R = R + 1
Loop
Range("A5:A" & R).Select 'This will give you a specific selection
You are left with R = to the number of the row after your data ends. This could be used for the column as well, and then you could use something like Cells(C , R).Select, if you made C the column representation.
answered Jul 30, 2013 at 19:25
MakeCentsMakeCents
7321 gold badge5 silver badges15 bronze badges
2
Sub SelectAllCellsInSheet(SheetName As String)
lastCol = Sheets(SheetName).Range("a1").End(xlToRight).Column
Lastrow = Sheets(SheetName).Cells(1, 1).End(xlDown).Row
Sheets(SheetName).Range("A1", Sheets(SheetName).Cells(Lastrow, lastCol)).Select
End Sub
To use with ActiveSheet:
Call SelectAllCellsInSheet(ActiveSheet.Name)
answered Mar 14, 2017 at 20:57
Yehia AmerYehia Amer
5985 silver badges11 bronze badges
Here is what I used, I know it could use some perfecting, but I think it will help others…
''STYLING''
Dim sheet As Range
' Find Number of rows used
Dim Final As Variant
Final = Range("A1").End(xlDown).Row
' Find Last Column
Dim lCol As Long
lCol = Cells(1, Columns.Count).End(xlToLeft).Column
Set sheet = ActiveWorkbook.ActiveSheet.Range("A" & Final & "", Cells(1, lCol ))
With sheet
.Interior.ColorIndex = 1
End With
answered Mar 16, 2019 at 4:29
I have found that the Worksheet «.UsedRange» method is superior in many instances to solve this problem.
I struggled with a truncation issue that is a normal behaviour of the «.CurrentRegion» method. Using [ Worksheets(«Sheet1»).Range(«A1»).CurrentRegion ] does not yield the results I desired when the worksheet consists of one column with blanks in the rows (and the blanks are wanted). In this case, the «.CurrentRegion» will truncate at the first record. I implemented a work around but recently found an even better one; see code below that allows copying the whole set to another sheet or to identify the actual address (or just rows and columns):
Sub mytest_GetAllUsedCells_in_Worksheet()
Dim myRange
Set myRange = Worksheets("Sheet1").UsedRange
'Alternative code: set myRange = activesheet.UsedRange
'use msgbox or debug.print to show the address range and counts
MsgBox myRange.Address
MsgBox myRange.Columns.Count
MsgBox myRange.Rows.Count
'Copy the Range of data to another sheet
'Note: contains all the cells with that are non-empty
myRange.Copy (Worksheets("Sheet2").Range("A1"))
'Note: transfers all cells starting at "A1" location.
' You can transfer to another area of the 2nd sheet
' by using an alternate starting location like "C5".
End Sub
answered May 2, 2019 at 19:38
Maybe this might work:
Sh.Range(«A1», Sh.Range(«A» & Rows.Count).End(xlUp))
answered Oct 31, 2014 at 18:38
Refering to the very first question, I am looking into the same.
The result I get, recording a macro, is, starting by selecting cell A76:
Sub find_last_row()
Range("A76").Select
Range(Selection, Selection.End(xlDown)).Select
End Sub
answered Aug 7, 2015 at 12:40
Home / VBA / How to SELECT ALL the Cells in a Worksheet using a VBA Code
In VBA, there is a property called CELLS that you can use to select all the cells that you have in a worksheet.
Cells.Select
- First, type the CELLS property to refer to all the cells in the worksheet.
- After that, enter a (.) dot.
- At this point, you’ll have a list of methods and properties.
- From that list select “Select” or type “Select”.
Once you select the entire worksheet you can change the font, clear contents from it, or do other things.
Notes
- The CELLS property works just like the way you use the keyboard shortcut Control + A to select all the cells.
- When you run this VBA code, it will select all the cells even if the sheet is protected and some of the cells are locked.
- It will select cells that are hidden as well.
The sheet Must Be Activated
Now you need to understand one thing here when you select all the cells from a sheet that sheet needs to be activated. In short, you can’t select cells from a sheet that is not activated.
Let’s say you want to select all the cells from “Sheet1”. If you use the type below code, you’ll get an error. You need to activate the “Sheet1” first and then use the “Cells” property to select all the cells.
Worksheets("Sheet1").Activate
Cells.Select
Now when you run this it will first activate the “Sheet1” and then select all the cells. This thing gives you a little limitation that you can’t select the entire sheet if that sheet is not activated.
Here’s another thing that you can do: You can add a new sheet and then select all the cells.
Sheets.Add.Name = "mySheet"
Cells.Select
More Tutorials
- Count Rows using VBA in Excel
- Excel VBA Font (Color, Size, Type, and Bold)
- Excel VBA Hide and Unhide a Column or a Row
- Excel VBA Range – Working with Range and Cells in VBA
- Apply Borders on a Cell using VBA in Excel
- Find Last Row, Column, and Cell using VBA in Excel
- Insert a Row using VBA in Excel
- Merge Cells in Excel using a VBA Code
- Select a Range/Cell using VBA in Excel
- ActiveCell in VBA in Excel
- Special Cells Method in VBA in Excel
- UsedRange Property in VBA in Excel
- VBA AutoFit (Rows, Column, or the Entire Worksheet)
- VBA ClearContents (from a Cell, Range, or Entire Worksheet)
- VBA Copy Range to Another Sheet + Workbook
- VBA Enter Value in a Cell (Set, Get and Change)
- VBA Insert Column (Single and Multiple)
- VBA Named Range | (Static + from Selection + Dynamic)
- VBA Range Offset
- VBA Sort Range | (Descending, Multiple Columns, Sort Orientation
- VBA Wrap Text (Cell, Range, and Entire Worksheet)
- VBA Check IF a Cell is Empty + Multiple Cells
⇠ Back to What is VBA in Excel
Helpful Links – Developer Tab – Visual Basic Editor – Run a Macro – Personal Macro Workbook – Excel Macro Recorder – VBA Interview Questions – VBA Codes
Return to VBA Code Examples
In this Article
- Select All Cells In Worksheet with .Cells
- An example of selecting all cells on Sheet1 using the code name Sheet1:
- An example of selecting all cells on Sheet1 using it’s tabname. You can replace “PlaceTabNameHere” with the name of your tab
- VBA Coding Made Easy
Select All Cells In Worksheet with .Cells
To select all the cells on a sheet using VBA you can use the .cells property of the worksheet, without specifying a specific cell.
An example of selecting all cells on Sheet1 using the code name Sheet1:
Sub MySelectAll()
Sheet1.Activate
Sheet1.Cells.Select
End Sub
An example of selecting all cells on Sheet1 using it’s tabname. You can replace “PlaceTabNameHere” with the name of your tab
Sub MySelectAll2()
Sheets("PlaceTabNameHere").Activate
Sheets("PlaceTabNameHere").Cells.Select
End Sub
VBA Coding Made Easy
Stop searching for VBA code online. Learn more about AutoMacro – A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!
Learn More!
Did you find this VBA tutorial useful? Then share it with your friends and colleagues using the share buttons at the side or the bottom.
How to select all objects (pictures and charts) easily in Excel?
How do you select all objects, such as all pictures, and all charts? This article is going to introduce tricky ways to select all objects, to select all pictures, and to select all charts easily in active worksheet in Excel.
Select all objects in active worksheet
Select all pictures in active worksheet
Select all charts in active worksheet
Delete all objects/ pictures/ charts/ shapes in active/selected/all worksheets
Easily insert multiple pictures/images into cells in Excel
Normally pictures are inserted above cells in Excel. But Kutools for Excel’s Import Pictures utility can help Excel users batch insert each picture/image into a single cell as below screenshot shown:
Go to Download Free Trial 60 daysPurchasePayPal / MyCommerce
Recommended Productivity Software
Office Tab: Use tabbed interface in Office as the use of web browser Chrome, Firefox and Internet Explorer.Kutools for Excel: Adds 120 powerful new features to Excel. Increase your productivity in 5 minutes. Save two hours every day!Classic Menu for Office: Brings back your familiar menus to Office 2007, 2010, 2013 and 2016 (includes Office 365).
Select all objects in active worksheet
Amazing! Using Tabs in Excel like Firefox, Chrome, Internet Explore 10!
How to be more efficient and save time when using Excel?
You can apply the Go To command to select all objects easily. You can do it with following steps:
Step 1: Press the F5 key to open the Go To dialog box.
Step 2: Click the Special button at the bottom to open the Go To Special dialog box.
Step 3: In the Go To Special dialog box, check the Objects option.
Step 4: Click OK. Then it selects all kinds of objects in active worksheet, including all pictures, all charts, all shapes, and so on.
Select all pictures in active worksheet
It seems no easy way to select all pictures except manually selecting each one. Actually, VB macro can help you to select all pictures in active worksheet quickly.
Step 1: Hold down the ALT + F11 keys, and it opens the Microsoft Visual Basic for Applications window.
Step 2: Click Insert > Module, and paste the following macro in the Module Window.
VBA: Select all pictures in active worksheet
1
2
3
Public Sub SelectAllPics()
ActiveSheet.Pictures.Select
End Sub
Step 3: Press the F5 key to run this macro. Then it selects all pictures in active worksheet immediately.
Select all charts in active worksheet
VB macro can also help you to select all charts in active worksheet too.
Step 1: Hold down the ALT + F11 keys, and it opens the Microsoft Visual Basic for Applications window.
Step 2: Click Insert > Module, and paste the following macro in the Module Window.
VBA: Select all charts in active worksheet
1
2
3
Public Sub SelectAllCharts()
ActiveSheet.ChartObjects.Select
End Sub
Step 3: Press the F5 key to run this macro. This macro will select all kinds of charts in active worksheet in a blink of eyes.
Quickly delete all objects/ pictures/ charts/ shapes in active/selected/all worksheets
Sometimes, you may need to delete all pictures, charts, or shapes from current worksheet, current workbook or specified worksheets. You can apply Kutools for Excel’s Delete Illustrations & Objects utility to archive it easily.
Kutools for Excel — Combines More Than 120 Advanced Functions and Tools for Microsoft Excel
Go to DownloadFree Trial 60 daysPurchasePayPal / MyCommerce
1. Click Kutools > Delete > Delete Illustrations & Objects.
2. In the opening dialog box, you need to:
(1) In the Delete section, please specify the types of objects you want to delete.
In our case, we want to remove charts and pictures, therefore we check the Charts option and Pictures option.
(2) In the Look in section, specify the deleting scope.
In our case, we want to remove charts and pictures from several specified sheets, therefore we check the Selected Sheets option, and then check the specified worksheet in the right box. See left screenshot:
Free Download Kutools for Excel Now
3. Click the Ok button.
Then all charts and pictures are removed from the specified worksheets.
Related Articles
Delete all Pictures easily
Delete all charts Workbooks
Delete all Auto Shapes quickly
Delete all Text Boxes quickly
Is your problem solved?
Yes, I want to be more efficiency and save time when using Excel No, the problem persists and I need further support
Recommended Productivity Tools
The following tools will greatly save your time and effort, which one do you prefer?Office Tab: Using handy tabs in your Office, as the way of Chrome, Firefox and New Internet Explorer.Kutools for Excel: 120 powerful new functions for Excel, Increase your productivity in 5 minutes. Save two hours every day!Classic Menu for Office: Bring back familiar menus to Office 2007, 2010, 2013 and 365, as if it were Office 2000 and 2003.
Kutools for Excel
Amazing! Increase your productivity in 5 minutes. Don’t need any special skills, save two hours every day!
More than 120 powerful advanced functions which designed for Excel:
- Merge Cell/Rows/Columns without Losing Data.
- Combine and Consolidate Multiple Sheets and Workbooks.
- Compare Ranges, Copy Multiple Ranges, Convert Text to Date, Unit and Currency Conversion.
- Count by Colors, Paging Subtotals, Advanced Sort and Super Filter,
- More Select/Insert/Delete/Text/Format/Link/Comment/Workbooks/Worksheets Tools…
This example teaches you how to select entire rows and columns in Excel VBA. Are you ready?
Place a command button on your worksheet and add the following code lines:
1. The following code line selects the entire sheet.
Cells.Select
Note: because we placed our command button on the first worksheet, this code line selects the entire first sheet. To select cells on another worksheet, you have to activate this sheet first. For example, the following code lines select the entire second worksheet.
Worksheets(2).Activate
Worksheets(2).Cells.Select
2. The following code line selects the second column.
Columns(2).Select
3. The following code line selects the seventh row.
Rows(7).Select
4. To select multiple rows, add a code line like this:
Rows(«5:7»).Select
5. To select multiple columns, add a code line like this:
Columns(«B:E»).Select
6. Be careful not to mix up the Rows and Columns properties with the Row and Column properties. The Rows and Columns properties return a Range object. The Row and Column properties return a single value.
Code line:
MsgBox Cells(5, 2).Row
Result:
7. Select cell D6. The following code line selects the entire row of the active cell.
ActiveCell.EntireRow.Select
Note: border for illustration only.
8. Select cell D6. The following code line enters the value 2 into the first cell of the column that contains the active cell.
ActiveCell.EntireColumn.Cells(1).Value = 2
Note: border for illustration only.
9. Select cell D6. The following code line enters the value 3 into the first cell of the row below the row that contains the active cell.
ActiveCell.EntireRow.Offset(1, 0).Cells(1).Value = 3
Note: border for illustration only.