Excel VBA查找范围中的最后一行

即时通讯发现最后一行有点麻烦。

我想要做的是find列“A”中的最后一行,然后用它来查找范围内的最后一行。

数据示例:

数据的例子

1) LR_wbSelect = wbshtSelect.cells(Rows.count, "A").End(xlUp).Row - 22 2) LR_wbSelectNew = wbshtSelect.cells(LR_wbSelect, "A").End(xlUp).Row 

我正在使用列“A”中的最后一行,因为第29行的数据总是相同的长度,第29行的列“B”中使用的行可以是不同数量的行。

所以我试图在列“A”中使用LR_wbSelect来获取我的最后LR_wbSelectNew ,然后在LR_wbSelectNew使用它作为查找的起点。

这个工程当我设置为“A”列, LR_wbSelectNew给我的行“17”,但是当我把LR_wbSelectNew的列LR_wbSelectNew为“B”它没有给出正确的最后一行“18”。

我可以将列更改为“C,D,E,F”,代码工作正常,但是我可以使用的唯一列是“B”,因为它总是有数据,其余行可能有一个空白单元格。

在表单上进行一些testing之后,通过按下LR_wbSelect列的“B”的列表中的CRTL&Up忽略行中的数据并转到find数据的行。 我不明白为什么Excel不认为这些单元格中有数据?

希望我已经解释了,所以你可以按照,如果不是请问。

如果需要的话,我可以上传完整的代码,如果需要,请让我知道。

任何帮助,将不胜感激。

searchLastRow(在列B中)有多个结果和方法。

当使用Cells(.Rows.Count, "B").End(xlUp).Row ,将会得到列B中数据的最后一行(它忽略了包含空格的行,并且一直向下)。

使用时:

  With wbshtSelect.Range("B10").CurrentRegion LR_wbSelectNew = .Rows(.Rows.Count).Row End With 

您正在使用CurrentRegion B列中的数据search最后一行,该行从单元格B10开始,直到没有数据的第一行(它停在空行的第一行)。

完整代码:

 Sub GetLastRow() Dim wbshtSelect As Worksheet Dim LR_wbSelectNew As Long ' modify "Sheet2" to your sheet's name Set wbshtSelect = Sheets("Sheet2") ' find last row with data in Column B With wbshtSelect LR_wbSelectNew = .Cells(.Rows.Count, "B").End(xlUp).Row End With ' for debug only Debug.Print LR_wbSelectNew ' >>result 31 ' find last row with data in Column B at current regioun starting at cell B10 With wbshtSelect.Range("B10").CurrentRegion LR_wbSelectNew = .Rows(.Rows.Count).Row End With ' for debug only Debug.Print LR_wbSelectNew ' >> result 18 End Sub 

编辑1 :代码search最后一行的单元格的值(它忽略空白单元格内的公式)。

 Sub GetLastRow() Dim wbshtSelect As Worksheet Dim LR_wbSelectNew As Long ' modify "Sheet2" to your sheet's name Set wbshtSelect = Sheets("Sheet2") ' find last row with data in Column B at current regioun starting at cell B10 With wbshtSelect.Range("B10").CurrentRegion LR_wbSelectNew = .Rows(.Rows.Count).Row End With Dim Rng As Range Set Rng = wbshtSelect.Range("B10:B" & LR_wbSelectNew) ' find last row inside the range, ignore values inside formulas LR_wbSelectNew = Rng.Find(What:="*", _ After:=Range("B10"), _ LookAt:=xlPart, _ LookIn:=xlValues, _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, _ MatchCase:=False).Row ' for debug Debug.Print LR_wbSelectNew ' << result 18 (with formulas in the range) End Sub 

如果您的wbshtSelect被定义为工作表,并且您已经使用set来定义特定的工作表,那么可以使用它。

  Dim LastRow As Long wbshtSelect.UsedRange ' Refresh UsedRange LastRow = wbshtSelect.UsedRange.Rows(wbshtSelect.UsedRange.Rows.Count).Row 

否则看看这里http://www.ozgrid.com/VBA/ExcelRanges.htm

 LR_wbSelectNew = wbshtSelect.cells(LR_wbSelect, "B").End(xlUp).Row 

你为什么使用“LR_wbSelect”作为行计数器? 如果你想知道列“B”的最后一行,你应该使用Rows.count

Rows.count – >返回最大行数(Excel 2007及更高版本为1048576)End(xlUp) – >将指针向上移动到最后使用的行

所以,单元格(Rows.count,“A”)。结束(xlUp).Row – >这将指针移动到最后一行,如果列'A'(就像当你按下Crtl +向上键时A1048576单元格select)

因此,使用Rows.count来select列“B”的最后一行。 如果您有与LR_wbSelect相关的特定要求,请提及它。

或者,如果您想知道工作表中使用的最后一行,则可以使用以下内容:

 mySheet.Cells.SpecialCells(xlCellTypeLastCell).Row 
 LR_wbSelect = ThisWorkbook.Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row 

希望这段代码有帮助!

 Sub LastRowInOneColumn() 'Find the last used row in a Column: column A in this example Dim LastRow As Long With ActiveSheet LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row End With MsgBox LastRow End Sub 

Range().End会带你到一个代码块的结尾。 如果起始单元格为空,则会为您带来第一个使用的单元格或最后一个单元格。 它的细胞不是空的它把你带到最后使用的细胞。 因此,您需要testing列B中的单元格是否要确定是否使用LR_wbSelectNew作为最后一行。

 With wbshtSelect LR_wbSelect = .Cells(Rows.Count, "A").End(xlUp).Row - 22 If .Cells(LR_wbSelect, "B") <> "" Then LR_wbSelectNew = LR_wbSelect Else LR_wbSelectNew = .Cells(LR_wbSelect, "B").End(xlUp).Row End If End With 

这段代码定义了一个目标范围,它从A1延伸到a-22列的最后一行,并扩展了10列。

 Dim Target As Range With wbshtSelect Set Target = .Range("A1", .Cells(Rows.Count, "A").End(xlUp).Offset(-22)).Resize(, 10) End With 

试图做到这一点的问题是,如果一个单元格包含一个公式,那么即使公式返回空白,Excel也会认为它已经填充。

  'This is sure method to find or catch last row in any column even 'if some cell are blank in-between. (Excel-2007)` 'This works even if sheet is not active 'mycol is the column you want to get last row number for n=1048575 to 1 step -1 myval=cells(n,mycol) if myval<>"" then mylastrow=n 'this is last row in the column exit for end if next ret=msgbox("Last row in column-" & mycol & "is=" & mylastrow)