简体   繁体   English

如何使用 VBA 选择带有 Range 的整个 Excel 表?

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

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#?我在 c# 如何在 Excel.Range 的 c# 对象中选择工作表中的所有单元格中找到了这个问题的类似解决方案

What is the process to do this in VBA?在 VBA 中执行此操作的过程是什么?

I select data normally by using "ctrl+shift over arrow, down arrow" to select an entire range of cells.我通常通过使用“ctrl+shift over arrow, down arrow”来选择整个单元格范围来选择数据。 When I run this in a macro it codes out A1:Q398247930, for example.例如,当我在宏中运行它时,它会编码出 A1:Q398247930。 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".还有其他部分我可能想使用相同的代码,但范围是“C3:行和列的结尾”。 Is there a way in VBA to get the location of the last cell in the document? VBA 中有没有办法获取文档中最后一个单元格的位置?

I believe you want to find the current region of A1 and surrounding cells - not necessarily all cells on the sheet.我相信您想找到 A1 和周围单元格的当前区域 - 不一定是工作表上的所有单元格。 If so - simply use... Range("A1").CurrentRegion如果是这样 - 只需使用... Range("A1").CurrentRegion

You can simply use cells.select to select all cells in the worksheet.您可以简单地使用cells.select来选择工作表中的所有单元格。 You can get a valid address by saying Range(Cells.Address) .您可以通过说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.如果您想找到最后一个Used Range ,您可以在其中进行一些格式更改或输入值,您可以调用ActiveSheet.UsedRange并从那里选择它。 Hope that helps希望有帮助

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 X 现在是包含整个工作表的范围对象

you have a few options here:你有几个选择:

  1. Using the UsedRange property使用 UsedRange 属性
  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:以下是使用 UsedRange 属性的方法:

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选择单元格(1,1)一直到最后一个单元格

只要数据是连续的,另一种选择范围内所有单元格的方法是使用Range("A1", Range("A1").End(xlDown).End(xlToRight)).Select

I would recommend recording a macro, like found in this post;我建议录制一个宏,就像在这篇文章中找到的一样;

Excel VBA macro to filter records 用于过滤记录的 Excel VBA 宏

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.数据结束后,您将剩下 R = 到行号。 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.这也可以用于列,然后你可以使用像 Cells(C , R).Select 这样的东西,如果你把 C 作为列表示。

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:与 ActiveSheet 一起使用:

Call SelectAllCellsInSheet(ActiveSheet.Name)

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

I have found that the Worksheet ".UsedRange" method is superior in many instances to solve this problem.我发现工作表“.UsedRange”方法在许多情况下都可以很好地解决这个问题。 I struggled with a truncation issue that is a normal behaviour of the ".CurrentRegion" method.我遇到了截断问题,这是“.CurrentRegion”方法的正常行为。 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).使用 [ Worksheets("Sheet1").Range("A1").CurrentRegion ] 不会产生我想要的结果,当工作表包含一列且行中有空格(并且需要空格)时。 In this case, the ".CurrentRegion" will truncate at the first record.在这种情况下,“.CurrentRegion”将在第一条记录处被截断。 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

Maybe this might work:也许这可能有效:

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

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:我得到的结果是录制宏,从选择单元格 A76 开始:

Sub find_last_row()
    Range("A76").Select
    Range(Selection, Selection.End(xlDown)).Select
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM