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 |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
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
1,239614
1,239614
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 |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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