How To Select All Cells In Excel Vba

To select all cells in Excel VBA, you can use the Range object. Here is an example:

    
      Sub SelectAllCells()
        Dim ws As Worksheet
        Set ws = ThisWorkbook.Worksheets("Sheet1")
        
        Dim rng As Range
        Set rng = ws.UsedRange
        
        rng.Select
      End Sub
    
  

Explanation of the code:

  • First, we declare and set a variable to reference the worksheet we want to work with. In this example, we are using “Sheet1”. You can change it according to your needs.
  • Next, we declare and set another variable to reference the range of cells we want to select. In this example, we are using the UsedRange property, which returns the used range of the worksheet.
  • Finally, we use the Select method of the Range object to select the range of cells.

Make sure to include this code within a VBA module in Excel. Then, you can run the macro “SelectAllCells” to select all cells in the specified worksheet.

Same cateogry post

Leave a comment