My nested For-Each loop isn't going through each cell in a given range
In Excel VBA:
My second For-Each loop is only going through the first cell in a given column.
I need help getting it to go through each cell in the entire column.
In my code included here, I want to loop through each cell in a row (no issue). then once a cell in the row matches my criteria (no issue), I want to loop through each cell (celltwo) in that cell's column (issue).
I'm able to loop through the row and identify my criteria, but then the second for-each loop only considers the first celltwo in the given column. So I never get a celltwo with .row >=10, the first cell in each column has row=1.
Any help is appreciated.
This is for VBA in Excel. I've tried different ways of identifying my second range to loop through but nothing has allowed the second for-each loop to cycle back from "Next Celltwo" to the beginning of the loop.
Sub WriteSummary()
Dim UploadRange As Range
Dim SummaryRow As Integer
Dim CategoryRange As Range
Dim Cell As Range
Dim Celltwo As Range
''''''''''''''''''''''''''''''''''''''''''''''''''''
'Set Variables
Set MacroFile = ThisWorkbook
Set MacroSheet = ThisWorkbook.Worksheets("Macro")
Set UploadDash = ThisWorkbook.Worksheets("Upload Dash")
Set SummarySheet = ThisWorkbook.Worksheets("Summary")
Set IndexSheet = ThisWorkbook.Worksheets("Indexes")
Set CategoryRange = UploadDash.Range("5:5")
''''''''''''''''''''''''''''''''''''''''''''''''''''
'Determine Output Row
SummaryRow = SummarySheet.Cells(Rows.Count, 1).End(xlUp).Row + 1
''''''MY PROBLEM STARTS HERE'''''''
For Each Cell In CategoryRange
If Cell.Value = 8840 Then
For Each Celltwo In SummarySheet.Cells(, Cell.Column).EntireColumn
i = MsgBox(Celltwo.Row, vbOKOnly)
If Celltwo.Row >= 10 Then
If Celltwo.Value > 0 Then
o = MsgBox(Celltwo.AddressLocal)
SummaryRow = SummaryRow + 1
Else
End If
Else
End If
Next Celltwo '''''DOES NOT LOOP'''''
Else
End If
Next Cell
I expect that when the code finds cell.value = 8840 it will then loop through each cell in that cell's column. Instead, it only loops through the first cell in that column and exits the second for-each loop
excel vba for-loop if-statement
add a comment |
In Excel VBA:
My second For-Each loop is only going through the first cell in a given column.
I need help getting it to go through each cell in the entire column.
In my code included here, I want to loop through each cell in a row (no issue). then once a cell in the row matches my criteria (no issue), I want to loop through each cell (celltwo) in that cell's column (issue).
I'm able to loop through the row and identify my criteria, but then the second for-each loop only considers the first celltwo in the given column. So I never get a celltwo with .row >=10, the first cell in each column has row=1.
Any help is appreciated.
This is for VBA in Excel. I've tried different ways of identifying my second range to loop through but nothing has allowed the second for-each loop to cycle back from "Next Celltwo" to the beginning of the loop.
Sub WriteSummary()
Dim UploadRange As Range
Dim SummaryRow As Integer
Dim CategoryRange As Range
Dim Cell As Range
Dim Celltwo As Range
''''''''''''''''''''''''''''''''''''''''''''''''''''
'Set Variables
Set MacroFile = ThisWorkbook
Set MacroSheet = ThisWorkbook.Worksheets("Macro")
Set UploadDash = ThisWorkbook.Worksheets("Upload Dash")
Set SummarySheet = ThisWorkbook.Worksheets("Summary")
Set IndexSheet = ThisWorkbook.Worksheets("Indexes")
Set CategoryRange = UploadDash.Range("5:5")
''''''''''''''''''''''''''''''''''''''''''''''''''''
'Determine Output Row
SummaryRow = SummarySheet.Cells(Rows.Count, 1).End(xlUp).Row + 1
''''''MY PROBLEM STARTS HERE'''''''
For Each Cell In CategoryRange
If Cell.Value = 8840 Then
For Each Celltwo In SummarySheet.Cells(, Cell.Column).EntireColumn
i = MsgBox(Celltwo.Row, vbOKOnly)
If Celltwo.Row >= 10 Then
If Celltwo.Value > 0 Then
o = MsgBox(Celltwo.AddressLocal)
SummaryRow = SummaryRow + 1
Else
End If
Else
End If
Next Celltwo '''''DOES NOT LOOP'''''
Else
End If
Next Cell
I expect that when the code finds cell.value = 8840 it will then loop through each cell in that cell's column. Instead, it only loops through the first cell in that column and exits the second for-each loop
excel vba for-loop if-statement
Option Explicit. Always.
– AJD
Jan 3 at 21:33
add a comment |
In Excel VBA:
My second For-Each loop is only going through the first cell in a given column.
I need help getting it to go through each cell in the entire column.
In my code included here, I want to loop through each cell in a row (no issue). then once a cell in the row matches my criteria (no issue), I want to loop through each cell (celltwo) in that cell's column (issue).
I'm able to loop through the row and identify my criteria, but then the second for-each loop only considers the first celltwo in the given column. So I never get a celltwo with .row >=10, the first cell in each column has row=1.
Any help is appreciated.
This is for VBA in Excel. I've tried different ways of identifying my second range to loop through but nothing has allowed the second for-each loop to cycle back from "Next Celltwo" to the beginning of the loop.
Sub WriteSummary()
Dim UploadRange As Range
Dim SummaryRow As Integer
Dim CategoryRange As Range
Dim Cell As Range
Dim Celltwo As Range
''''''''''''''''''''''''''''''''''''''''''''''''''''
'Set Variables
Set MacroFile = ThisWorkbook
Set MacroSheet = ThisWorkbook.Worksheets("Macro")
Set UploadDash = ThisWorkbook.Worksheets("Upload Dash")
Set SummarySheet = ThisWorkbook.Worksheets("Summary")
Set IndexSheet = ThisWorkbook.Worksheets("Indexes")
Set CategoryRange = UploadDash.Range("5:5")
''''''''''''''''''''''''''''''''''''''''''''''''''''
'Determine Output Row
SummaryRow = SummarySheet.Cells(Rows.Count, 1).End(xlUp).Row + 1
''''''MY PROBLEM STARTS HERE'''''''
For Each Cell In CategoryRange
If Cell.Value = 8840 Then
For Each Celltwo In SummarySheet.Cells(, Cell.Column).EntireColumn
i = MsgBox(Celltwo.Row, vbOKOnly)
If Celltwo.Row >= 10 Then
If Celltwo.Value > 0 Then
o = MsgBox(Celltwo.AddressLocal)
SummaryRow = SummaryRow + 1
Else
End If
Else
End If
Next Celltwo '''''DOES NOT LOOP'''''
Else
End If
Next Cell
I expect that when the code finds cell.value = 8840 it will then loop through each cell in that cell's column. Instead, it only loops through the first cell in that column and exits the second for-each loop
excel vba for-loop if-statement
In Excel VBA:
My second For-Each loop is only going through the first cell in a given column.
I need help getting it to go through each cell in the entire column.
In my code included here, I want to loop through each cell in a row (no issue). then once a cell in the row matches my criteria (no issue), I want to loop through each cell (celltwo) in that cell's column (issue).
I'm able to loop through the row and identify my criteria, but then the second for-each loop only considers the first celltwo in the given column. So I never get a celltwo with .row >=10, the first cell in each column has row=1.
Any help is appreciated.
This is for VBA in Excel. I've tried different ways of identifying my second range to loop through but nothing has allowed the second for-each loop to cycle back from "Next Celltwo" to the beginning of the loop.
Sub WriteSummary()
Dim UploadRange As Range
Dim SummaryRow As Integer
Dim CategoryRange As Range
Dim Cell As Range
Dim Celltwo As Range
''''''''''''''''''''''''''''''''''''''''''''''''''''
'Set Variables
Set MacroFile = ThisWorkbook
Set MacroSheet = ThisWorkbook.Worksheets("Macro")
Set UploadDash = ThisWorkbook.Worksheets("Upload Dash")
Set SummarySheet = ThisWorkbook.Worksheets("Summary")
Set IndexSheet = ThisWorkbook.Worksheets("Indexes")
Set CategoryRange = UploadDash.Range("5:5")
''''''''''''''''''''''''''''''''''''''''''''''''''''
'Determine Output Row
SummaryRow = SummarySheet.Cells(Rows.Count, 1).End(xlUp).Row + 1
''''''MY PROBLEM STARTS HERE'''''''
For Each Cell In CategoryRange
If Cell.Value = 8840 Then
For Each Celltwo In SummarySheet.Cells(, Cell.Column).EntireColumn
i = MsgBox(Celltwo.Row, vbOKOnly)
If Celltwo.Row >= 10 Then
If Celltwo.Value > 0 Then
o = MsgBox(Celltwo.AddressLocal)
SummaryRow = SummaryRow + 1
Else
End If
Else
End If
Next Celltwo '''''DOES NOT LOOP'''''
Else
End If
Next Cell
I expect that when the code finds cell.value = 8840 it will then loop through each cell in that cell's column. Instead, it only loops through the first cell in that column and exits the second for-each loop
excel vba for-loop if-statement
excel vba for-loop if-statement
asked Jan 3 at 16:52
JustinJustin
82
82
Option Explicit. Always.
– AJD
Jan 3 at 21:33
add a comment |
Option Explicit. Always.
– AJD
Jan 3 at 21:33
Option Explicit. Always.– AJD
Jan 3 at 21:33
Option Explicit. Always.– AJD
Jan 3 at 21:33
add a comment |
1 Answer
1
active
oldest
votes
Cells needs a row and column argument, but I think it's the EntireColumn which is taking the whole column as a single range.
Try something like this instead, which will restrict to cells containing something.
It starts at row 6 so amend to suit.
With SummarySheet
For Each Celltwo In .Range(.Cells(6, Cell.Column), .Cells(.Rows.Count, Cell.Column).end(xlup))
' etc
Next
End With
1
Thanks SJR, referencing the range in this manner got me going. I also had a serious reference issue - "CellTwo" is in the "UploadDash" worksheet object not the "SummarySheet" worksheet object - New Year, same me
– Justin
Jan 3 at 18:19
1
Forgot dot beforeRows.Count😉
– JohnyL
Jan 3 at 20:08
add a comment |
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%2f54026534%2fmy-nested-for-each-loop-isnt-going-through-each-cell-in-a-given-range%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
Cells needs a row and column argument, but I think it's the EntireColumn which is taking the whole column as a single range.
Try something like this instead, which will restrict to cells containing something.
It starts at row 6 so amend to suit.
With SummarySheet
For Each Celltwo In .Range(.Cells(6, Cell.Column), .Cells(.Rows.Count, Cell.Column).end(xlup))
' etc
Next
End With
1
Thanks SJR, referencing the range in this manner got me going. I also had a serious reference issue - "CellTwo" is in the "UploadDash" worksheet object not the "SummarySheet" worksheet object - New Year, same me
– Justin
Jan 3 at 18:19
1
Forgot dot beforeRows.Count😉
– JohnyL
Jan 3 at 20:08
add a comment |
Cells needs a row and column argument, but I think it's the EntireColumn which is taking the whole column as a single range.
Try something like this instead, which will restrict to cells containing something.
It starts at row 6 so amend to suit.
With SummarySheet
For Each Celltwo In .Range(.Cells(6, Cell.Column), .Cells(.Rows.Count, Cell.Column).end(xlup))
' etc
Next
End With
1
Thanks SJR, referencing the range in this manner got me going. I also had a serious reference issue - "CellTwo" is in the "UploadDash" worksheet object not the "SummarySheet" worksheet object - New Year, same me
– Justin
Jan 3 at 18:19
1
Forgot dot beforeRows.Count😉
– JohnyL
Jan 3 at 20:08
add a comment |
Cells needs a row and column argument, but I think it's the EntireColumn which is taking the whole column as a single range.
Try something like this instead, which will restrict to cells containing something.
It starts at row 6 so amend to suit.
With SummarySheet
For Each Celltwo In .Range(.Cells(6, Cell.Column), .Cells(.Rows.Count, Cell.Column).end(xlup))
' etc
Next
End With
Cells needs a row and column argument, but I think it's the EntireColumn which is taking the whole column as a single range.
Try something like this instead, which will restrict to cells containing something.
It starts at row 6 so amend to suit.
With SummarySheet
For Each Celltwo In .Range(.Cells(6, Cell.Column), .Cells(.Rows.Count, Cell.Column).end(xlup))
' etc
Next
End With
edited Jan 3 at 20:37
answered Jan 3 at 16:57
SJRSJR
13.4k31219
13.4k31219
1
Thanks SJR, referencing the range in this manner got me going. I also had a serious reference issue - "CellTwo" is in the "UploadDash" worksheet object not the "SummarySheet" worksheet object - New Year, same me
– Justin
Jan 3 at 18:19
1
Forgot dot beforeRows.Count😉
– JohnyL
Jan 3 at 20:08
add a comment |
1
Thanks SJR, referencing the range in this manner got me going. I also had a serious reference issue - "CellTwo" is in the "UploadDash" worksheet object not the "SummarySheet" worksheet object - New Year, same me
– Justin
Jan 3 at 18:19
1
Forgot dot beforeRows.Count😉
– JohnyL
Jan 3 at 20:08
1
1
Thanks SJR, referencing the range in this manner got me going. I also had a serious reference issue - "CellTwo" is in the "UploadDash" worksheet object not the "SummarySheet" worksheet object - New Year, same me
– Justin
Jan 3 at 18:19
Thanks SJR, referencing the range in this manner got me going. I also had a serious reference issue - "CellTwo" is in the "UploadDash" worksheet object not the "SummarySheet" worksheet object - New Year, same me
– Justin
Jan 3 at 18:19
1
1
Forgot dot before
Rows.Count 😉– JohnyL
Jan 3 at 20:08
Forgot dot before
Rows.Count 😉– JohnyL
Jan 3 at 20:08
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%2f54026534%2fmy-nested-for-each-loop-isnt-going-through-each-cell-in-a-given-range%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

Option Explicit. Always.– AJD
Jan 3 at 21:33