根据条件复制和粘贴行

时间:2019-06-13 22:18:27

标签: excel vba

基于两个条件复制并粘贴整行

对于一个学校项目,我试图找到同时满足(两个)条件的所有行,然后将整个行粘贴到数据集的末尾。我正在尝试使用变量进行此操作,因为电子表格可能会在教授将执行的测试中发生变化。我不断收到“下标超出范围”错误。我的读取和分配给P和T,for循环,if语句和count函数都可以正常工作。

numrow = Rows(Rows.Count).End(xlUp).row
numcolumn = Columns(Columns.Count).End(xlUp).Column
P = Range(Cells(3, 1), Cells(numrow, 1)).Value
T = Range(Cells(3, 2), Cells(numrow, 2)).Value

For i = LBound(P, 1) To UBound(P, 1)
    If P(i, 1) = 5 And T(i, 1) = 100 Then
       countrow = countrow + 1 'check: return is 25

       'Range(i, numcolumn).copy Sheets("Sheet1").End(xlUp).Offset(1, 0)
        Worksheets("Sheet1").Range(Cells(i, numcolumn)).Value.copy
        lastrow = ActiveSheet.Cells(1, 1).CurrentRegion.Rows.Count + 1
        Range(Cells(lastrow, 1)).PasteSpecial xlPasteFormulasAndNumberFormats
        Application.CutCopyMode = False
'I would also like to change all the cells that were just pasted in the first column to the value of 2.5 here, but I have no idea where to start with that
    End If
Next i

如您所见,我尝试了两种方法 1.) Range(i,numcolumn)。复制Sheets(“ Sheet1”)。End(xlUp).Offset(1,0) (目前已评论) 2.) Worksheets(“ Sheet1”)。Range(Cells(i,numcolumn))。Value.copy

当我尝试调试并出现“下标超出范围”错误时,两者都会突出显示

0 个答案:

没有答案