VBA使用值查找最后一行,而不检查每个单元格

时间:2016-11-27 12:28:55

标签: vba excel-vba excel

对于我的excel VBA项目,我有以下代码,找到包含任何数据的最后一行。它会检查工作表中的每一行,并查看是否插入了一个值(自下而上):

table.table-border {
  border: 2px solid #E6E9ED;
}
table,
th,
td {
  border: 1px solid #E6E9ED;
  text-align: center
}
thead tr:nth-child(2) th:first-child,
tbody td:first-child {
  border-right-width: 0;
}
thead tr:nth-child(2) th:nth-child(2),
tbody td:nth-child(2) {
  border-left-width: 0;
}
thead tr:nth-child(2) th:nth-child(2) ~ th,
tbody td:nth-child(2) ~ td {
  border-left-width: 0;
}
thead tr:nth-child(2) th:nth-child(2) ~ th:not(:last-child),
tbody td:nth-child(2) ~ td:not(:last-child) {
  border-right-width: 0;
}

这很有效,但问题是如果循环遍历所有记录,那么完成超过1.000.000的计算以找到具有值的最后一行。我选择这种方法是因为我的数据不一致,它由3列组成,所有都可以拥有一些数据。举例说明:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<div class="table-responsive">
  <table class="table table-condensed table-border table-striped">
    <thead>
      <tr>
        <th colspan="2">h12</th>
        <th colspan="4">h345</th>
      </tr>
      <tr>
        <th>h1</th>
        <th>h2</th>
        <th>h3</th>
        <th>h4</th>
        <th>h5</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>abc</td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
      </tr>
      <tr>
        <td>efg</td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
      </tr>
      <tr>
        <td>hij</td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
      </tr>
    </tbody>
  </table>
</div>

我唯一的保证是3列中至少有一列填充了数据。是否可以在VBA中编写一个从顶部开始并检查A或B或C是否已填充的函数,如果不是,则应返回前一行? (没有很多代码?)

4 个答案:

答案 0 :(得分:2)

也许这样的事情,找到每列的最后一行并比较它是否为具有最高值的 LastRow

Option Explicit

Sub FindLastRow()

Dim col As Integer
Dim LastRow As Long
Dim MaxLastRow As Long

MaxLastRow = 2 ' initialize value

' modify "Sheet1" to your sheet's name
With Sheets("Sheet1")
    ' loop from column A to Column C >> can modify easily
    For col = 1 To 3
        LastRow = .Cells(.Rows.Count, col).End(xlUp).Row
        If MaxLastRow < LastRow Then MaxLastRow = LastRow
    Next col
End With

End Sub

答案 1 :(得分:1)

已编辑以处理不连续的范围

你可以尝试这个功能:

Function RangeLastRow(rng As Range) As Long
    If WorksheetFunction.CountA(rng) > 0 Then
        With Intersect(rng.SpecialCells(xlCellTypeConstants).EntireRow, rng).EntireRow
            RangeLastRow = Split(.Areas(.Areas.Count).Address, "$")(2)
        End With
    End If
End Function

将返回:

  • 零,如果传递的范围只有空单元格

  • 非空“最远”单元格的行索引,如果通过范围至少有一个非空单元格

答案 2 :(得分:0)

好像你已经有了一些有用的答案。为了各种各样,我会把帽子扔进戒指。

Sub test()
Dim Count As Long
Dim A As String
Dim B As String
Dim C As String
With Sheets("Sheet1")
Range("D1").Select
For Count = 1 To 1000000
A = .Cells(Count, "A")
B = .Cells(Count, "B")
C = .Cells(Count, "C")

combined = A & B & C
.Cells(Count, "D").Value = combined
DoEvents
If combined = "" Then GoTo Line100
Next
End With

Line100:

End Sub

此宏停止在A,B或C列中没有任何值的最后一行。

答案 3 :(得分:0)

摘自John Walkenbach的书&#39; Excel 2007 Power Programming with VBA&#39;尝试:

Nextrow = cells(rows.count, 1).end(xlup).row +1