将最后一个excel工作表导入到另一个工作簿中,作为第一个工作表

时间:2018-10-04 08:56:34

标签: excel vba excel-vba import worksheet

我有两个excel文件,一个具有多个excel工作表,每个月作为工作表名称,对于8月,它将有8个工作表,对于10月将有10个工作表,依此类推。另一个excel是我正在处理的excel文件,我需要将每月excel文件中的最后一个工作表作为第一个工作表导入到该excel中,因为在第一个工作表中需要有宏代码。

简而言之,将最后一个工作表(工作表名称始终更改)的重复项/“创建新副本”导入到另一个工作簿中作为第一个工作表。

Dim wbCopy As Workbook
Dim wsCopy As Worksheet
Dim rngCopy As Range
Dim wbPaste As Workbook
Dim wsPaste As Worksheet
Dim rngPaste As Range

Set wbPaste = ActiveWorkbook
Set wbCopy = Workbooks.Open("O:\abc\Inventory\Monthly.xlsm") 
Set wsCopy = wbCopy.Worksheets("Sheet1")  'Question- how to always select last worksheet?
Set rngCopy = wsCopy.Range("a:aa").EntireColumn  'Question- can i duplicate a copy of worksheet ?
Set wsPaste = wbPaste.Worksheets("Order Quantities")  
Set rngPaste = wsPaste.Range("a1")  'Question- this just paste into "Order Quantities", but how to move or duplicate the worksheet into first worksheet in excel workbook. ?

rngCopy.Copy
rngPaste.PasteSpecial

Workbooks.Application.CutCopyMode = False
Workbooks("Monthly.xlsm").Close False

1 个答案:

答案 0 :(得分:0)

Worksheets.count将为您提供队列中最后一个工作表的索引。

dim wbPaste  as workbook

Set wbPaste = ActiveWorkbook

with Workbooks.Open("O:\abc\Inventory\Monthly.xlsm", readonly:=true) 

    .workSheets(.Worksheets.count).Copy Before:=wbPaste.Sheets(1)
    .close savechanges:=false

end with

'optionally rename the new imported worksheet
wbPaste.workSheets(1).name = "abc"