需要确定整行的最后一行

时间:2015-01-28 21:46:54

标签: excel vba excel-vba

我不是程序员,但已经设法拼凑了大量代码,这些代码适用于4个相当大的项目(对我来说是啊!)我已经尝试了很多方法来找到最后一行。有些人为我工作有些不合适。我可以找到一些能给我“实际”的#34;最后一行,不管A列中的空格(这是我需要的)。然而,我不能为我的生活想象如何将该代码与我将数组中的值从一个工作簿传递到另一个工作簿的方式进行集成。所有的代码都可以正常工作" As Is"但我需要找到一种更好的方法来搜索最后一行的整行(当前列A:O),然后复制数据。 A列有时可能是空的,以避免代码被覆盖," Last Row"需要检查整行。我目前正在使用"强制隐藏单元格(A7)。"作为强制占位符。任何建议都很棒。

 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

4 个答案:

答案 0 :(得分:1)

这是"锯齿状的常见问题"数据如:

enter image description here

显然,此列 B 有最后一行。
这是通过循环四个候选列来获得整个 Last 行的一种方法:

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

答案 1 :(得分:1)

查找最适合大多数情况,下面是我使用的函数,它将sheet ref作为输入并返回行号作为Long类型

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

答案 2 :(得分:1)

最简单的方法可能是使用specialcells方法,如range.specialcells(xllastcell)中所示。这将返回其行号是电子表格中任何位置使用的最后一行的单元格,其列是工作表中任何位置使用的最后一列。 (我不认为重要的是"范围"您指定;结果始终是工作表中的最后一个单元格。)

因此,如果您在单元格B30X5以及其他地方都有数据,cells.specialcells(xllastcell)将指向单元格X30(而range("A1").specialcells(xlastcell)也会指向单元格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)

答案 3 :(得分:0)

经过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

这可以找到" Real"最后一行和列并忽略列A或J中的任何似乎影响某些LastRow片段的空单元格。我需要将它改为ROWS而不是ROW,并且还要添加Offset部分。 (-10)让我回到专栏" A"对于我的工作表,现在我已经删除了Column" A" {强制占位符"。"}并且拥有" Real"那里有数据。对于" Hacking Code Cobbler"。

很高兴他们在工作中付钱给我学习这些东西。 :)解决了这一段时间。刚刚更新这篇文章。