Fill Excel file with for loop
I'm trying to fill an Excel file using a for loop, the logic for filling is to do it every 189 results, for Example:
Fill from A1 to A189
Fill from B1 to B189
At the moment I have a code which works fine with first row A, but the problem is when I try it with B, C, D, etc.
This is my code:
'' I don't need first 4 results.
'' Total rows in this case is 569
For index as Integer = 5 To totalRows Step 1
Dim column as Integer = 2
'' var used to know if row is completed and change the ExcelProcess method
If rowsCompleted = 1 Then
'' realRows = 569 / 3 rounded down = 189
'' Flag initial value = 5 and is used as a internal index value instead of index var at for loop.
If flag <= realRows Then
'' copy
'' Excel Range = spreadsheet1.Cells(index, 7)
'' paste
'' Excel Range = spreadsheet2.Cells(8 * rowsCompleted - 6, index)
flag = flag + 1
Else
'' copy
'' Excel Range = spreadsheet1.Cells(index + 2, 7)
flag = flag + 1
'' paste
'' Excel Range = spreadshee2.Cells(8 * rowsCompleted - 6, flag)
End If
Else
rowsCompleted = rowsCompleted + 1
flag = 5
End If
Next
Debugging step by step I founded some important details.
First row contains 190 rows but the first one is not needed so I only need 189 rows and start on 5. second and third row contains 189 rows so there is no problem. Also I need to start on 5 row.
Also I found that with my code the first row ends fine on 190. second row must ends on 379 but I found that ends on 381 or 382. So I think that maybe the problem is with my for loop and index or flag vars.
Also I think that maybe the problem is with copy the value with this code:
Excel Range = spreadsheet1.Cells(index + 2, 7) because I'm adding + 2.
excel vba vb.net
add a comment |
I'm trying to fill an Excel file using a for loop, the logic for filling is to do it every 189 results, for Example:
Fill from A1 to A189
Fill from B1 to B189
At the moment I have a code which works fine with first row A, but the problem is when I try it with B, C, D, etc.
This is my code:
'' I don't need first 4 results.
'' Total rows in this case is 569
For index as Integer = 5 To totalRows Step 1
Dim column as Integer = 2
'' var used to know if row is completed and change the ExcelProcess method
If rowsCompleted = 1 Then
'' realRows = 569 / 3 rounded down = 189
'' Flag initial value = 5 and is used as a internal index value instead of index var at for loop.
If flag <= realRows Then
'' copy
'' Excel Range = spreadsheet1.Cells(index, 7)
'' paste
'' Excel Range = spreadsheet2.Cells(8 * rowsCompleted - 6, index)
flag = flag + 1
Else
'' copy
'' Excel Range = spreadsheet1.Cells(index + 2, 7)
flag = flag + 1
'' paste
'' Excel Range = spreadshee2.Cells(8 * rowsCompleted - 6, flag)
End If
Else
rowsCompleted = rowsCompleted + 1
flag = 5
End If
Next
Debugging step by step I founded some important details.
First row contains 190 rows but the first one is not needed so I only need 189 rows and start on 5. second and third row contains 189 rows so there is no problem. Also I need to start on 5 row.
Also I found that with my code the first row ends fine on 190. second row must ends on 379 but I found that ends on 381 or 382. So I think that maybe the problem is with my for loop and index or flag vars.
Also I think that maybe the problem is with copy the value with this code:
Excel Range = spreadsheet1.Cells(index + 2, 7) because I'm adding + 2.
excel vba vb.net
The destination file, you want all data in 1 col. right?
– alowflyingpig
Nov 26 '18 at 2:14
@alowflyingpig Hello, no... in spreadsheet1 I have info but in different rows and I want to pass it to spreadsheet2 in one row but differents columns, as you can see on for loop .Cells()... I'm gonna start pasting on 2,5 then 2,6, 2,7, 2,8, and it must ends on 2, 189, then code enter to else statment and now pasting starts on 10, 5, 10, 6, and must ends on 10, 189.
– User1899289003
Nov 26 '18 at 2:24
Do you mean 5,2 6,2? When specifyingcellsits row then col. thus.Cells(5,2)=.Cells("B5")
– alowflyingpig
Nov 26 '18 at 2:41
To understand your question, you want data in spreadsheet1 A1-A189 copied to spreadsheet2 B5 - B193, spreadsheet1 B1 - B189 to spreadsheet2 J5 - J193 etc..
– alowflyingpig
Nov 26 '18 at 2:47
add a comment |
I'm trying to fill an Excel file using a for loop, the logic for filling is to do it every 189 results, for Example:
Fill from A1 to A189
Fill from B1 to B189
At the moment I have a code which works fine with first row A, but the problem is when I try it with B, C, D, etc.
This is my code:
'' I don't need first 4 results.
'' Total rows in this case is 569
For index as Integer = 5 To totalRows Step 1
Dim column as Integer = 2
'' var used to know if row is completed and change the ExcelProcess method
If rowsCompleted = 1 Then
'' realRows = 569 / 3 rounded down = 189
'' Flag initial value = 5 and is used as a internal index value instead of index var at for loop.
If flag <= realRows Then
'' copy
'' Excel Range = spreadsheet1.Cells(index, 7)
'' paste
'' Excel Range = spreadsheet2.Cells(8 * rowsCompleted - 6, index)
flag = flag + 1
Else
'' copy
'' Excel Range = spreadsheet1.Cells(index + 2, 7)
flag = flag + 1
'' paste
'' Excel Range = spreadshee2.Cells(8 * rowsCompleted - 6, flag)
End If
Else
rowsCompleted = rowsCompleted + 1
flag = 5
End If
Next
Debugging step by step I founded some important details.
First row contains 190 rows but the first one is not needed so I only need 189 rows and start on 5. second and third row contains 189 rows so there is no problem. Also I need to start on 5 row.
Also I found that with my code the first row ends fine on 190. second row must ends on 379 but I found that ends on 381 or 382. So I think that maybe the problem is with my for loop and index or flag vars.
Also I think that maybe the problem is with copy the value with this code:
Excel Range = spreadsheet1.Cells(index + 2, 7) because I'm adding + 2.
excel vba vb.net
I'm trying to fill an Excel file using a for loop, the logic for filling is to do it every 189 results, for Example:
Fill from A1 to A189
Fill from B1 to B189
At the moment I have a code which works fine with first row A, but the problem is when I try it with B, C, D, etc.
This is my code:
'' I don't need first 4 results.
'' Total rows in this case is 569
For index as Integer = 5 To totalRows Step 1
Dim column as Integer = 2
'' var used to know if row is completed and change the ExcelProcess method
If rowsCompleted = 1 Then
'' realRows = 569 / 3 rounded down = 189
'' Flag initial value = 5 and is used as a internal index value instead of index var at for loop.
If flag <= realRows Then
'' copy
'' Excel Range = spreadsheet1.Cells(index, 7)
'' paste
'' Excel Range = spreadsheet2.Cells(8 * rowsCompleted - 6, index)
flag = flag + 1
Else
'' copy
'' Excel Range = spreadsheet1.Cells(index + 2, 7)
flag = flag + 1
'' paste
'' Excel Range = spreadshee2.Cells(8 * rowsCompleted - 6, flag)
End If
Else
rowsCompleted = rowsCompleted + 1
flag = 5
End If
Next
Debugging step by step I founded some important details.
First row contains 190 rows but the first one is not needed so I only need 189 rows and start on 5. second and third row contains 189 rows so there is no problem. Also I need to start on 5 row.
Also I found that with my code the first row ends fine on 190. second row must ends on 379 but I found that ends on 381 or 382. So I think that maybe the problem is with my for loop and index or flag vars.
Also I think that maybe the problem is with copy the value with this code:
Excel Range = spreadsheet1.Cells(index + 2, 7) because I'm adding + 2.
excel vba vb.net
excel vba vb.net
edited Nov 26 '18 at 6:38
JohnyL
3,6901924
3,6901924
asked Nov 26 '18 at 0:31
User1899289003User1899289003
179217
179217
The destination file, you want all data in 1 col. right?
– alowflyingpig
Nov 26 '18 at 2:14
@alowflyingpig Hello, no... in spreadsheet1 I have info but in different rows and I want to pass it to spreadsheet2 in one row but differents columns, as you can see on for loop .Cells()... I'm gonna start pasting on 2,5 then 2,6, 2,7, 2,8, and it must ends on 2, 189, then code enter to else statment and now pasting starts on 10, 5, 10, 6, and must ends on 10, 189.
– User1899289003
Nov 26 '18 at 2:24
Do you mean 5,2 6,2? When specifyingcellsits row then col. thus.Cells(5,2)=.Cells("B5")
– alowflyingpig
Nov 26 '18 at 2:41
To understand your question, you want data in spreadsheet1 A1-A189 copied to spreadsheet2 B5 - B193, spreadsheet1 B1 - B189 to spreadsheet2 J5 - J193 etc..
– alowflyingpig
Nov 26 '18 at 2:47
add a comment |
The destination file, you want all data in 1 col. right?
– alowflyingpig
Nov 26 '18 at 2:14
@alowflyingpig Hello, no... in spreadsheet1 I have info but in different rows and I want to pass it to spreadsheet2 in one row but differents columns, as you can see on for loop .Cells()... I'm gonna start pasting on 2,5 then 2,6, 2,7, 2,8, and it must ends on 2, 189, then code enter to else statment and now pasting starts on 10, 5, 10, 6, and must ends on 10, 189.
– User1899289003
Nov 26 '18 at 2:24
Do you mean 5,2 6,2? When specifyingcellsits row then col. thus.Cells(5,2)=.Cells("B5")
– alowflyingpig
Nov 26 '18 at 2:41
To understand your question, you want data in spreadsheet1 A1-A189 copied to spreadsheet2 B5 - B193, spreadsheet1 B1 - B189 to spreadsheet2 J5 - J193 etc..
– alowflyingpig
Nov 26 '18 at 2:47
The destination file, you want all data in 1 col. right?
– alowflyingpig
Nov 26 '18 at 2:14
The destination file, you want all data in 1 col. right?
– alowflyingpig
Nov 26 '18 at 2:14
@alowflyingpig Hello, no... in spreadsheet1 I have info but in different rows and I want to pass it to spreadsheet2 in one row but differents columns, as you can see on for loop .Cells()... I'm gonna start pasting on 2,5 then 2,6, 2,7, 2,8, and it must ends on 2, 189, then code enter to else statment and now pasting starts on 10, 5, 10, 6, and must ends on 10, 189.
– User1899289003
Nov 26 '18 at 2:24
@alowflyingpig Hello, no... in spreadsheet1 I have info but in different rows and I want to pass it to spreadsheet2 in one row but differents columns, as you can see on for loop .Cells()... I'm gonna start pasting on 2,5 then 2,6, 2,7, 2,8, and it must ends on 2, 189, then code enter to else statment and now pasting starts on 10, 5, 10, 6, and must ends on 10, 189.
– User1899289003
Nov 26 '18 at 2:24
Do you mean 5,2 6,2? When specifying
cells its row then col. thus .Cells(5,2) = .Cells("B5")– alowflyingpig
Nov 26 '18 at 2:41
Do you mean 5,2 6,2? When specifying
cells its row then col. thus .Cells(5,2) = .Cells("B5")– alowflyingpig
Nov 26 '18 at 2:41
To understand your question, you want data in spreadsheet1 A1-A189 copied to spreadsheet2 B5 - B193, spreadsheet1 B1 - B189 to spreadsheet2 J5 - J193 etc..
– alowflyingpig
Nov 26 '18 at 2:47
To understand your question, you want data in spreadsheet1 A1-A189 copied to spreadsheet2 B5 - B193, spreadsheet1 B1 - B189 to spreadsheet2 J5 - J193 etc..
– alowflyingpig
Nov 26 '18 at 2:47
add a comment |
1 Answer
1
active
oldest
votes
Why do you need to loop?
Range("A2:D189").Copy
spreadsheet2.range("A2").PasteSpecial xlpastevalues
It's not really very clear what you are trying to achieve at the start you say Fill from A1 to A189 Fill from B1 to B189 but then you say for columns A to D which is fine.
The second block of text says
First row contains 190 rows but the first one is not needed so I only need 189 rows and start on 5. second and third row contains 189 rows so there is no problem. Also I need to start on 5 row.
Also I found that with my code the first row ends fine on 190. second row must ends on 379 but I found that ends on 381 or 382. So I think that maybe the problem is with my for loop and index or flag vars.
I am having a hard time digesting what you mean when you say First row contains 190 rows but first is not needed (I assume you want row 2 to 190?) but then you say that you need it to start on 5 row so I am not sure if you want it to be from row to or 5??
Then you say second row must end on 379 so that doesn't make much sense other than it's 190 doubled.
Can you give a clearer outline if what you want to achieve? what do range do you want to populate and where from?
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',
autoActivateHeartbeat: false,
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%2f53473405%2ffill-excel-file-with-for-loop%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
Why do you need to loop?
Range("A2:D189").Copy
spreadsheet2.range("A2").PasteSpecial xlpastevalues
It's not really very clear what you are trying to achieve at the start you say Fill from A1 to A189 Fill from B1 to B189 but then you say for columns A to D which is fine.
The second block of text says
First row contains 190 rows but the first one is not needed so I only need 189 rows and start on 5. second and third row contains 189 rows so there is no problem. Also I need to start on 5 row.
Also I found that with my code the first row ends fine on 190. second row must ends on 379 but I found that ends on 381 or 382. So I think that maybe the problem is with my for loop and index or flag vars.
I am having a hard time digesting what you mean when you say First row contains 190 rows but first is not needed (I assume you want row 2 to 190?) but then you say that you need it to start on 5 row so I am not sure if you want it to be from row to or 5??
Then you say second row must end on 379 so that doesn't make much sense other than it's 190 doubled.
Can you give a clearer outline if what you want to achieve? what do range do you want to populate and where from?
add a comment |
Why do you need to loop?
Range("A2:D189").Copy
spreadsheet2.range("A2").PasteSpecial xlpastevalues
It's not really very clear what you are trying to achieve at the start you say Fill from A1 to A189 Fill from B1 to B189 but then you say for columns A to D which is fine.
The second block of text says
First row contains 190 rows but the first one is not needed so I only need 189 rows and start on 5. second and third row contains 189 rows so there is no problem. Also I need to start on 5 row.
Also I found that with my code the first row ends fine on 190. second row must ends on 379 but I found that ends on 381 or 382. So I think that maybe the problem is with my for loop and index or flag vars.
I am having a hard time digesting what you mean when you say First row contains 190 rows but first is not needed (I assume you want row 2 to 190?) but then you say that you need it to start on 5 row so I am not sure if you want it to be from row to or 5??
Then you say second row must end on 379 so that doesn't make much sense other than it's 190 doubled.
Can you give a clearer outline if what you want to achieve? what do range do you want to populate and where from?
add a comment |
Why do you need to loop?
Range("A2:D189").Copy
spreadsheet2.range("A2").PasteSpecial xlpastevalues
It's not really very clear what you are trying to achieve at the start you say Fill from A1 to A189 Fill from B1 to B189 but then you say for columns A to D which is fine.
The second block of text says
First row contains 190 rows but the first one is not needed so I only need 189 rows and start on 5. second and third row contains 189 rows so there is no problem. Also I need to start on 5 row.
Also I found that with my code the first row ends fine on 190. second row must ends on 379 but I found that ends on 381 or 382. So I think that maybe the problem is with my for loop and index or flag vars.
I am having a hard time digesting what you mean when you say First row contains 190 rows but first is not needed (I assume you want row 2 to 190?) but then you say that you need it to start on 5 row so I am not sure if you want it to be from row to or 5??
Then you say second row must end on 379 so that doesn't make much sense other than it's 190 doubled.
Can you give a clearer outline if what you want to achieve? what do range do you want to populate and where from?
Why do you need to loop?
Range("A2:D189").Copy
spreadsheet2.range("A2").PasteSpecial xlpastevalues
It's not really very clear what you are trying to achieve at the start you say Fill from A1 to A189 Fill from B1 to B189 but then you say for columns A to D which is fine.
The second block of text says
First row contains 190 rows but the first one is not needed so I only need 189 rows and start on 5. second and third row contains 189 rows so there is no problem. Also I need to start on 5 row.
Also I found that with my code the first row ends fine on 190. second row must ends on 379 but I found that ends on 381 or 382. So I think that maybe the problem is with my for loop and index or flag vars.
I am having a hard time digesting what you mean when you say First row contains 190 rows but first is not needed (I assume you want row 2 to 190?) but then you say that you need it to start on 5 row so I am not sure if you want it to be from row to or 5??
Then you say second row must end on 379 so that doesn't make much sense other than it's 190 doubled.
Can you give a clearer outline if what you want to achieve? what do range do you want to populate and where from?
answered Nov 26 '18 at 2:50
Dan DonoghueDan Donoghue
4,6661631
4,6661631
add a comment |
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.
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%2f53473405%2ffill-excel-file-with-for-loop%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
The destination file, you want all data in 1 col. right?
– alowflyingpig
Nov 26 '18 at 2:14
@alowflyingpig Hello, no... in spreadsheet1 I have info but in different rows and I want to pass it to spreadsheet2 in one row but differents columns, as you can see on for loop .Cells()... I'm gonna start pasting on 2,5 then 2,6, 2,7, 2,8, and it must ends on 2, 189, then code enter to else statment and now pasting starts on 10, 5, 10, 6, and must ends on 10, 189.
– User1899289003
Nov 26 '18 at 2:24
Do you mean 5,2 6,2? When specifying
cellsits row then col. thus.Cells(5,2)=.Cells("B5")– alowflyingpig
Nov 26 '18 at 2:41
To understand your question, you want data in spreadsheet1 A1-A189 copied to spreadsheet2 B5 - B193, spreadsheet1 B1 - B189 to spreadsheet2 J5 - J193 etc..
– alowflyingpig
Nov 26 '18 at 2:47