English 中文(简体)
将工作表从一个Excel文档复制到另一个
原标题:copying worksheets from one Excel document to another
  • 时间:2011-06-01 02:19:08
  •  标签:
  • excel
  • vba

考虑2个Excel文档:Excel文件A和Excel文件B。这些Excel文件中有工作表(文件A有A、B、c工作表,文件B有d、e、f工作表)。

我需要复制文件A、工作表A(依此类推)中的内容,并将其粘贴到我的试错工作表的第二页。我知道我需要为此做循环,但仅此而已。

我对这个编程很陌生,更不用说VBA了。

我想把表a中的内容复制到我的第二张纸上,把表b中的内容拷贝到表3中,以此类推。

问题回答

一些让你开始的提示

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

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

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
Dim x as Integer
Dim wbA as Workbook, wbB as Workbook

Set wbA = Workbooks("FileA")
Set wbB = Workbooks("FileB")

For x=1 to wbA.Sheets.Count
   wbA.sheets(x).Copy After:=wbB.sheets((2*x)-1)
Next x

Well, eight years after this question has been originally asked, Power Query has been integrated into Excel and comes to help. From your destination workbook (“B”) you can load the source worksheets from “A” following these steps.

  1. With workbook “B” as the active workbook go to “Data” -> “Get Data” -> “From File” -> “From Workbook” and select your source workbook “A” in the file browser.
  2. The Power Query Navigator dialog will appear.
  3. Select the table/worksheet you want to copy and press “Load” (“Transform Data” can be used to fine-tune the import).
  4. The source worksheet will be loaded.
  5. Repeat steps 1. to 4. for every worksheet you want to copy.

此过程的好处是,在源数据更改的情况下,刷新目标“B”中的数据所需做的就是触发“数据”->;“全部刷新”。





相关问题
import of excel in SQL imports NULL lines

I have a stored procedure that imports differently formatted workbooks into a database table, does work on them then drops the table. Here is the populating query. SELECT IDENTITY(INT,1,1) AS ID ...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

Excel date to Unix timestamp

Does anyone know how to convert an Excel date to a correct Unix timestamp?

C# GemBox Excel Import Error

I am trying to import an excel file into a data table using GemBox and I keep getting this error: Invalid data value when extracting to DataTable at SourceRowIndex: 1, and SourceColumnIndex: 1. As ...

Importing from excel "applications" using SSIS

I am looking for any tips or resources on importing from excel into a SQL database, but specifically when the information is NOT in column and row format. I am currently doing some pre-development ...

热门标签