VBA For loop only loops through the last sheet where the call function is coded











up vote
-1
down vote

favorite












Hi i am am trying to add formulas to specific ranges on each worksheet in my workbook, however it only works on the last sheet in the workbook rather all of the worksheets in the workbook



Sourcesheet is the sheet from which the data for individual schools are copied to a new worksheet



Sub Formatting()
Dim sourceSheet As Worksheet
Dim lrow As Long

Set sourceSheet = ThisWorkbook.Worksheets(Sheets.Count)

With sourceSheet
lrow = .Cells(rcell, Col_Western).End(xlUp).Row
End With


For rcell = 1 To lrow
CharacterIndex = InStr(1, sourceSheet.Cells(rcell, Col_Western), "Delivery for Creative", vbBinaryCompare)
If CharacterIndex > 0 Then
deliveryname = "CS"

With ThisWorkbook.Worksheets.add
.Name = deliveryname
sourceSheet.Range(sourceSheet.Cells(rcell, Col_Western), sourceSheet.Cells(lastrow, Col_phone).End(xlDown)).Copy .Range("A1")
Cells.Select
Selection.RowHeight = 50
Selection.ColumnWidth = 30
'Add Autofilter to Row above student details
Range("a8:e8").EntireRow.AutoFilter
End With
End If

Next rcell



For Each Grey_ws In ThisWorkbook.Worksheets
Call Grey_VALUE_AND_RANGE_ALL(Grey_ws)
Next Grey_ws

End Sub

Sub Grey_VALUE_AND_RANGE_ALL(Grey_ws As Worksheet)
With Grey_ws
.Range("A5").FormulaR1C1 = "=Count_items_SmallWest()"
.Range("A6").FormulaR1C1 = "=Count_items_LargeWest()"
.Range("B5").FormulaR1C1 = "=Count_items_Small_Asian()"
.Range("B6").FormulaR1C1 = "=Count_items_Large_Asian()"
.Range("C5").FormulaR1C1 = "=Count_items_Small_Veg()"
.Range("C6").FormulaR1C1 = "=Count_items_Large_Veg()"
.Range("D5:D6").FormulaR1C1 = "=Count_items_Salad()"
.Range("E5:E6").FormulaR1C1 = "=Count_items_Dessert()"
.Range("F5:F6").FormulaR1C1 = "=Count_items_Snack()"
End With
End Sub









share|improve this question
























  • Where is sourceSheet defined? And I am confused as to your use of On Error Resume Next. Errors don't pop up just to annoy you, they explain serious flaws in your code. So remove that line and edit your question with your errors.
    – K.Dᴀᴠɪs
    Nov 14 at 5:43












  • I think you will need to show the code for: Grey_VALUE_AND_RANGE_ALL and Add_Complimentary. The main thing you'd need to do inside those procedures is ensure that you're adding formulas to the worksheet reference that you've passed as an argument (and not some hardcoded or implicitly referenced worksheet). Also, putting Option Explicit before your code may benefit you.
    – chillin
    Nov 14 at 5:46










  • i added code for Grey_VALUE_AND_RANGE_ALL
    – nikhilsharma
    Nov 14 at 6:06










  • k.Davis , in the event that sourcesheet does not contain a certain phrase it would continue the code instead of stopping
    – nikhilsharma
    Nov 14 at 6:09










  • I concur with @K.Dᴀᴠɪs & chillin. 1st concentrate on sourceSheet. Better define it by name instead of hard coding.
    – Ahmed AU
    Nov 14 at 6:32















up vote
-1
down vote

favorite












Hi i am am trying to add formulas to specific ranges on each worksheet in my workbook, however it only works on the last sheet in the workbook rather all of the worksheets in the workbook



Sourcesheet is the sheet from which the data for individual schools are copied to a new worksheet



Sub Formatting()
Dim sourceSheet As Worksheet
Dim lrow As Long

Set sourceSheet = ThisWorkbook.Worksheets(Sheets.Count)

With sourceSheet
lrow = .Cells(rcell, Col_Western).End(xlUp).Row
End With


