复制新工作簿中的列;运行时错误

时间:2018-11-21 23:50:20

标签: excel vba excel-vba

在“新文件夹”中,我有将通过循环打开的excel工作簿;我想在每个工作簿中复制2列,然后将其粘贴到另一个名为“新”的工作簿中

运行代码时,我得到运行时错误'91':对象变量或With块变量未设置

行,使用wb.Worksheets(5),仅复制第一个工作簿的数据。

我该如何解决?

Option Explicit

Sub ProcessFiles()

Dim Filename, Pathname As String
Dim wb As Workbook
Dim wbMain As Workbook
Dim i As Integer

Set wbMain = Workbooks.Open("C:\Users\A\Desktop\VBA\new.xlsx")
Pathname = "C:\Users\A\Desktop\VBA\New folder\"

Filename = Dir(Pathname)
i = 1

Do While Filename <> ""
    Set wb = Workbooks.Open(Pathname & Filename)
    Enter_Formulas wb, wbMain, i

    wb.Close SaveChanges:=True
    Filename = Dir()
    i = i + 2
Loop   
End Sub


Sub Enter_Formulas(wb As Workbook, wbMain As Workbook, i)
With wb.Worksheets(5)
    .Columns(1).Copy Destination:=wbMain.Worksheets(2).Columns(i)
    .Columns(3).Copy Destination:=wbMain.Worksheets(2).Columns(i + 1)
End With   
End Sub

1 个答案:

答案 0 :(得分:0)

您要告诉VBA复制打开的工作簿的第5个工作表中的列A和C。好像没有五张或更多张纸。