VBA - Skipping Rows when trying to loop through all rows
I am trying to write code that will loop through each row in each tab in a spreadsheet and if the values in columns W and X are greater than 0, write that row to the next available row in a tab called "Budget" in the same spreadsheet. When I run this, it is skipping every other row. Any advice would be appreciated. I think it has something to do with the selected range and it being relative to the row, but I can't figure out exactly how to set the context properly.
Sub Button1_Click()
Dim source As Worksheet
Dim target As Worksheet
Dim targetLastRow As Long
Dim LastRow As Long
Set target = ThisWorkbook.Sheets("Budget")
targetLastRow = target.Range("A" & target.Rows.Count).End(xlUp).Row
For Each ws In Worksheets
Set source = ws
'do not read rows from budget because that is our target
If source.Name <> "Budget" Then
'get the last row in the current sheet
LastRow = source.Cells(source.Rows.Count, "X").End(xlUp).Row
Set rowRange = source.Range("A1:A" & LastRow)
'Loop through each row
For Each r In rowRange
'if column W and X have valuces then write the row to the target sheet
If source.Cells(r.Row, 24) > 0 And source.Cells(r.Row, 23) > 0 Then
target.Cells(targetLastRow, 1) = source.Cells(r.Row, 23)
'activeRow.EntireRow.Copy target.Cells(1, targetLastRow)
targetLastRow = targetLastRow + 1
End If
Next r
MsgBox ("Processing complete for Sheet: " & source.Name)
End If
Next ws
End Sub
excel vba
add a comment |
I am trying to write code that will loop through each row in each tab in a spreadsheet and if the values in columns W and X are greater than 0, write that row to the next available row in a tab called "Budget" in the same spreadsheet. When I run this, it is skipping every other row. Any advice would be appreciated. I think it has something to do with the selected range and it being relative to the row, but I can't figure out exactly how to set the context properly.
Sub Button1_Click()
Dim source As Worksheet
Dim target As Worksheet
Dim targetLastRow As Long
Dim LastRow As Long
Set target = ThisWorkbook.Sheets("Budget")
targetLastRow = target.Range("A" & target.Rows.Count).End(xlUp).Row
For Each ws In Worksheets
Set source = ws
'do not read rows from budget because that is our target
If source.Name <> "Budget" Then
'get the last row in the current sheet
LastRow = source.Cells(source.Rows.Count, "X").End(xlUp).Row
Set rowRange = source.Range("A1:A" & LastRow)
'Loop through each row
For Each r In rowRange
'if column W and X have valuces then write the row to the target sheet
If source.Cells(r.Row, 24) > 0 And source.Cells(r.Row, 23) > 0 Then
target.Cells(targetLastRow, 1) = source.Cells(r.Row, 23)
'activeRow.EntireRow.Copy target.Cells(1, targetLastRow)
targetLastRow = targetLastRow + 1
End If
Next r
MsgBox ("Processing complete for Sheet: " & source.Name)
End If
Next ws
End Sub
excel vba
1
Just to clarify, by "skipping every other row" do you mean you're getting alternating blank rows on the "Budget" sheet, or is it skipping data from thesource
worksheet?
– Comintern
Nov 15 '18 at 1:15
A few minutes using the debugger should help you figure this out. Have you tried stepping through the loop using it?
– Ken White
Nov 15 '18 at 1:23
Your code is not copying the row; only column W
– Ron Rosenfeld
Nov 15 '18 at 1:24
I did step through and even thougt r.row was the correct sequence, the other variable I added to the watch would look like it went from row1 to row3 to row5.
– Jim
Nov 15 '18 at 16:05
I commented out the copy row for simplicity sake while debugging
– Jim
Nov 15 '18 at 16:05
add a comment |
I am trying to write code that will loop through each row in each tab in a spreadsheet and if the values in columns W and X are greater than 0, write that row to the next available row in a tab called "Budget" in the same spreadsheet. When I run this, it is skipping every other row. Any advice would be appreciated. I think it has something to do with the selected range and it being relative to the row, but I can't figure out exactly how to set the context properly.
Sub Button1_Click()
Dim source As Worksheet
Dim target As Worksheet
Dim targetLastRow As Long
Dim LastRow As Long
Set target = ThisWorkbook.Sheets("Budget")
targetLastRow = target.Range("A" & target.Rows.Count).End(xlUp).Row
For Each ws In Worksheets
Set source = ws
'do not read rows from budget because that is our target
If source.Name <> "Budget" Then
'get the last row in the current sheet
LastRow = source.Cells(source.Rows.Count, "X").End(xlUp).Row
Set rowRange = source.Range("A1:A" & LastRow)
'Loop through each row
For Each r In rowRange
'if column W and X have valuces then write the row to the target sheet
If source.Cells(r.Row, 24) > 0 And source.Cells(r.Row, 23) > 0 Then
target.Cells(targetLastRow, 1) = source.Cells(r.Row, 23)
'activeRow.EntireRow.Copy target.Cells(1, targetLastRow)
targetLastRow = targetLastRow + 1
End If
Next r
MsgBox ("Processing complete for Sheet: " & source.Name)
End If
Next ws
End Sub
excel vba
I am trying to write code that will loop through each row in each tab in a spreadsheet and if the values in columns W and X are greater than 0, write that row to the next available row in a tab called "Budget" in the same spreadsheet. When I run this, it is skipping every other row. Any advice would be appreciated. I think it has something to do with the selected range and it being relative to the row, but I can't figure out exactly how to set the context properly.
Sub Button1_Click()
Dim source As Worksheet
Dim target As Worksheet
Dim targetLastRow As Long
Dim LastRow As Long
Set target = ThisWorkbook.Sheets("Budget")
targetLastRow = target.Range("A" & target.Rows.Count).End(xlUp).Row
For Each ws In Worksheets
Set source = ws
'do not read rows from budget because that is our target
If source.Name <> "Budget" Then
'get the last row in the current sheet
LastRow = source.Cells(source.Rows.Count, "X").End(xlUp).Row
Set rowRange = source.Range("A1:A" & LastRow)
'Loop through each row
For Each r In rowRange
'if column W and X have valuces then write the row to the target sheet
If source.Cells(r.Row, 24) > 0 And source.Cells(r.Row, 23) > 0 Then
target.Cells(targetLastRow, 1) = source.Cells(r.Row, 23)
'activeRow.EntireRow.Copy target.Cells(1, targetLastRow)
targetLastRow = targetLastRow + 1
End If
Next r
MsgBox ("Processing complete for Sheet: " & source.Name)
End If
Next ws
End Sub
excel vba
excel vba
edited Nov 15 '18 at 1:10
Comintern
19.3k42560
19.3k42560
asked Nov 15 '18 at 1:09
JimJim
1
1
1
Just to clarify, by "skipping every other row" do you mean you're getting alternating blank rows on the "Budget" sheet, or is it skipping data from thesource
worksheet?
– Comintern
Nov 15 '18 at 1:15
A few minutes using the debugger should help you figure this out. Have you tried stepping through the loop using it?
– Ken White
Nov 15 '18 at 1:23
Your code is not copying the row; only column W
– Ron Rosenfeld
Nov 15 '18 at 1:24
I did step through and even thougt r.row was the correct sequence, the other variable I added to the watch would look like it went from row1 to row3 to row5.
– Jim
Nov 15 '18 at 16:05
I commented out the copy row for simplicity sake while debugging
– Jim
Nov 15 '18 at 16:05
add a comment |
1
Just to clarify, by "skipping every other row" do you mean you're getting alternating blank rows on the "Budget" sheet, or is it skipping data from thesource
worksheet?
– Comintern
Nov 15 '18 at 1:15
A few minutes using the debugger should help you figure this out. Have you tried stepping through the loop using it?
– Ken White
Nov 15 '18 at 1:23
Your code is not copying the row; only column W
– Ron Rosenfeld
Nov 15 '18 at 1:24
I did step through and even thougt r.row was the correct sequence, the other variable I added to the watch would look like it went from row1 to row3 to row5.
– Jim
Nov 15 '18 at 16:05
I commented out the copy row for simplicity sake while debugging
– Jim
Nov 15 '18 at 16:05
1
1
Just to clarify, by "skipping every other row" do you mean you're getting alternating blank rows on the "Budget" sheet, or is it skipping data from the
source
worksheet?– Comintern
Nov 15 '18 at 1:15
Just to clarify, by "skipping every other row" do you mean you're getting alternating blank rows on the "Budget" sheet, or is it skipping data from the
source
worksheet?– Comintern
Nov 15 '18 at 1:15
A few minutes using the debugger should help you figure this out. Have you tried stepping through the loop using it?
– Ken White
Nov 15 '18 at 1:23
A few minutes using the debugger should help you figure this out. Have you tried stepping through the loop using it?
– Ken White
Nov 15 '18 at 1:23
Your code is not copying the row; only column W
– Ron Rosenfeld
Nov 15 '18 at 1:24
Your code is not copying the row; only column W
– Ron Rosenfeld
Nov 15 '18 at 1:24
I did step through and even thougt r.row was the correct sequence, the other variable I added to the watch would look like it went from row1 to row3 to row5.
– Jim
Nov 15 '18 at 16:05
I did step through and even thougt r.row was the correct sequence, the other variable I added to the watch would look like it went from row1 to row3 to row5.
– Jim
Nov 15 '18 at 16:05
I commented out the copy row for simplicity sake while debugging
– Jim
Nov 15 '18 at 16:05
I commented out the copy row for simplicity sake while debugging
– Jim
Nov 15 '18 at 16:05
add a comment |
1 Answer
1
active
oldest
votes
Give this a try:
Option Explicit
Sub Button1_Click()
Dim Trg As Worksheet: Set Trg = ThisWorkbook.Sheets("Budget")
Dim a As Integer: a = Trg.Cells(Trg.Rows.Count, 1).End(xlUp).Row + 1
Dim T1(), ws As Worksheet, i As Integer
For Each ws In Worksheets
If ws.Name <> "Budget" Then
T1 = ws.Range("A1", ws.Cells.SpecialCells(xlCellTypeLastCell))
For i = 1 To UBound(T1)
If T1(i, 23) > 0 And T1(i, 24) > 0 Then
ws.Cells(i, 1).EntireRow.Copy Trg.Cells(a, 1)
a = a + 1
End If
Next i
MsgBox ("Processing complete for Sheet: " & ws.Name)
End If
Next ws
End Sub
That seemed to work. But for some odd reason when I went back in and ran my code again this morning it worked appropriately. Thanks!
– Jim
Nov 15 '18 at 16:07
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%2f53311043%2fvba-skipping-rows-when-trying-to-loop-through-all-rows%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
Give this a try:
Option Explicit
Sub Button1_Click()
Dim Trg As Worksheet: Set Trg = ThisWorkbook.Sheets("Budget")
Dim a As Integer: a = Trg.Cells(Trg.Rows.Count, 1).End(xlUp).Row + 1
Dim T1(), ws As Worksheet, i As Integer
For Each ws In Worksheets
If ws.Name <> "Budget" Then
T1 = ws.Range("A1", ws.Cells.SpecialCells(xlCellTypeLastCell))
For i = 1 To UBound(T1)
If T1(i, 23) > 0 And T1(i, 24) > 0 Then
ws.Cells(i, 1).EntireRow.Copy Trg.Cells(a, 1)
a = a + 1
End If
Next i
MsgBox ("Processing complete for Sheet: " & ws.Name)
End If
Next ws
End Sub
That seemed to work. But for some odd reason when I went back in and ran my code again this morning it worked appropriately. Thanks!
– Jim
Nov 15 '18 at 16:07
add a comment |
Give this a try:
Option Explicit
Sub Button1_Click()
Dim Trg As Worksheet: Set Trg = ThisWorkbook.Sheets("Budget")
Dim a As Integer: a = Trg.Cells(Trg.Rows.Count, 1).End(xlUp).Row + 1
Dim T1(), ws As Worksheet, i As Integer
For Each ws In Worksheets
If ws.Name <> "Budget" Then
T1 = ws.Range("A1", ws.Cells.SpecialCells(xlCellTypeLastCell))
For i = 1 To UBound(T1)
If T1(i, 23) > 0 And T1(i, 24) > 0 Then
ws.Cells(i, 1).EntireRow.Copy Trg.Cells(a, 1)
a = a + 1
End If
Next i
MsgBox ("Processing complete for Sheet: " & ws.Name)
End If
Next ws
End Sub
That seemed to work. But for some odd reason when I went back in and ran my code again this morning it worked appropriately. Thanks!
– Jim
Nov 15 '18 at 16:07
add a comment |
Give this a try:
Option Explicit
Sub Button1_Click()
Dim Trg As Worksheet: Set Trg = ThisWorkbook.Sheets("Budget")
Dim a As Integer: a = Trg.Cells(Trg.Rows.Count, 1).End(xlUp).Row + 1
Dim T1(), ws As Worksheet, i As Integer
For Each ws In Worksheets
If ws.Name <> "Budget" Then
T1 = ws.Range("A1", ws.Cells.SpecialCells(xlCellTypeLastCell))
For i = 1 To UBound(T1)
If T1(i, 23) > 0 And T1(i, 24) > 0 Then
ws.Cells(i, 1).EntireRow.Copy Trg.Cells(a, 1)
a = a + 1
End If
Next i
MsgBox ("Processing complete for Sheet: " & ws.Name)
End If
Next ws
End Sub
Give this a try:
Option Explicit
Sub Button1_Click()
Dim Trg As Worksheet: Set Trg = ThisWorkbook.Sheets("Budget")
Dim a As Integer: a = Trg.Cells(Trg.Rows.Count, 1).End(xlUp).Row + 1
Dim T1(), ws As Worksheet, i As Integer
For Each ws In Worksheets
If ws.Name <> "Budget" Then
T1 = ws.Range("A1", ws.Cells.SpecialCells(xlCellTypeLastCell))
For i = 1 To UBound(T1)
If T1(i, 23) > 0 And T1(i, 24) > 0 Then
ws.Cells(i, 1).EntireRow.Copy Trg.Cells(a, 1)
a = a + 1
End If
Next i
MsgBox ("Processing complete for Sheet: " & ws.Name)
End If
Next ws
End Sub
answered Nov 15 '18 at 2:23
Display nameDisplay name
53416
53416
That seemed to work. But for some odd reason when I went back in and ran my code again this morning it worked appropriately. Thanks!
– Jim
Nov 15 '18 at 16:07
add a comment |
That seemed to work. But for some odd reason when I went back in and ran my code again this morning it worked appropriately. Thanks!
– Jim
Nov 15 '18 at 16:07
That seemed to work. But for some odd reason when I went back in and ran my code again this morning it worked appropriately. Thanks!
– Jim
Nov 15 '18 at 16:07
That seemed to work. But for some odd reason when I went back in and ran my code again this morning it worked appropriately. Thanks!
– Jim
Nov 15 '18 at 16:07
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%2f53311043%2fvba-skipping-rows-when-trying-to-loop-through-all-rows%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
1
Just to clarify, by "skipping every other row" do you mean you're getting alternating blank rows on the "Budget" sheet, or is it skipping data from the
source
worksheet?– Comintern
Nov 15 '18 at 1:15
A few minutes using the debugger should help you figure this out. Have you tried stepping through the loop using it?
– Ken White
Nov 15 '18 at 1:23
Your code is not copying the row; only column W
– Ron Rosenfeld
Nov 15 '18 at 1:24
I did step through and even thougt r.row was the correct sequence, the other variable I added to the watch would look like it went from row1 to row3 to row5.
– Jim
Nov 15 '18 at 16:05
I commented out the copy row for simplicity sake while debugging
– Jim
Nov 15 '18 at 16:05