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

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

问题描述

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

  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))。选择



编辑:更新以强调当使用范围(单元格(...),单元格(...))最好符合 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屋!

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