0

尝试在 VBA for Excel 中编写宏以查看列表中每一行数据中某一列中的值,如果该值为“是”,则它将整行复制并粘贴到同一工作簿中的不同工作表上. 让我们将这两个工作表命名为“Data”和“Final”。我希望引用工作表,因此在运行代码时打开哪个工作表并不重要。我打算使用一个Do循环来循环遍历一个数据表上的行,直到它发现没有更多条目,并使用 if 语句来检查值。

我对如何从一张纸切换到下一张感到困惑。

如何专门引用不同工作表中的单元格?

这是我想到的伪代码:

Do while DataCells(x,1).Value <> " "
    for each DataCells(x,1).Value="NO"
        if DataCells(x,2).Value > DataCells(x,3).Value or _
        DataCells(x,4).Value < DataCells(x,5).Value 
            'Copy and paste/insert row x from Data to Final sheet adding a new 
            'row for each qualifying row
        else
            x=x+1
        end
    else if DataCells(x,1).Value="YES"   
Loop
'copy and paste entire row to a third sheet
'continue this cycle until all rows in the data sheet are examined
4

1 回答 1

0
Sub FilterAndCopy()

Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual

Dim sh As Worksheet, sh2 As Worksheet
Dim lastrow1 As Long
Dim lastcolumn1 As Long



Set sh = ThisWorkbook.Sheets("Data")
Set sh2 = ThisWorkbook.Sheets("Final")

lastrow1 = sh.Cells(Rows.Count, "A").End(xlUp).Row ' Replace "A" With column that has the most Rows
lastcolumn1 = sh.Cells(1, Columns.Count).End(xlToLeft).Column

With sh.Range(.Cells(1, 1), .Cells(lastrow1, lastcolumn1))

'Replace the number in the field section with your Columns number
    .AutoFilter , _
        Field:=1, _
        Criteria1:="yes"

    .Copy sh2.Range("A1")

End With

Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic

End Sub
于 2013-06-28T15:23:55.960 回答