通过VBA中的列循环来复制整行

我试图从第14行开始循环直到最后。 我写了下面的代码,但它停止工作在范围(“K14:”)行。 我尝试使用Range(“K14”和Rows.Count),但也没有帮助。

Windows("Price VolatilityDM.xlsm").Activate Sheets("Volatility Static Data").Activate Dim x As Single Dim Cell As Range For Each Cell In Range("K14:") If Cell.Value > 0.25 Then Sheets("Volatility Static Data").Range("B:K").Copy Windows("Tolerance ReportDM.xslm").Activate Sheets("Sheet1").Range("K17:Q17").Paste End If Next Cell 

 Windows("Price VolatilityDM.xlsm").Activate Sheets("Volatility Static Data").Activate Set sh = ThisWorkbook.Workheets("Volatility Static Data") ' add a reference to the sheet for simplicity Dim x As Single Dim Cell As Range Dim lastRow lastRow = sh.Cells(sh.Rows.Count, "K").End(xlUp).Row ' get the last row For Each Cell In Range("K14:K" & lastRow) If Cell.Value > 0.25 Then Sheets("Volatility Static Data").Range("B:K").Copy Windows("Tolerance ReportDM.xslm").Activate Sheets("Sheet1").Range("K17:Q17").Paste End If Next Cell 

你只需要findRange对象的末尾,并确保你迭代到那个。 往上看; 如果有任何问题,请告诉我。

它停在那里,因为你还没有写完整个范围。 "K14:"语法无效。 例如,您可以执行: "K14:K" & LastRow

你可以使用像这样的东西来find从第14列开始的K列的结尾:

 dim end as range set cell = range("K14") 'go down one cell at a time until you find that 'the next one is empty. This is the end of the column do while not cell.offset(1,0).value = "" set cell = cell.offset(1,0) loop set end = cell 

然后for each cell in range("K14:" & end.address)

在你的代码中看起来像这样:

 Windows("Price VolatilityDM.xlsm").Activate Sheets("Volatility Static Data").Activate Dim x As Single Dim Cell As Range dim end as range set cell = range("K14") 'go down one cell at a time until you find that 'the next one is empty. This is the end of the column do while not cell.offset(1,0).value = "" set cell = cell.offset(1,0) loop set end = cell For Each Cell In Range("K14:" & end.address) If Cell.Value > 0.25 Then Sheets("Volatility Static Data").Range("B:K").Copy Windows("Tolerance ReportDM.xslm").Activate Sheets("Sheet1").Range("K17:Q17").Paste End If Next Cell