Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA:查找固定范围内的最后一行_Excel_Vba - Fatal编程技术网

Excel VBA:查找固定范围内的最后一行

Excel VBA:查找固定范围内的最后一行,excel,vba,Excel,Vba,我试了几个小时寻找可能的答案。我准备放弃了。我还没有找到一个和我所问的情景非常相似的人,也许我忽略了 我想找到特定范围内的最后一行。范围为A7至A21。我希望能够将表单中的输入数据输入到该范围内的空行中 这就是它变得棘手的地方。我在同一张表上还有另外两个类别需要输入数据。数据可能已经在这里了,我想再次找到最后一行,然后输入数据。范围A27:A41 最后一类的范围是A46:A66 希望这里有人能帮助我。在工作表上定义Excel中用作表格的范围。然后在代码中使用: Dim Table1 As lis

我试了几个小时寻找可能的答案。我准备放弃了。我还没有找到一个和我所问的情景非常相似的人,也许我忽略了

我想找到特定范围内的最后一行。范围为
A7
A21
。我希望能够将表单中的输入数据输入到该范围内的空行中

这就是它变得棘手的地方。我在同一张表上还有另外两个类别需要输入数据。数据可能已经在这里了,我想再次找到最后一行,然后输入数据。范围
A27:A41

最后一类的范围是
A46:A66


希望这里有人能帮助我。

在工作表上定义Excel中用作表格的范围。然后在代码中使用:

Dim Table1 As listObject, Table2 As ListObject

With ThisWorkbook.Worksheets("Name of the sheet the tables are on")
    Set Table1 = .ListObjects("Name of the table")
    Set Table2 = .ListObjects("Name of the table")
End With

Dim LastRowT1 As Long, LastRowT2 As Long
LastRowT1 = 1: LastRowT2 = 1
Do Until Table1.DataBodyRange(LastRowT1, 1) = Empty
    LastRowT1 = LastRowT1 + 1
Loop
Do Until Table2.DataBodyRange(LastRowT2, 1) = Empty
    LastRowT2 = LastRowT2 + 1
Loop

'If you run out of space and automatically want to add an extra row add
'the following code.
If LastRowT1 > Table1.ListRows.Count Then
    Table2.ListRows.Add AlwaysInsert:=True
End If
If LastRowT2 > Table2.ListRows.Count Then
    Table2.ListRows.Add AlwaysInsert:=True
End If

LastRowT1和LastRowT2的值应为第一个空行的行号(listobject的行号)。

这将为您指明正确的方向

Sub Main()
    Dim r1 As Range
    Dim r2 As Range
    Dim r3 As Range
    Dim rFind As Range

    'Set your range vars
    Set r1 = Range("A7:A21")
    Set r2 = Range("A27:A41")
    Set r3 = Range("A46:A66")

    'Find the next empty cell and display the address
    On Error Resume Next
   'First range
    Set rFind = r1.Find("*", searchdirection:=xlPrevious).Offset(1, 0)
    If Not rFind Is Nothing Then
        MsgBox "First open cell in " & r1.Address & " is " & rFind.Address
    Else
        MsgBox "First open cell in " & r1.Address & " is " & r1.Cells(1, 1).Address
    End If

    'Second range
    Set rFind = r2.Find("*", searchdirection:=xlPrevious).Offset(1, 0)
    If Not rFind Is Nothing Then
        MsgBox "First open cell in " & r2.Address & " is " & rFind.Address
    Else
        MsgBox "First open cell in " & r2.Address & " is " & r2.Cells(1, 1).Address
    End If

    'Third range
    Set rFind = r3.Find("*", searchdirection:=xlPrevious).Offset(1, 0)
    If Not rFind Is Nothing Then
        MsgBox "First open cell in " & r3.Address & " is " & rFind.Address
    Else
        MsgBox "First open cell in " & r3.Address & " is " & r3.Cells(1, 1).Address
    End If
End Sub

这假设您正在自上而下填充单元格(例如,先填充
A7
,然后填充
A8
,然后填充
A9
,等等)。如果不是这样的话,那么您需要使用循环来代替
。查找
。您肯定需要根据自己的情况进行调整,特别是当您的区域中的所有单元格都已满时的逻辑。

要使您的请求更通用(因此可扩展),您可以创建一个函数来查找任何给定区域的第一行:

Function FindFirstOpenCell(ByVal R As Range) As Integer

  Dim row, col As Integer
  row = R.row
  col = R.Column

  FindFirstOpenCell = Cells(row + R.Rows.Count - 1, col).End(xlUp).row + 1

End Function
从这里,您可以反复调用函数:

Dim row As Integer
row = FindFirstOpenCell(Range("A7:A21"))

Cells(row, 1).Value = "My Next Item"

也许这很有用:如果使用Range(“A7”).CurrentRegion.End(xlDown),则会得到包含单元格A7的区域(或范围)中的最后一个单元格。在这之后,使用偏移量转到下一行就很容易了。很好的建议。表(ListObjects)绝对是一种选择。