For rcell = 1 To lrow
CharacterIndex = InStr(1, sourceSheet.Cells(rcell, Col_Western), "Delivery for Creative", vbBinaryCompare)
If CharacterIndex > 0 Then
deliveryname = "CS"

With ThisWorkbook.Worksheets.add
.Name = deliveryname
sourceSheet.Range(sourceSheet.Cells(rcell, Col_Western), sourceSheet.Cells(lastrow, Col_phone).End(xlDown)).Copy .Range("A1")
Cells.Select
Selection.RowHeight = 50
Selection.ColumnWidth = 30
'Add Autofilter to Row above student details
Range("a8:e8").EntireRow.AutoFilter
End With
End If

Next rcell



For Each Grey_ws In ThisWorkbook.Worksheets
Call Grey_VALUE_AND_RANGE_ALL(Grey_ws)
Next Grey_ws

End Sub

Sub Grey_VALUE_AND_RANGE_ALL(Grey_ws As Worksheet)
With Grey_ws
.Range("A5").FormulaR1C1 = "=Count_items_SmallWest()"
.Range("A6").FormulaR1C1 = "=Count_items_LargeWest()"
.Range("B5").FormulaR1C1 = "=Count_items_Small_Asian()"
.Range("B6").FormulaR1C1 = "=Count_items_Large_Asian()"
.Range("C5").FormulaR1C1 = "=Count_items_Small_Veg()"
.Range("C6").FormulaR1C1 = "=Count_items_Large_Veg()"
.Range("D5:D6").FormulaR1C1 = "=Count_items_Salad()"
.Range("E5:E6").FormulaR1C1 = "=Count_items_Dessert()"
.Range("F5:F6").FormulaR1C1 = "=Count_items_Snack()"
End With
End Sub









share|improve this question
























  • Where is sourceSheet defined? And I am confused as to your use of On Error Resume Next. Errors don't pop up just to annoy you, they explain serious flaws in your code. So remove that line and edit your question with your errors.
    – K.Dᴀᴠɪs
    Nov 14 at 5:43












  • I think you will need to show the code for: Grey_VALUE_AND_RANGE_ALL and Add_Complimentary. The main thing you'd need to do inside those procedures is ensure that you're adding formulas to the worksheet reference that you've passed as an argument (and not some hardcoded or implicitly referenced worksheet). Also, putting Option Explicit before your code may benefit you.
    – chillin
    Nov 14 at 5:46










  • i added code for Grey_VALUE_AND_RANGE_ALL
    – nikhilsharma
    Nov 14 at 6:06










  • k.Davis , in the event that sourcesheet does not contain a certain phrase it would continue the code instead of stopping
    – nikhilsharma
    Nov 14 at 6:09










  • I concur with @K.Dᴀᴠɪs & chillin. 1st concentrate on sourceSheet. Better define it by name instead of hard coding.
    – Ahmed AU
    Nov 14 at 6:32













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











Hi i am am trying to add formulas to specific ranges on each worksheet in my workbook, however it only works on the last sheet in the workbook rather all of the worksheets in the workbook



Sourcesheet is the sheet from which the data for individual schools are copied to a new worksheet



Sub Formatting()
Dim sourceSheet As Worksheet
Dim lrow As Long

Set sourceSheet = ThisWorkbook.Worksheets(Sheets.Count)

With sourceSheet
lrow = .Cells(rcell, Col_Western).End(xlUp).Row
End With


For rcell = 1 To lrow
CharacterIndex = InStr(1, sourceSheet.Cells(rcell, Col_Western), "Delivery for Creative", vbBinaryCompare)
If CharacterIndex > 0 Then
deliveryname = "CS"

With ThisWorkbook.Worksheets.add
.Name = deliveryname
sourceSheet.Range(sourceSheet.Cells(rcell, Col_Western), sourceSheet.Cells(lastrow, Col_phone).End(xlDown)).Copy .Range("A1")
Cells.Select
Selection.RowHeight = 50
Selection.ColumnWidth = 30
'Add Autofilter to Row above student details
Range("a8:e8").EntireRow.AutoFilter
End With
End If

