Copy columns in a new workbook; Run-time error
up vote
0
down vote
favorite
At "New folder" I have the excel workbooks which will be opened by the loop; I want to copy 2 columns in each of these workbooks and paste it in another workbook called "new"
When I run the code I get the Run-time error '91': Object variable or With block variable not set
at line With wb.Worksheets(5) and only data of the first workbook are copied.
How can I fix it?
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:UsersADesktopVBAnew.xlsx")
Pathname = "C:UsersADesktopVBANew 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
excel vba excel-vba
add a comment |
up vote
0
down vote
favorite
At "New folder" I have the excel workbooks which will be opened by the loop; I want to copy 2 columns in each of these workbooks and paste it in another workbook called "new"
When I run the code I get the Run-time error '91': Object variable or With block variable not set
at line With wb.Worksheets(5) and only data of the first workbook are copied.
How can I fix it?
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:UsersADesktopVBAnew.xlsx")
Pathname = "C:UsersADesktopVBANew 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
excel vba excel-vba
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
At "New folder" I have the excel workbooks which will be opened by the loop; I want to copy 2 columns in each of these workbooks and paste it in another workbook called "new"
When I run the code I get the Run-time error '91': Object variable or With block variable not set
at line With wb.Worksheets(5) and only data of the first workbook are copied.
How can I fix it?
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:UsersADesktopVBAnew.xlsx")
Pathname = "C:UsersADesktopVBANew 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
excel vba excel-vba
At "New folder" I have the excel workbooks which will be opened by the loop; I want to copy 2 columns in each of these workbooks and paste it in another workbook called "new"
When I run the code I get the Run-time error '91': Object variable or With block variable not set
at line With wb.Worksheets(5) and only data of the first workbook are copied.
How can I fix it?
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:UsersADesktopVBAnew.xlsx")
Pathname = "C:UsersADesktopVBANew 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
excel vba excel-vba
excel vba excel-vba
asked Nov 21 at 23:50
M.J
51
51
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
You are telling VBA to copy columns A and C from the 5th worksheet of the opened workbook. Seems like it doesn't have five or more sheets.
Yes, that is what I want to do so it has more than five sheets. But now I noticed that when a document with the same name as these worksheets is open, then it gives this error, otherwise it works ok.
– M.J
Nov 22 at 0:53
You cannot have two documents with the same name opened at the same time.
– Michal Rosa
Nov 22 at 0:54
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You are telling VBA to copy columns A and C from the 5th worksheet of the opened workbook. Seems like it doesn't have five or more sheets.
Yes, that is what I want to do so it has more than five sheets. But now I noticed that when a document with the same name as these worksheets is open, then it gives this error, otherwise it works ok.
– M.J
Nov 22 at 0:53
You cannot have two documents with the same name opened at the same time.
– Michal Rosa
Nov 22 at 0:54
add a comment |
up vote
0
down vote
You are telling VBA to copy columns A and C from the 5th worksheet of the opened workbook. Seems like it doesn't have five or more sheets.
Yes, that is what I want to do so it has more than five sheets. But now I noticed that when a document with the same name as these worksheets is open, then it gives this error, otherwise it works ok.
– M.J
Nov 22 at 0:53
You cannot have two documents with the same name opened at the same time.
– Michal Rosa
Nov 22 at 0:54
add a comment |
up vote
0
down vote
up vote
0
down vote
You are telling VBA to copy columns A and C from the 5th worksheet of the opened workbook. Seems like it doesn't have five or more sheets.
You are telling VBA to copy columns A and C from the 5th worksheet of the opened workbook. Seems like it doesn't have five or more sheets.
answered Nov 22 at 0:16
Michal Rosa
953514
953514
Yes, that is what I want to do so it has more than five sheets. But now I noticed that when a document with the same name as these worksheets is open, then it gives this error, otherwise it works ok.
– M.J
Nov 22 at 0:53
You cannot have two documents with the same name opened at the same time.
– Michal Rosa
Nov 22 at 0:54
add a comment |
Yes, that is what I want to do so it has more than five sheets. But now I noticed that when a document with the same name as these worksheets is open, then it gives this error, otherwise it works ok.
– M.J
Nov 22 at 0:53
You cannot have two documents with the same name opened at the same time.
– Michal Rosa
Nov 22 at 0:54
Yes, that is what I want to do so it has more than five sheets. But now I noticed that when a document with the same name as these worksheets is open, then it gives this error, otherwise it works ok.
– M.J
Nov 22 at 0:53
Yes, that is what I want to do so it has more than five sheets. But now I noticed that when a document with the same name as these worksheets is open, then it gives this error, otherwise it works ok.
– M.J
Nov 22 at 0:53
You cannot have two documents with the same name opened at the same time.
– Michal Rosa
Nov 22 at 0:54
You cannot have two documents with the same name opened at the same time.
– Michal Rosa
Nov 22 at 0:54
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422048%2fcopy-columns-in-a-new-workbook-run-time-error%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown