如何使用VBA使用Range选择整个Excel工作表? [英] How do you select the entire excel sheet with Range using VBA?

查看:850
本文介绍了如何使用VBA使用Range选择整个Excel工作表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c#

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#?

在VBA中执行此操作的过程是什么?

What is the process to do this in VBA?

我通常通过使用"Ctrl +向上移,向下移"来选择整个单元格区域来选择数据.例如,当我在宏中运行此代码时,它会编码为A1:Q398247930.我需要它只是

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


在其他部分,我可能想使用相同的代码,但范围是"C3:行和列的结尾". VBA中有没有一种方法可以获取文档中最后一个单元格的位置?

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?

推荐答案

我相信您想找到A1的当前区域和周围的单元格-不一定是图纸上的所有单元格. 如果是这样-只需使用... 范围("A1").CurrentRegion

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

这篇关于如何使用VBA使用Range选择整个Excel工作表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