Next rcell



For Each Grey_ws In ThisWorkbook.Worksheets
Call Grey_VALUE_AND_RANGE_ALL(Grey_ws)
Next Grey_ws

End Sub

Sub Grey_VALUE_AND_RANGE_ALL(Grey_ws As Worksheet)
With Grey_ws
.Range("A5").FormulaR1C1 = "=Count_items_SmallWest()"
.Range("A6").FormulaR1C1 = "=Count_items_LargeWest()"
.Range("B5").FormulaR1C1 = "=Count_items_Small_Asian()"
.Range("B6").FormulaR1C1 = "=Count_items_Large_Asian()"
.Range("C5").FormulaR1C1 = "=Count_items_Small_Veg()"
.Range("C6").FormulaR1C1 = "=Count_items_Large_Veg()"
.Range("D5:D6").FormulaR1C1 = "=Count_items_Salad()"
.Range("E5:E6").FormulaR1C1 = "=Count_items_Dessert()"
.Range("F5:F6").FormulaR1C1 = "=Count_items_Snack()"
End With
End Sub









share|improve this question















Hi i am am trying to add formulas to specific ranges on each worksheet in my workbook, however it only works on the last sheet in the workbook rather all of the worksheets in the workbook



Sourcesheet is the sheet from which the data for individual schools are copied to a new worksheet



Sub Formatting()
Dim sourceSheet As Worksheet
Dim lrow As Long

Set sourceSheet = ThisWorkbook.Worksheets(Sheets.Count)

With sourceSheet
lrow = .Cells(rcell, Col_Western).End(xlUp).Row
End With


For rcell = 1 To lrow
CharacterIndex = InStr(1, sourceSheet.Cells(rcell, Col_Western), "Delivery for Creative", vbBinaryCompare)
If CharacterIndex > 0 Then
deliveryname = "CS"

With ThisWorkbook.Worksheets.add
.Name = deliveryname
sourceSheet.Range(sourceSheet.Cells(rcell, Col_Western), sourceSheet.Cells(lastrow, Col_phone).End(xlDown)).Copy .Range("A1")
Cells.Select
Selection.RowHeight = 50
Selection.ColumnWidth = 30
'Add Autofilter to Row above student details
Range("a8:e8").EntireRow.AutoFilter
End With
End If

Next rcell



For Each Grey_ws In ThisWorkbook.Worksheets
Call Grey_VALUE_AND_RANGE_ALL(Grey_ws)
Next Grey_ws

End Sub

Sub Grey_VALUE_AND_RANGE_ALL(Grey_ws As Worksheet)
With Grey_ws
.Range("A5").FormulaR1C1 = "=Count_items_SmallWest()"
.Range("A6").FormulaR1C1 = "=Count_items_LargeWest()"
.Range("B5").FormulaR1C1 = "=Count_items_Small_Asian()"
.Range("B6").FormulaR1C1 = "=Count_items_Large_Asian()"
.Range("C5").FormulaR1C1 = "=Count_items_Small_Veg()"
.Range("C6").FormulaR1C1 = "=Count_items_Large_Veg()"
.Range("D5:D6").FormulaR1C1 = "=Count_items_Salad()"
.Range("E5:E6").FormulaR1C1 = "=Count_items_Dessert()"
.Range("F5:F6").FormulaR1C1 = "=Count_items_Snack()"
End With
End Sub






excel vba excel-vba






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 at 1:29

























asked Nov 14 at 5:35









nikhilsharma

65




