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
excel vba excel-vba
add a comment |
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
excel vba excel-vba
Where issourceSheet
defined? And I am confused as to your use ofOn 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
andAdd_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, puttingOption 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 onsourceSheet
. Better define it by name instead of hard coding.
– Ahmed AU
Nov 14 at 6:32
add a comment |
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
excel vba excel-vba
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
excel vba excel-vba
edited Nov 15 at 1:29
asked Nov 14 at 5:35
nikhilsharma
65
65
Where issourceSheet
defined? And I am confused as to your use ofOn 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
andAdd_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, puttingOption 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 onsourceSheet
. Better define it by name instead of hard coding.
– Ahmed AU
Nov 14 at 6:32
add a comment |
Where issourceSheet
defined? And I am confused as to your use ofOn 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
andAdd_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, puttingOption 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 onsourceSheet
. 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
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%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
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
Where is
sourceSheet
defined? And I am confused as to your use ofOn 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
andAdd_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, puttingOption 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