在vba中定义dynamic范围
如何在电子表格中的最后一个单元格旁边的列中指定单元格,例如,如果我有数据
Range(Cells(1, 1), Cells(lastRow, lastColumn))
我如何dynamic定义
Range(Cells(1, 2), Cells(lastRow, n_lastColumn))
在n_lastColumn
引用lastColumn
之后的下一列的lastColumn
?
Range(Cells(1,1), Cells(lastRow, lastColumn)).offset(,1)
要么
Range("A1").currentregion.offset(,1)
您可以使用Worksheet
对象上可用的UsedRange
属性。
但是请注意,这给你一个水印使用范围,而不是目前使用的范围; 即如果你在一个单元格中写入一个值然后删除它,它仍然包含UsedRange
。
sub find_last() Dim LastRow As Integer Dim LastCol As Integer Dim WS as Worksheet Set WS = ThisWorkbook.Worksheets("MySheet") LastRow = WS.Cells.Find(What:="*", After:=WS.Range("A1"), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False, MatchByte:=False).row LastCol = WS.Cells.Find(What:="*", After:=WS.Range("A1"), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False, MatchByte:=False).Column WS.Cells(LastRow, LastCol + 1) = "MyData" end sub