我的最大数据范围是C34:S64
,带有值的列从左到右放置。列数和行数会有所不同(但所有列的行数相同)。
我使用xlDown
和xlToRight
查找第一个空白单元格如下:
Lastrow = Range("C34").End(xlDown).Offset(1, 0)
Lastcol = Range("C34").End(xlToRight).Offset(0, 1)
我正在努力将这些信息纳入边界
如何将Lastrow
和Lastcol
限制为C64
和S34
?
我的表格中包含了其他数据,以及它的外观 sheet http://oi66.tinypic.com/2rn7jnt.jpg
答案 0 :(得分:1)
要确定要突出显示的范围,请创建一些帮助函数
Function MyMax(p_x As Integer, p_y As Integer) As Integer
If p_x >= p_y Then MyMax = p_x Else MyMax = p_y
End Function
Function GetRange(p_StartRange As Range, p_MaxRow As Integer, p_MaxCol As Integer) As Range
Dim lRow as Integer
lRow = MyMax(p_StartRange.End(xlDown).Row, p_MaxRow)
Dim lCol as Integer
lCol = MyMax(p_StartRange.End(xlToRight).Column, p_MaxCol)
Set GetRange = Range(Cells(p_StartRange.Row, p_StartRange.Column), Cells(lRow, lCol))
End Function
然后在你的代码中
Dim rngData As Range: Set rngData = GetRange(Range("C34"), 64, 19)
答案 1 :(得分:0)
要查找上次使用的行,请尝试此操作。
Sub FindingLastRow()
'PURPOSE: Different ways to find the last row number of a range
'SOURCE: www.TheSpreadsheetGuru.com
Dim sht As Worksheet
Dim LastRow As Long
Set sht = ThisWorkbook.Worksheets("Sheet1")
'Ctrl + Shift + End
LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
'Using UsedRange
sht.UsedRange 'Refresh UsedRange
LastRow = sht.UsedRange.Rows(sht.UsedRange.Rows.Count).Row
'Using Table Range
LastRow = sht.ListObjects("Table1").Range.Rows.Count
'Using Named Range
LastRow = sht.Range("MyNamedRange").Rows.Count
'Ctrl + Shift + Down (Range should be first cell in data set)
LastRow = sht.Range("A1").CurrentRegion.Rows.Count
End Sub
另外,要查找上次使用的列,请查看此内容。
Sub FindingLastColumn()
'PURPOSE: Different ways to find the last column number of a range
'SOURCE: www.TheSpreadsheetGuru.com
Dim sht As Worksheet
Dim LastColumn As Long
Set sht = ThisWorkbook.Worksheets("Sheet1")
'Ctrl + Shift + End
LastColumn = sht.Cells(7, sht.Columns.Count).End(xlToLeft).Column
'Using UsedRange
sht.UsedRange 'Refresh UsedRange
LastColumn = sht.UsedRange.Columns(sht.UsedRange.Columns.Count).Column
'Using Table Range
LastColumn = sht.ListObjects("Table1").Range.Columns.Count
'Using Named Range
LastColumn = sht.Range("MyNamedRange").Columns.Count
'Ctrl + Shift + Right (Range should be first cell in data set)
LastColumn = sht.Range("A1").CurrentRegion.Columns.Count
End Sub