How do you select the entire excel sheet with Range using VBA?

前端 未结 11 593
萌比男神i
萌比男神i 2021-02-01 16:37

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?

相关标签:
11条回答
  • 2021-02-01 17:00

    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.

    0 讨论(0)
  • 2021-02-01 17:01

    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
    
    0 讨论(0)
  • 2021-02-01 17:06

    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 in you can call ActiveSheet.UsedRange and select it from there. Hope that helps

    0 讨论(0)
  • 2021-02-01 17:07

    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

    0 讨论(0)
  • 2021-02-01 17:07

    Maybe this might work:

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

    0 讨论(0)
  • 2021-02-01 17:08

    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

    0 讨论(0)
提交回复
热议问题