65












  • Where is sourceSheet defined? And I am confused as to your use of On Error Resume Next. Errors don't pop up just to annoy you, they explain serious flaws in your code. So remove that line and edit your question with your errors.
    – K.Dᴀᴠɪs
    Nov 14 at 5:43












  • I think you will need to show the code for: Grey_VALUE_AND_RANGE_ALL and Add_Complimentary. The main thing you'd need to do inside those procedures is ensure that you're adding formulas to the worksheet reference that you've passed as an argument (and not some hardcoded or implicitly referenced worksheet). Also, putting Option Explicit before your code may benefit you.
    – chillin
    Nov 14 at 5:46










  • i added code for Grey_VALUE_AND_RANGE_ALL
    – nikhilsharma
    Nov 14 at 6:06










  • k.Davis , in the event that sourcesheet does not contain a certain phrase it would continue the code instead of stopping
    – nikhilsharma
    Nov 14 at 6:09










  • I concur with @K.Dᴀᴠɪs & chillin. 1st concentrate on sourceSheet. Better define it by name instead of hard coding.
    – Ahmed AU
    Nov 14 at 6:32


















  • Where is sourceSheet defined? And I am confused as to your use of On Error Resume Next. Errors don't pop up just to annoy you, they explain serious flaws in your code. So remove that line and edit your question with your errors.
    – K.Dᴀᴠɪs
    Nov 14 at 5:43












  • I think you will need to show the code for: Grey_VALUE_AND_RANGE_ALL and Add_Complimentary. The main thing you'd need to do inside those procedures is ensure that you're adding formulas to the worksheet reference that you've passed as an argument (and not some hardcoded or implicitly referenced worksheet). Also, putting Option Explicit before your code may benefit you.
    – chillin
    Nov 14 at 5:46










  • i added code for Grey_VALUE_AND_RANGE_ALL
    – nikhilsharma
    Nov 14 at 6:06










  • k.Davis , in the event that sourcesheet does not contain a certain phrase it would continue the code instead of stopping
    – nikhilsharma
    Nov 14 at 6:09










  • I concur with @K.Dᴀᴠɪs & chillin. 1st concentrate on sourceSheet. Better define it by name instead of hard coding.
    – Ahmed AU
    Nov 14 at 6:32
















Where is sourceSheet defined? And I am confused as to your use of On Error Resume Next. Errors don't pop up just to annoy you, they explain serious flaws in your code. So remove that line and edit your question with your errors.
– K.Dᴀᴠɪs
Nov 14 at 5:43






Where is sourceSheet defined? And I am confused as to your use of On Error Resume Next. Errors don't pop up just to annoy you, they explain serious flaws in your code. So remove that line and edit your question with your errors.
– K.Dᴀᴠɪs
Nov 14 at 5:43














I think you will need to show the code for: Grey_VALUE_AND_RANGE_ALL and Add_Complimentary. The main thing you'd need to do inside those procedures is ensure that you're adding formulas to the worksheet reference that you've passed as an argument (and not some hardcoded or implicitly referenced worksheet). Also, putting Option Explicit before your code may benefit you.
– chillin
Nov 14 at 5:46




I think you will need to show the code for: Grey_VALUE_AND_RANGE_ALL and Add_Complimentary. The main thing you'd need to do inside those procedures is ensure that you're adding formulas to the worksheet reference that you've passed as an argument (and not some hardcoded or implicitly referenced worksheet). Also, putting Option Explicit before your code may benefit you.
– chillin
Nov 14 at 5:46












i added code for Grey_VALUE_AND_RANGE_ALL
– nikhilsharma
Nov 14 at 6:06




i added code for Grey_VALUE_AND_RANGE_ALL
– nikhilsharma
Nov 14 at 6:06












k.Davis , in the event that sourcesheet does not contain a certain phrase it would continue the code instead of stopping
– nikhilsharma
Nov 14 at 6:09




k.Davis , in the event that sourcesheet does not contain a certain phrase it would continue the code instead of stopping
– nikhilsharma
Nov 14 at 6:09












I concur with @K.Dᴀᴠɪs & chillin. 1st concentrate on sourceSheet. Better define it by name instead of hard coding.
– Ahmed AU
Nov 14 at 6:32




I concur with @K.Dᴀᴠɪs & chillin. 1st concentrate on sourceSheet. Better define it by name instead of hard coding.
– Ahmed AU
Nov 14 at 6:32

















active

oldest

votes











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293780%2fvba-for-loop-only-loops-through-the-last-sheet-where-the-call-function-is-coded%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293780%2fvba-for-loop-only-loops-through-the-last-sheet-where-the-call-function-is-coded%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?