如何使用VBA中的“Range”选择整个excel表? [英] How do you select the entire excel sheet with Range using Macro in VBA?
问题描述
我在c#中找到了一个类似的解决方案。
请参阅以下链接
I found a similar solution to this question in c#... See link below
如何在Excel的Excel.Range对象中选择工作表中的所有单元格? / a>
How to Select all the cells in a worksheet in Excel.Range object of c#?
有没有人在VBA中执行此操作?我不太熟悉VBA,所以这将是有帮助的。这是我到目前为止...
Does anyone have a snippet to do this in VBA? I'm not really familiar with VBA, so this would be helpful. Here's what I've got so far...
我通过使用ctrl + shift over arrow,向下箭头选择数据正常选择数据,以选择整个单元格范围。当我在一个宏中运行它时,它会编码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")
这很简单,我可以很容易地做到自己没有一个宏,但我试图使整个过程是一个宏,这只是一块。
This is very simple, and 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屋!