将工作表从一个Excel文档复制到另一个Excel文档 [英] copying worksheets from one Excel document to another

查看:309
本文介绍了将工作表从一个Excel文档复制到另一个Excel文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑2个Excel文档:Excel文件A和Excel文件B.这些Excel文件在其中有工作表(文件A具有a,b,c工作表,文件B具有d,e,f工作表)。



我需要复制文件A中的所有内容,并将其粘贴到我的试用和错误工作表的第二张表中。我知道我需要为此做循环,但就是这样。



我对这个编程非常新,更别说VBA。



我想将表格a中的任何内容复制到我的第二张表格中,并将表格b中的任何内容复制到表格3中,依此类推。 >

解决方案

有些提示让你开始



(我不完全清楚在你想要的一些细节上,但这应该让你开始)



首先打开这两个工作簿,在其中一个创建一个模块(与此代码无关)哪一个)然后运行宏

 选项显式在模块顶部 - 强制显式声明变量
'一个好东西,特别是在学习
Sub CopySheets()
Dim wbFileA As Workbook
Dim wbFileB As Workbook
Dim sh As Worksheet
Dim shCopAfter As Worksheet

'指向工作簿
设置wbFileA = Application.Workbooks(NameOfFileA.xls)
设置wbFileB = Application.Workbooks(NameOfFileB.xls)

'在F中设置指针到第一页ileB
设置shCopAfter = wbFileB.Sheets(1)

'循环通过文件中的工作表
对于每个sh在wbFileA.Sheets
'将工作表复制到FileB
sh.Copy After:= shCopAfter
'如果书中的最后一张,然后设置shCopyAfter最后一张表
如果ActiveSheet.Index> = wbFileB.Sheets.Count然后
设置shCopAfter = ActiveSheet
Else
'Else将shCopyAfter设置为刚刚复制的一个
设置shCopAfter = wbFileB.Sheets(ActiveSheet.Index + 1)
结束如果
下一步
End Sub


Consider 2 Excel documents: Excel file A and Excel file B. These Excel files have worksheets inside them (file A has a,b,c worksheets and file B has d,e,f worksheets) .

I need to copy whatever is in file A, sheet a(and so on) and paste it to the 2nd sheet of my Trial and error worksheet. I know I need to do looping for this, but that's it.

I am very new to this programming, let alone VBA.

I want to copy whatever is in sheet a, to my 2nd sheet, and whatever is in sheet b, is copied in sheet 3, and so on.

解决方案

Some hints to get you started

(I'm not entirely clear on some of the details you want, but this should get you started)

First open both workbooks, create a module in one of them (doesn't matter to this code which one) then run the macro

Option Explicit ' at top of module - forces explicit declaration of variables,
    'a good thing particularly while learning
Sub CopySheets()
    Dim wbFileA As Workbook
    Dim wbFileB As Workbook
    Dim sh As Worksheet
    Dim shCopAfter As Worksheet

    ' Point to the workbooks
    Set wbFileA = Application.Workbooks("NameOfFileA.xls")
    Set wbFileB = Application.Workbooks("NameOfFileB.xls")

    ' Set pointer to first sheet in FileB
    Set shCopAfter = wbFileB.Sheets(1)

    ' loop through the sheets in FileA
    For Each sh In wbFileA.Sheets
        ' Copy sheet to FileB
        sh.Copy After:=shCopAfter
        ' If last sheet in book then set shCopyAfter to last sheet
        If ActiveSheet.Index >= wbFileB.Sheets.Count Then
            Set shCopAfter = ActiveSheet
        Else
            ' Else set shCopyAfter to the one after the one just copied
            Set shCopAfter = wbFileB.Sheets(ActiveSheet.Index + 1)
        End If
    Next
End Sub

这篇关于将工作表从一个Excel文档复制到另一个Excel文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