Select all excel миф

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. Tewalt's user avatar

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

ExcelExpert's user avatar

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's user avatar

June7

19.5k8 gold badges24 silver badges33 bronze badges

answered Jul 30, 2013 at 19:11

chancea's user avatar

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

user4791681's user avatar

you have a few options here:

  1. Using the UsedRange property
  2. find the last row and column used
  3. 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:

  1. Find the last cell containing any value
  2. select cell(1,1) all the way to the last cell

Faisal Mehmood's user avatar

answered Jul 30, 2013 at 20:32

Derek Cheng's user avatar

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

Bob the Builder's user avatar

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.

Community's user avatar

answered Jul 30, 2013 at 19:25

MakeCents's user avatar

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 Amer's user avatar

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

FreeSoftwareServers's user avatar

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

Lifygen's user avatar

Maybe this might work:

Sh.Range(«A1», Sh.Range(«A» & Rows.Count).End(xlUp))

answered Oct 31, 2014 at 18:38

Sarah's user avatar

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

Pavlin Todorov's user avatar

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!

alt text

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.

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
  1. First, type the CELLS property to refer to all the cells in the worksheet.
    1-type-the-cells-property
  2. After that, enter a (.) dot.
  3. At this point, you’ll have a list of methods and properties.
    2-enter-a-dot
  4. From that list select “Select” or type “Select”.
    3-from-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

    • #2

    Welcome to the Board!<pre>
    Sheets.Select</pre>

    that should do it.

    EDIT:: Should’ve tested that with some hidden sheets really shouldn’t I? Nate Oliver’s solution is the way to go.
    _________________<font color = green> Mark O’Brien </font>

    Columbus Ohio Celtic Supporters Club
    This message was edited by Mark O’Brien on 2002-12-19 10:48

    • #3

    ActiveWorkbook.Sheets.Select

    • #4

    Howdy Oliver (sharp name!), welcome to the board. How’s about the following:

    <pre>
    Sub gram_em()
    Dim ws As Worksheet
    For Each ws In Sheets
    If ws.Visible Then ws.Select (False)
    Next
    End Sub</pre>

    • #5

    ta Nate it was that ****ing false thing i’d missed

    :)

    • #6

    Hi Nate,

    What the use for «(false)» in that syntax?
    If i using that syntax, error message appear

    «run time error ‘1004’
    application-defined or object-defined error»

    But when i delete the (false), all the syntax work like charm

    • #7

    It was 2002 when Nate wrote that. I don’t think it likely that he is still subscribed to this thread.

    • #8

    Hi Nate,

    What the use for «(false)» in that syntax?
    If i using that syntax, error message appear

    «run time error ‘1004’
    application-defined or object-defined error»

    But when i delete the (false), all the syntax work like charm

    I believe the (false) flag tells Excel not to replace whatever’s currently selected with the object you’re selecting (I think it only works with worksheets; not sure it works with ranges, etc.). (true) would tell it to select the sheet as the only sheet selected; (false) tells it to keep selecting.

    • #9

    I know it is an old thread, but it was usefully to me….
    Remove the brackets () around FALSE
    :eek:

    For the equivalent of Ctrl + A go down to the CurrentRegion section

    UsedRange — All cells (with values)

    The simplest change you can do to your code to include everything is changing Range("$A$1:$C$3") to ActiveSheet.UsedRange.
    Note that this will make a table to include everything in the sheet, so if you have other data in there, you need a different approach. If you want everything included, then great, you can trim some of the code away if you like:

    Sub Macro1()
        ActiveSheet.ListObjects.Add(xlSrcRange, ActiveSheet.UsedRange, , xlYes).Name = _
            "Table1"
        ActiveSheet.ListObjects("Table1").TableStyle = "TableStyleMedium9"
    End Sub
    

    CurrentRegion — All connected cells (with values)

    If you want a more constrained way of doing it, you can use CurrentRegion instead.
    If you know that the table will always be including for example A1 then you could use Range("A1").CurrentRegion instead:

    ActiveSheet.ListObjects.Add(xlSrcRange, Range("A1").CurrentRegion, , xlYes).Name = _
        "Table1"
    ActiveSheet.ListObjects("Table1").TableStyle = "TableStyleMedium9"
    

    If location is not a given, you could even select a cell before running it, and use Selection.CurrentRegion instead.

    enter image description here

    Example run with the following code:

    Sub Macro1()
        Set objListObject = ActiveSheet.ListObjects.Add(xlSrcRange, Selection.CurrentRegion, TableStyle = "TableStyleMedium9")
    End Sub
    

    Понравилась статья? Поделить с друзьями:
  • Select all empty cells excel
  • Select all charts excel
  • Select all changes word
  • Select all cells shortcut excel
  • Select a word game