[英]excel vba select range of a table
I want to select a table, it will have fixed columns(4 of them) but can have any number of rows, empty rows as well.我想选择一个表格,它将有固定的列(其中 4 个),但可以有任意数量的行,也可以有空行。 How will I select a table range?我将如何选择表格范围?
For example, the current selection is B2 to E5
比如当前选择的是B2 to E5
How can I get this programmatically in VBA?如何在 VBA 中以编程方式获得它?
Here is how you can set a reference to the Table.以下是设置对表的引用的方法。 You should watch: Excel VBA Introduction Part 5 - Selecting Cells (Range, Cells, Activecell, End, Offset) .您应该观看: Excel VBA 介绍第 5 部分 - 选择单元格(范围、单元格、活动单元格、结束、偏移) 。 It will give you a better understanding of Ranges, Worksheets and the Excel object model.它将让您更好地理解范围、工作表和 Excel 对象模型。
Dim Target As Range
With Worksheets("SheetName")
Set Target = .Range("B2:E2", .Range("B" & .Rows.Count).End(xlUp))
End With
Assuming your table always starts in B2
and has always a width of 4 columns you could use something like this to get the lastrow over all 4 columns:假设你的表格总是从B2
开始并且总是有 4 列的宽度,你可以使用这样的东西来获得所有 4 列的最后一行:
Function getlastrow() As Integer
Dim i As Integer
getlastrow = 0
With Worksheets("YourWorksheet")
For i = 0 To 3
'starting with 2+i=2 (column B) and End with 2+i=5 (column E)
If (.Cells(.Rows.Count, 2 + i).End(xlUp).Row) > getlastrow Then
getlastrow = .Cells(.Rows.Count, 2 + i).End(xlUp).Row
End If
Next i
End With
End Function
With this information you can set your range:有了这些信息,您可以设置您的范围:
Sub SetRange()
Dim myrange As Range
With Worksheets("Tabelle1")
Set myrange = .Range("B2:E" & getlastrow)
End With
End Sub
try,尝试,
Worksheets("Sheet3").Range("Table1[#All]").Select
'or,
Worksheets("Sheet3").ListObjects("Table1").Range.Select
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.