Excel VBA - 选择动态单元格范围 [英] Excel VBA - select a dynamic cell range
问题描述
Range(A1:M1)。选择
如果要选择包含所有标题的变量范围单元格:
Dim sht as WorkSheet
Set sht = This Workbook.Sheets(Data)
'Range(Cells(1,1),Cells(1,Columns.Count).End(xlToLeft))。选择'<<非ROBUST
sht.Range(sht.Cells(1,1),sht.Cells(1,Columns.Count).End(xlToLeft))。选择
$ c $只要该行没有其他内容,c>
。
编辑:更新以强调当使用范围(单元格(...),单元格(...))
最好符合 Range
和单元格
与工作表引用。
I want to be able to dynamically select a range of cells (the heading row), where the row is 1 but the columns with be for 1 to last column, where "A" is the first Column and where "M" is the last column. I know how to find the last column, but I don't know how to modified the below range to input the first and last column as "A" and "M".
Range("A1:M1").Select
If you want to select a variable range containing all headers cells:
Dim sht as WorkSheet
Set sht = This Workbook.Sheets("Data")
'Range(Cells(1,1),Cells(1,Columns.Count).End(xlToLeft)).Select '<<< NOT ROBUST
sht.Range(sht.Cells(1,1),sht.Cells(1,Columns.Count).End(xlToLeft)).Select
...as long as there's no other content on that row.
EDIT: updated to stress that when using Range(Cells(...), Cells(...))
it's good practice to qualify both Range
and Cells
with a worksheet reference.
这篇关于Excel VBA - 选择动态单元格范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!