想要选择列“H”中值大于或等于“2.5”的行,然后想要选择与该行相邻的行。以便我可以将它们复制到新工作表例如(请参阅附件图片)我想选择第 409 410 411 行,因为第 410 行的值等于“H”列中的“2.5”请建议一个 VBA 代码,我的试验代码看起来像这样 Sub Selectrows() Dim lastrow As Long Dim i As Range lastrow = Range("E" & Rows.Count).End(xlUp).row For Each i In Range("H4:H" & lastrow) If i.Value >= 2.5 Then i.Select ActiveCell.Rows("1:1").EntireRow.Select End If Next I End Sub
PS:对不起我的英语,我是英语和VBA的新手
这是您的代码的可能增强功能(注释中的解释)
Option Explicit
Sub Selectrows()
Dim lastrow As Long
Dim cel As Range
Dim destSht As Worksheet
Set destSht = Worksheets("myDestinationSheetName") '<-- change "myDestinationSheetName" to your actual destination sheet name
With Worksheets("mySourceSheetName") ' reference source sheet - change "mySourceSheetName" to your actual source sheet name
lastrow = .Range("E" & .Rows.Count).End(xlUp).Row ' get referenced sheet column E last not empty cell row index
For Each cel In .Range("H4:H" & lastrow).SpecialCells(xlCellTypeFormulas) ' loop through referenced sheet column H cells cointaining formulas from row 4 down to column E las not empty one
If cel.Value >= 2.5 Then cel.Offset(-1, 0).Resize(3, 1).EntireRow.Copy Destination:=destSht.Cells(destSht.Rows.Count, 1).End(xlUp).Offset(1)
Next
End With
End Sub
如你所见,我建议
用于With .. End With block
引用特定对象(Worksheets("mySourceSheetName")
在本例中),并在其内部的每个子成员(如 Range、Cells 等)之前使用点 (.) 以确保它们确实属于引用的对象
使用对象的SpecialCells()
方法Range
仅过滤具有“常量”(即不是从公式派生的)内容的单元格
使用.Offset()
属性来抵消它被调用的范围对象(cel
在这种情况下)向上一行和零列
使用.Resize()
属性将被调用的范围对象(cel.Offset(-1,0)
在本例中)扩大到三行高度和一列宽度
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句