需要确定整行上的LastRow

我不是一个程序员,但已经设法凑齐大量的代码,在4个相当大的项目上工作(对我来说,Yay!)我已经尝试了很多方法来find最后一行。 有些为我工作有些不。 我可以find一些给我的“实际”最后一行,而不pipe列A中的空白(这是我所需要的)。 然而,我不能为我的生活形象如何整合代码与我从我的数组从一个工作簿传递值到另一个的方式。 所有的代码工作“原样”,但我需要find一个更好的方式search整个行(目前列A:O)的最后一行,然后复制数据。 列A可能有时是空的,为了避免代码被覆盖,“最后一行”需要检查整行。 我目前正在用“。”强制隐藏的单元格(A7)。 作为强制占位符。 任何build议将是真棒。

Option Explicit Public Sub SaveToLog15() Dim rng As Range, aCell As Range Dim MyAr() As Variant Dim n As Long, i As Long Dim LastRow As Long Dim NextCell As Range Dim Sheet2 As Worksheet Set Sheet2 = ActiveSheet Application.ScreenUpdating = False With Sheet2 ' rng are the cells you want to read into the array. ' Cell A7 (".") is a needed "Forced Place Holder" for last row _ determination ' A7 will go away once "better" LastRow can be added to this code Set rng = Worksheets("Main").Range("A7,D22,D19,D20,J22:J24,E23,D21,J25:J27,D62,D63,G51") ' counts number of cells in MyAr n = rng.Cells.Count ' Redimensions array for above range ReDim MyAr(1 To n) ' Sets start cell at 1 or "A" n = 1 ' Loops through cells to add data to the array For Each aCell In rng.Cells MyAr(n) = aCell.Value n = n + 1 Next aCell End With On Error Resume Next ' Opens "Test Log.xls" Workbooks.Open FileName:= _ "S:\Test Folder\Test Log.xls" ' SUBROUTINE 1 "Disable Sheet Protection and Show All" REMOVED ' Finds last row on Tab "Tracking" based on Column "A" ' Last row determination DOES NOT go to next row if first _ Column is blank ' Use A7 "." to always force Data to Col A '********************************************************************** 'THIS WORKS FINE BUT DOES NOT RECOGNIZE THE POSSIBLE BLANK IN COL A. With Worksheets("Incoming Data") Set NextCell = Worksheets("Incoming Data").Cells _ (Worksheets("Incoming Data").Rows.Count, "A").End(xlUp).Offset(1, 0) End With ' I need this code replaced by the following code or integrated into ' this code snippet. I am lost on how to make that happen. '*********************************************************************** '*********************************************************************** 'THIS CODE FINDS THE "ACTUAL" LAST ROW AND THIS IS WHAT I'D LIKE TO USE ' I need to figure how to integrate this code block with the above ' Or maybe redo the whole thing. LastRow = Cells.Find(What:="*", After:=[A1], _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious).Row MsgBox ("The Last Row Is: " & LastRow) ' I am not using this code in the program. It's just there to show ' what I need to use because it works. I need to make this code work 'WITH the above block. '*********************************************************************** ' Sets the size of the new array and copies MyAr to it NextCell.Resize(1, UBound(MyAr)).Value = (MyAr) ' SUBROUTINE 2 "Add borders to cells in range" REMOVED ' SUBROUTINE 3 "Re-enable Sheet Protection" REMOVED ActiveWorkbook.Save 'ActiveWindow.Close Application.ScreenUpdating = True MsgBox "Your Data has been saved to the Log File: " & vbCrLf & vbCrLf _ & "'Test Log.xls'", vbInformation, "Log Save Confirmation" End Sub 

这是一个“锯齿状”的数据通常的问题,如:

在这里输入图像说明

显然这里B列是最后一行。
这是通过遍历四个候选列来获得整个最后一行的一种方法:

 Sub RealLast() Dim m As Long m = 0 For i = 1 To 4 candidate = Cells(Rows.Count, i).End(xlUp).Row If candidate > m Then m = candidate Next i MsgBox m End Sub 

find最适合大多数情况下,我使用的function,作为input作为input和返回行号为龙型

 Dim lLastRow As Long lLastRow = LastUsedRow(shName) Private Function LastUsedRow(sh As Worksheet) As Long LastUsedRow = sh.Cells.Find(What:="*", After:=sh.Cells.Cells(1), _ LookAt:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, MatchCase:=False).Row End Function 

最简单的事情可能是使用range.specialcells(xllastcell)方法,如range.specialcells(xllastcell) 。 这将返回行号是电子表格中任何位置使用的最后一行的单元格,并且其列是在工作表中任何位置使用的最后一列。 (我认为这不重要,你指定的“范围”,结果总是工作表上的最后一个单元格。)

所以如果你在单元格B30X5以及其他地方都有数据, cells.specialcells(xllastcell)将指向单元格X30 (和range("A1").specialcells(xlastcell)cells.specialcells(xllastcell)也将指向单元格X30 )。

代替:

 LastRow = Cells.Find(What:="*", After:=[A1], _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious).Row MsgBox ("The Last Row Is: " & LastRow) 

用这个:

 LastRow = cells.specialcells(xllastcell).row MsgBox ("The Last Row Is: " & LastRow) 

经过35次尝试,这是我能够侵入我的原始代码:

  ' Used to determine LastRow, LastColumn, LastCell, NextCell Dim LastRow As Long Dim LastColumn As Integer Dim LastCell As Range, NextCell As Range With Worksheets("Tracking") ' Find LastRow. Works Best. 1st and last cells can be empty If WorksheetFunction.CountA(Cells) > 0 Then 'Search for any entry, by searching backwards by Rows. LastRow = Cells.Find(What:="*", After:=[A1], _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious).Row 'Search for any entry, by searching backwards by Columns. LastColumn = Cells.Find(What:="*", After:=[A1], _ SearchOrder:=xlByColumns, _ SearchDirection:=xlPrevious).Column 'MsgBox "Last Cell" & vbCrLf & vbCrLf & Cells(LastRow, LastColumn).Address 'MsgBox "The Last Row is: " & vbCrLf & vbCrLf & LastRow 'MsgBox "The Last Column is: " & vbCrLf & vbCrLf & LastColumn End If ' Number of columns based on actual size of log range NOT MyAr(n) Set NextCell = Worksheets("Tracking").Cells(LastRow + 1, (LastColumn - 10)) End With 

这find了“真正的”最后一行和列,并忽略了列A或J中的任何空单元格,这似乎影响了一些LastRow片段。 我需要使它成为ROWS,而不是ROW,并添加Offset部分。 (-10)让我回到列“A”为我的表,现在我已经删除列“A”{强制占位符“。”},并有“真实”的数据在那里。 YAY为“黑客代码补鞋匠”。

很高兴他们付钱给我学习这些东西。 :)解决了这一段时间回来。 刚刚要更新这个职位。