[英]extend selection to last used column vba
Code : 代码:
Sub MyColumnSelect()
Dim myCurrentRow As Long
Dim myLastColumn As Long
myCurrentRow = ActiveCell.Row
myLastColumn = ActiveCell.SpecialCells(xlLastCell).Column
Range(ActiveCell, Cells(myCurrentRow, myLastColumn)).Select
End Sub
I need to extend my selection from any colum or any row to the last used column in the sheet. 我需要将选择范围从任何列或任何行扩展到工作表中最后使用的列。 This code extends the selection to a particular column, in my case to
column P
. 此代码将选择范围扩展到特定列,在我的情况下扩展到
column P
I don't know why is this the case but even if there are only 4 columns, it still extends the selection to column P
. 我不知道为什么会这样,但是即使只有4列,它仍然会将选择范围扩展到
column P
。
PS: I know I shouldn't use select
, but I can't make it work even with select
. PS:我知道我不应该使用
select
,但是即使使用select
也无法使它正常工作。 Need Help. 需要帮忙。
Bit of googling would have absolutely yielded an answer. 一点点谷歌搜索肯定会给出答案。 This is the most common way of retrieving the last actively used row
这是检索最后一个活跃使用的行的最常用方法
Shets("Sheetname").Cells(Rows.Count, <inColumn>).End(xlUp).Row
and column 和列
Shets("Sheetname").Cells(<inRow>, Columns.Count).End(xlToLeft).Column
In your case, presuming your sheet is named " Sheet1
" it would be: 在您的情况下,假设您的工作表名为“
Sheet1
”,则它将为:
Dim lr as Long
Dim lc as Long
lr = Sheets("Sheet1").Cells(Rows.Count, 2).End(xlUp).Row
lc = Sheets("Sheet1").Cells(4, Columns.Count).End(xlToLeft).Column
Obviously, if your column ranges vary, you should retrieve them in a loop (eg. loop through all the active rows) 显然,如果您的列范围有所不同,则应以循环方式检索它们(例如,循环遍历所有活动行)
For i = 3 to lr
lc = Sheets("Sheet1").Cells(i, Columns.Count).End(xlToLeft).Column
' do something
Next i
Also, do absolutely read up on why you should not use Select
, ActiveSheet
and so on.. 另外,请绝对阅读为什么不应该使用
Select
, ActiveSheet
等。
Try this: 尝试这个:
Sub MyColumnSelect()
Dim myRow, myLastColumn As Long
For myRow = 3 To ActiveSheet.Cells(Rows.Count, 2).End(xlUp).Row
myLastColumn = ActiveSheet.Cells(myRow, Columns.Count).End(xlToLeft).Column
ActiveSheet.Range(Cells(myRow, 1), Cells(myRow, myLastColumn)).Copy
Next
End Sub
Ron de Bruin - Find last row, column or last cell show you how to find the last used column. Ron de Bruin-查找最后一行,最后一列或最后一个单元格,向您展示如何查找最后使用的列。
Sub MyColumnSelect()
Dim myCurrentRow As Long
Dim myLastColumn As Long
myCurrentRow = ActiveCell.Row
myLastColumn = Cells.Find(What:="*", After:=Cells(1, 1), _
Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, MatchCase:=False).Column
Range(ActiveCell, Cells(myCurrentRow, myLastColumn)).Select
End Sub
This also works. 这也有效。 But beware that
UsedRange
can sometimes extend past the data do to non-continuous formatting. 但是请注意,
UsedRange
有时可能会超出数据范围而扩展为非连续格式。
Intersect(ActiveCell.EntireRow, ActiveSheet.UsedRange).Select
As @Rawrplus pointed out using ActiveCell
and Selection
are bad habits and should be avoided. 正如@Rawrplus指出,使用
ActiveCell
和Selection
是不良习惯,应避免使用。 Watch: Excel VBA Introduction Part 5 - Selecting Cells (Range, Cells, Activecell, End, Offset) 观看: Excel VBA简介第5部分-选择单元格(范围,单元格,活动单元格,结束,偏移)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.