如何设置从活动行到最后一行的范围?

问题描述 投票:0回答:2

我希望我的 For 循环能够查看从(并包括)活动行开始的每一行,一直到带有数据的最后一行。然后,它将复制此范围内邮政编码与第一个活动行相同的任何行,并将它们粘贴到新工作簿中。

原代码使用第二行到最后一行。

'Any extra measures need to be copied using an if loop,
'  this is so it will capture all rows underneath with the same postcode.

Dim r As Range
Dim j As Integer
Dim post_code_on_sheet As String
post_code_on_sheet = wb2.Worksheets("Sheet2").Cells(3, 2).Value
lastRow1 = wb.Worksheets("Sheet1").Range("J" & rows.Count).End(xlUp).row
j = 2
Cells(row, 10).Activate

'DEFINE RANGE AS FROM ACTIVE ROW DOWN TO LAST NON-EMPTY ROW
For Each r In wb.Worksheets("Sheet1").Range("J2:J" & lastRow1)
'This copies all of the rows that match within the whole workbook, 
' need it to select only from the active row down
   If r = post_code_on_sheet Then
       wb.Worksheets("Sheet1").rows(r.row).Copy wb2.Worksheets("Sheet1").rows(j)
       j = j + 1
    End If
Next r

我尝试调整范围以从活动单元格行到最后一行。

'Any extra measures need to be copied using an if loop,
' this is so it will capture all rows underneath with the same postcode.

Dim r As Range
Dim j As Integer
Dim post_code_on_sheet As String
post_code_on_sheet = wb2.Worksheets("Sheet2").Cells(3, 2).Value
lastRow1 = wb.Worksheets("Sheet1").Range("J" & rows.Count).End(xlUp).row
j = 2
Cells(row, 10).Activate

'DEFINE RANGE AS FROM ACTIVE ROW DOWN TO LAST NON-EMPTY ROW
For Each r In wb.Worksheets("Sheet1").Range(ActiveCell.row & lastRow1)
'This copies all of the rows that match within the whole workbook, need it to select only from the active row down
   If r = post_code_on_sheet Then
       wb.Worksheets("Sheet1").rows(r.row).Copy wb2.Worksheets("Sheet1").rows(j)
       j = j + 1
    End If
Next r

当我将鼠标悬停在 VBA 窗口中的 ActiveCell.row 上时,它显示分配给它的正确行号,并且 lastRow1 变量也正确显示为 15。

excel vba loops for-loop
2个回答
2
投票

一定数量的列的范围需要一个起始单元格和一个结束单元格,而不是两个行号。

在你的情况下,它是:

'Any extra measures need to be copied using an if loop, this is so it will capture all rows underneath with the same postcode.
'I want the range to be from the active cell, down to the last row. I am having trouble setting this.

Dim r As Range
Dim j As Long
Dim post_code_on_sheet As String
post_code_on_sheet = wb2.Worksheets("Sheet2").Cells(3, 2).Value
lastRow1 = wb.Worksheets("Sheet1").Range("J" & rows.Count).End(xlUp).row
j = 2
Cells(row, 10).Activate 'what is row here?
'if row isn't defined as a proper variable, this will throw an error
'DEFINE RANGE AS FROM ACTIVE ROW DOWN TO LAST NON-EMPTY ROW
For Each r In wb.Worksheets("Sheet1").Range("J" & row & ":J" & lastRow1)
'This copies all of the rows that match within the whole workbook, need it to select only from the active row down
   If r = post_code_on_sheet Then
       wb.Worksheets("Sheet1").rows(r.row).Copy wb2.Worksheets("Sheet1").rows(j)
       j = j + 1
    End If
Next r

如果您只想要值,则无需复制粘贴。

编辑:没有注意到你同时得到了帮助,哎呀。


0
投票

要指定行范围,您必须将范围设置为字符串。

类似这样的事情


For Each r In wb.Worksheets("Sheet1").Range("""" & ActiveCell.row & ":" & lastRow1 & """")

它会抛出错误,因为这只迭代行。如果你想让每个单元格都迭代,你必须像这样添加 Cells 属性

For Each r In wb.Worksheets("Sheet1").Range("""" & ActiveCell.row & ":" & lastRow1 & """").Cells
© www.soinside.com 2019 - 2024. All rights reserved.