首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用VBA代码在工作簿中的工作表之间复制和粘贴

使用VBA代码在工作簿中的工作表之间复制和粘贴
EN

Stack Overflow用户
提问于 2013-06-28 22:22:16
回答 1查看 3.7K关注 0票数 0

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

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

如何在不同的工作表中明确引用单元格?

下面是我想要的伪代码:

代码语言:javascript
运行
复制
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
EN

回答 1

Stack Overflow用户

发布于 2013-06-28 23:23:56

代码语言:javascript
运行
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17366920

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档