只带一个工作表

时间:2014-06-13 17:30:03

标签: excel vba excel-vba

这是我必须从另一个工作簿中引入工作表的代码。 但我需要的是只带一个工作表(不是全部)。 我还需要宏来将工作表重命名为在其导入的原始工作簿中调用的内容之外的其他内容。这两个人可以帮助我吗?

这就是我所拥有的:

Option Explicit

Private Sub Bring_Workbooks_Click()

Dim directory As String, fileName As String, sheet As Worksheet, total As Integer

Application.ScreenUpdating = False
Application.DisplayAlerts = False

directory = "access path to where the original workbook is stored"
fileName = Dir(directory & "Name of workboork where sheet will be copied from.xls")

Do While fileName <> ""
Workbooks.Open (directory & fileName)

For Each sheet In Workbooks(fileName).Worksheets
    total = Workbooks("Name of my workbook.xlsm").Worksheets.Count
    Workbooks(fileName).Worksheets(sheet.Name).Copy _
    after:=Workbooks("Name of my workbook.xlsm").Worksheets(1)
Next sheet

Workbooks(fileName).Close
fileName = Dir()
Loop

Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub

1 个答案:

答案 0 :(得分:1)

试试这段代码:

Sub Bring_Workbooks_Click()

Dim path, fileName, WkshtOrig, fullName, MyWkbk  As String
Dim total As Integer

path = "C:\VBA\"  'access path to where the original workbook is stored
fileName = "OrigWkbk.xlsx" 'Name of workbook where sheet will be copied from.xls
fullName = path & fileName
WkshtOrig = "My Orig Wksht" 'name of worksheet to be copied & placed in this workbook.
MyWkbk = "StkOvrFlwuser3738555.xlsm" 'What I named my sample workbook

Workbooks.Open fileName:= fullName

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Workbooks(fileName).Worksheets(WkshtOrig).Copy _
    after:=Workbooks(MyWkbk).Worksheets(1)
Workbooks(MyWkbk).Worksheets(WkshtOrig).Select
ActiveSheet.Name = "MyNewName"

Workbooks(fileName).Close

Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub