Excel VBA - 选择动态单元格范围 [英] Excel VBA - select a dynamic cell range

查看:59
本文介绍了Excel VBA - 选择动态单元格范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够动态选择一系列单元格(标题行),其中行为 1,但列为 1 到最后一列,其中A"是第一列,M"是最后一列.我知道如何找到最后一列,但我不知道如何修改以下范围以将第一列和最后一列输入为A"和M".

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.

更新以强调在使用 Range(Cells(...), Cells(...)) 时,最好同时限定 Range 和 <带有工作表参考的代码>单元格.

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屋!

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