Loop over all worksheets in a workbook and add formulas










0














Hi I am trying to add formulas to specific ranges on each worksheet in my workbook, however when i run it does not work properly.



The formulas are functions that help count the number of cells that meet a certain criteria.



 Sub Formatting()
For rcell = 1 To lrow
CharacterIndex = InStr(1, sourceSheet.Cells(rcell, Col_Western), "Delivery for Creative", vbBinaryCompare)
If CharacterIndex > 0 Then
On Error Resume Next
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)
'do nothing else
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























  • Can you explain what is not working properly?
    – BigBen
    Nov 13 '18 at 2:08










  • I can only assume that your Count_items... functions expect a range to work wiht like "=Count_items_SmallWest(R1C:R4C)"
    – Display name
    Nov 13 '18 at 2:17











  • So the cells of the range mentioned in the code,should have the formula inserted once it runs. However, the cells remain unchanged after running the code
    – nikhilsharma
    Nov 13 '18 at 2:18






  • 1




    After copy pasting your code as is, it is working as expected (e.g. inserting the formula) in all sheets of the activeworkbook.
    – Display name
    Nov 13 '18 at 2:21










  • i tried it, oddly it only works on one worksheets, while the other remains unchanged
    – nikhilsharma
    Nov 13 '18 at 2:40















0














Hi I am trying to add formulas to specific ranges on each worksheet in my workbook, however when i run it does not work properly.



The formulas are functions that help count the number of cells that meet a certain criteria.



 Sub Formatting()
For rcell = 1 To lrow
CharacterIndex = InStr(1, sourceSheet.Cells(rcell, Col_Western), "Delivery for Creative", vbBinaryCompare)
If CharacterIndex > 0 Then
On Error Resume Next
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)
'do nothing else
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























  • Can you explain what is not working properly?
    – BigBen
    Nov 13 '18 at 2:08










  • I can only assume that your Count_items... functions expect a range to work wiht like "=Count_items_SmallWest(R1C:R4C)"
    – Display name
    Nov 13 '18 at 2:17











  • So the cells of the range mentioned in the code,should have the formula inserted once it runs. However, the cells remain unchanged after running the code
    – nikhilsharma
    Nov 13 '18 at 2:18






  • 1




    After copy pasting your code as is, it is working as expected (e.g. inserting the formula) in all sheets of the activeworkbook.
    – Display name
    Nov 13 '18 at 2:21










  • i tried it, oddly it only works on one worksheets, while the other remains unchanged
    – nikhilsharma
    Nov 13 '18 at 2:40













0












0








0







Hi I am trying to add formulas to specific ranges on each worksheet in my workbook, however when i run it does not work properly.



The formulas are functions that help count the number of cells that meet a certain criteria.



 Sub Formatting()
For rcell = 1 To lrow
CharacterIndex = InStr(1, sourceSheet.Cells(rcell, Col_Western), "Delivery for Creative", vbBinaryCompare)
If CharacterIndex > 0 Then
On Error Resume Next
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)
'do nothing else
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 trying to add formulas to specific ranges on each worksheet in my workbook, however when i run it does not work properly.



The formulas are functions that help count the number of cells that meet a certain criteria.



 Sub Formatting()
For rcell = 1 To lrow
CharacterIndex = InStr(1, sourceSheet.Cells(rcell, Col_Western), "Delivery for Creative", vbBinaryCompare)
If CharacterIndex > 0 Then
On Error Resume Next
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)
'do nothing else
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 14 '18 at 5:19







nikhilsharma

















asked Nov 13 '18 at 2:04









nikhilsharmanikhilsharma

85




85











  • Can you explain what is not working properly?
    – BigBen
    Nov 13 '18 at 2:08










  • I can only assume that your Count_items... functions expect a range to work wiht like "=Count_items_SmallWest(R1C:R4C)"
    – Display name
    Nov 13 '18 at 2:17











  • So the cells of the range mentioned in the code,should have the formula inserted once it runs. However, the cells remain unchanged after running the code
    – nikhilsharma
    Nov 13 '18 at 2:18






  • 1




    After copy pasting your code as is, it is working as expected (e.g. inserting the formula) in all sheets of the activeworkbook.
    – Display name
    Nov 13 '18 at 2:21










  • i tried it, oddly it only works on one worksheets, while the other remains unchanged
    – nikhilsharma
    Nov 13 '18 at 2:40
















  • Can you explain what is not working properly?
    – BigBen
    Nov 13 '18 at 2:08










  • I can only assume that your Count_items... functions expect a range to work wiht like "=Count_items_SmallWest(R1C:R4C)"
    – Display name
    Nov 13 '18 at 2:17











  • So the cells of the range mentioned in the code,should have the formula inserted once it runs. However, the cells remain unchanged after running the code
    – nikhilsharma
    Nov 13 '18 at 2:18






  • 1




    After copy pasting your code as is, it is working as expected (e.g. inserting the formula) in all sheets of the activeworkbook.
    – Display name
    Nov 13 '18 at 2:21










  • i tried it, oddly it only works on one worksheets, while the other remains unchanged
    – nikhilsharma
    Nov 13 '18 at 2:40















Can you explain what is not working properly?
– BigBen
Nov 13 '18 at 2:08




Can you explain what is not working properly?
– BigBen
Nov 13 '18 at 2:08












I can only assume that your Count_items... functions expect a range to work wiht like "=Count_items_SmallWest(R1C:R4C)"
– Display name
Nov 13 '18 at 2:17





I can only assume that your Count_items... functions expect a range to work wiht like "=Count_items_SmallWest(R1C:R4C)"
– Display name
Nov 13 '18 at 2:17













So the cells of the range mentioned in the code,should have the formula inserted once it runs. However, the cells remain unchanged after running the code
– nikhilsharma
Nov 13 '18 at 2:18




So the cells of the range mentioned in the code,should have the formula inserted once it runs. However, the cells remain unchanged after running the code
– nikhilsharma
Nov 13 '18 at 2:18




1




1




After copy pasting your code as is, it is working as expected (e.g. inserting the formula) in all sheets of the activeworkbook.
– Display name
Nov 13 '18 at 2:21




After copy pasting your code as is, it is working as expected (e.g. inserting the formula) in all sheets of the activeworkbook.
– Display name
Nov 13 '18 at 2:21












i tried it, oddly it only works on one worksheets, while the other remains unchanged
– nikhilsharma
Nov 13 '18 at 2:40




i tried it, oddly it only works on one worksheets, while the other remains unchanged
– nikhilsharma
Nov 13 '18 at 2:40












1 Answer
1






active

oldest

votes


















0














I suspect the user-defined functions used by your formulas are invoked as your code executes, perturbating the loop; it is also possible that the Worksheet_Change event handler, if it exists, interferes with your loop.



In your Formatting sub, sandwich the For loop between Application.Calculation and Application.EnableEvents as follows:



Sub Formatting()
Dim Grey_ws as Worksheet

Application.Calculation = xlCalculationManual
Application.EnableEvents = False

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

Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
End Sub


This will let Excel know it shouldn't calculate any formula results, nor emit any events, while the loop executes.



To stay on the safe side, make sure to re-establish calculations and events in case of error:



Sub Formatting()
Dim Grey_ws As Worksheet
Dim lInitialCalculation As XlCalculation
Dim bInitialEnableEvents As Boolean

On Error GoTo ErrorHandler

lInitialCalculation = Application.Calculation
Application.Calculation = xlCalculationManual

bInitialEnableEvents = Application.EnableEvents
Application.EnableEvents = False

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

Cleanup:
On Error Resume Next '<== important to prevent an infinite error loop, should the cleanup code fail.
If Application.EnableEvents <> bInitialEnableEvents Then
Application.EnableEvents = bInitialEnableEvents
End If
If Application.Calculation <> lInitialCalculation Then
Application.Calculation = lInitialCalculation '<== back to the initial value.
End If

Exit Sub

ErrorHandler:
MsgBox Err.Description, vbExclamation + vbOKOnly '<== or whatever you see fit.
GoTo Cleanup
End Sub





share|improve this answer






















  • thanks for your help. I tried running my code it does work, but it only works on one worksheet. Rather than all of the worksheets in the workbook
    – nikhilsharma
    Nov 13 '18 at 6:39











  • Sure, you tried your code; but have you tried mine? Note that I've modified my answer to add management of the EnableEvents property.
    – Excelosaurus
    Nov 13 '18 at 13:34










  • I gave it a try, but it doesnt work. I forgot to mention that i add new worksheets in the file and then after all worksheets have been added then i want it to call sub function
    – nikhilsharma
    Nov 14 '18 at 4:20











  • In Grey_VALUE_AND_RANGE_ALL, replace your formulas by "=1+1". Does it work?
    – Excelosaurus
    Nov 14 '18 at 16:10










  • Excelosaurus, thanks, it works
    – nikhilsharma
    Nov 15 '18 at 1:45










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



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272718%2floop-over-all-worksheets-in-a-workbook-and-add-formulas%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









0














I suspect the user-defined functions used by your formulas are invoked as your code executes, perturbating the loop; it is also possible that the Worksheet_Change event handler, if it exists, interferes with your loop.



In your Formatting sub, sandwich the For loop between Application.Calculation and Application.EnableEvents as follows:



Sub Formatting()
Dim Grey_ws as Worksheet

Application.Calculation = xlCalculationManual
Application.EnableEvents = False

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

Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
End Sub


This will let Excel know it shouldn't calculate any formula results, nor emit any events, while the loop executes.



To stay on the safe side, make sure to re-establish calculations and events in case of error:



Sub Formatting()
Dim Grey_ws As Worksheet
Dim lInitialCalculation As XlCalculation
Dim bInitialEnableEvents As Boolean

On Error GoTo ErrorHandler

lInitialCalculation = Application.Calculation
Application.Calculation = xlCalculationManual

bInitialEnableEvents = Application.EnableEvents
Application.EnableEvents = False

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

Cleanup:
On Error Resume Next '<== important to prevent an infinite error loop, should the cleanup code fail.
If Application.EnableEvents <> bInitialEnableEvents Then
Application.EnableEvents = bInitialEnableEvents
End If
If Application.Calculation <> lInitialCalculation Then
Application.Calculation = lInitialCalculation '<== back to the initial value.
End If

Exit Sub

ErrorHandler:
MsgBox Err.Description, vbExclamation + vbOKOnly '<== or whatever you see fit.
GoTo Cleanup
End Sub





share|improve this answer






















  • thanks for your help. I tried running my code it does work, but it only works on one worksheet. Rather than all of the worksheets in the workbook
    – nikhilsharma
    Nov 13 '18 at 6:39











  • Sure, you tried your code; but have you tried mine? Note that I've modified my answer to add management of the EnableEvents property.
    – Excelosaurus
    Nov 13 '18 at 13:34










  • I gave it a try, but it doesnt work. I forgot to mention that i add new worksheets in the file and then after all worksheets have been added then i want it to call sub function
    – nikhilsharma
    Nov 14 '18 at 4:20











  • In Grey_VALUE_AND_RANGE_ALL, replace your formulas by "=1+1". Does it work?
    – Excelosaurus
    Nov 14 '18 at 16:10










  • Excelosaurus, thanks, it works
    – nikhilsharma
    Nov 15 '18 at 1:45















0














I suspect the user-defined functions used by your formulas are invoked as your code executes, perturbating the loop; it is also possible that the Worksheet_Change event handler, if it exists, interferes with your loop.



In your Formatting sub, sandwich the For loop between Application.Calculation and Application.EnableEvents as follows:



Sub Formatting()
Dim Grey_ws as Worksheet

Application.Calculation = xlCalculationManual
Application.EnableEvents = False

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

Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
End Sub


This will let Excel know it shouldn't calculate any formula results, nor emit any events, while the loop executes.



To stay on the safe side, make sure to re-establish calculations and events in case of error:



Sub Formatting()
Dim Grey_ws As Worksheet
Dim lInitialCalculation As XlCalculation
Dim bInitialEnableEvents As Boolean

On Error GoTo ErrorHandler

lInitialCalculation = Application.Calculation
Application.Calculation = xlCalculationManual

bInitialEnableEvents = Application.EnableEvents
Application.EnableEvents = False

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

Cleanup:
On Error Resume Next '<== important to prevent an infinite error loop, should the cleanup code fail.
If Application.EnableEvents <> bInitialEnableEvents Then
Application.EnableEvents = bInitialEnableEvents
End If
If Application.Calculation <> lInitialCalculation Then
Application.Calculation = lInitialCalculation '<== back to the initial value.
End If

Exit Sub

ErrorHandler:
MsgBox Err.Description, vbExclamation + vbOKOnly '<== or whatever you see fit.
GoTo Cleanup
End Sub





share|improve this answer






















  • thanks for your help. I tried running my code it does work, but it only works on one worksheet. Rather than all of the worksheets in the workbook
    – nikhilsharma
    Nov 13 '18 at 6:39











  • Sure, you tried your code; but have you tried mine? Note that I've modified my answer to add management of the EnableEvents property.
    – Excelosaurus
    Nov 13 '18 at 13:34










  • I gave it a try, but it doesnt work. I forgot to mention that i add new worksheets in the file and then after all worksheets have been added then i want it to call sub function
    – nikhilsharma
    Nov 14 '18 at 4:20











  • In Grey_VALUE_AND_RANGE_ALL, replace your formulas by "=1+1". Does it work?
    – Excelosaurus
    Nov 14 '18 at 16:10










  • Excelosaurus, thanks, it works
    – nikhilsharma
    Nov 15 '18 at 1:45













0












0








0






I suspect the user-defined functions used by your formulas are invoked as your code executes, perturbating the loop; it is also possible that the Worksheet_Change event handler, if it exists, interferes with your loop.



In your Formatting sub, sandwich the For loop between Application.Calculation and Application.EnableEvents as follows:



Sub Formatting()
Dim Grey_ws as Worksheet

Application.Calculation = xlCalculationManual
Application.EnableEvents = False

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

Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
End Sub


This will let Excel know it shouldn't calculate any formula results, nor emit any events, while the loop executes.



To stay on the safe side, make sure to re-establish calculations and events in case of error:



Sub Formatting()
Dim Grey_ws As Worksheet
Dim lInitialCalculation As XlCalculation
Dim bInitialEnableEvents As Boolean

On Error GoTo ErrorHandler

lInitialCalculation = Application.Calculation
Application.Calculation = xlCalculationManual

bInitialEnableEvents = Application.EnableEvents
Application.EnableEvents = False

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

Cleanup:
On Error Resume Next '<== important to prevent an infinite error loop, should the cleanup code fail.
If Application.EnableEvents <> bInitialEnableEvents Then
Application.EnableEvents = bInitialEnableEvents
End If
If Application.Calculation <> lInitialCalculation Then
Application.Calculation = lInitialCalculation '<== back to the initial value.
End If

Exit Sub

ErrorHandler:
MsgBox Err.Description, vbExclamation + vbOKOnly '<== or whatever you see fit.
GoTo Cleanup
End Sub





share|improve this answer














I suspect the user-defined functions used by your formulas are invoked as your code executes, perturbating the loop; it is also possible that the Worksheet_Change event handler, if it exists, interferes with your loop.



In your Formatting sub, sandwich the For loop between Application.Calculation and Application.EnableEvents as follows:



Sub Formatting()
Dim Grey_ws as Worksheet

Application.Calculation = xlCalculationManual
Application.EnableEvents = False

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

Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
End Sub


This will let Excel know it shouldn't calculate any formula results, nor emit any events, while the loop executes.



To stay on the safe side, make sure to re-establish calculations and events in case of error:



Sub Formatting()
Dim Grey_ws As Worksheet
Dim lInitialCalculation As XlCalculation
Dim bInitialEnableEvents As Boolean

On Error GoTo ErrorHandler

lInitialCalculation = Application.Calculation
Application.Calculation = xlCalculationManual

bInitialEnableEvents = Application.EnableEvents
Application.EnableEvents = False

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

Cleanup:
On Error Resume Next '<== important to prevent an infinite error loop, should the cleanup code fail.
If Application.EnableEvents <> bInitialEnableEvents Then
Application.EnableEvents = bInitialEnableEvents
End If
If Application.Calculation <> lInitialCalculation Then
Application.Calculation = lInitialCalculation '<== back to the initial value.
End If

Exit Sub

ErrorHandler:
MsgBox Err.Description, vbExclamation + vbOKOnly '<== or whatever you see fit.
GoTo Cleanup
End Sub






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 13:44

























answered Nov 13 '18 at 4:18









ExcelosaurusExcelosaurus

2,1171715




2,1171715











  • thanks for your help. I tried running my code it does work, but it only works on one worksheet. Rather than all of the worksheets in the workbook
    – nikhilsharma
    Nov 13 '18 at 6:39











  • Sure, you tried your code; but have you tried mine? Note that I've modified my answer to add management of the EnableEvents property.
    – Excelosaurus
    Nov 13 '18 at 13:34










  • I gave it a try, but it doesnt work. I forgot to mention that i add new worksheets in the file and then after all worksheets have been added then i want it to call sub function
    – nikhilsharma
    Nov 14 '18 at 4:20











  • In Grey_VALUE_AND_RANGE_ALL, replace your formulas by "=1+1". Does it work?
    – Excelosaurus
    Nov 14 '18 at 16:10










  • Excelosaurus, thanks, it works
    – nikhilsharma
    Nov 15 '18 at 1:45
















  • thanks for your help. I tried running my code it does work, but it only works on one worksheet. Rather than all of the worksheets in the workbook
    – nikhilsharma
    Nov 13 '18 at 6:39











  • Sure, you tried your code; but have you tried mine? Note that I've modified my answer to add management of the EnableEvents property.
    – Excelosaurus
    Nov 13 '18 at 13:34










  • I gave it a try, but it doesnt work. I forgot to mention that i add new worksheets in the file and then after all worksheets have been added then i want it to call sub function
    – nikhilsharma
    Nov 14 '18 at 4:20











  • In Grey_VALUE_AND_RANGE_ALL, replace your formulas by "=1+1". Does it work?
    – Excelosaurus
    Nov 14 '18 at 16:10










  • Excelosaurus, thanks, it works
    – nikhilsharma
    Nov 15 '18 at 1:45















thanks for your help. I tried running my code it does work, but it only works on one worksheet. Rather than all of the worksheets in the workbook
– nikhilsharma
Nov 13 '18 at 6:39





thanks for your help. I tried running my code it does work, but it only works on one worksheet. Rather than all of the worksheets in the workbook
– nikhilsharma
Nov 13 '18 at 6:39













Sure, you tried your code; but have you tried mine? Note that I've modified my answer to add management of the EnableEvents property.
– Excelosaurus
Nov 13 '18 at 13:34




Sure, you tried your code; but have you tried mine? Note that I've modified my answer to add management of the EnableEvents property.
– Excelosaurus
Nov 13 '18 at 13:34












I gave it a try, but it doesnt work. I forgot to mention that i add new worksheets in the file and then after all worksheets have been added then i want it to call sub function
– nikhilsharma
Nov 14 '18 at 4:20





I gave it a try, but it doesnt work. I forgot to mention that i add new worksheets in the file and then after all worksheets have been added then i want it to call sub function
– nikhilsharma
Nov 14 '18 at 4:20













In Grey_VALUE_AND_RANGE_ALL, replace your formulas by "=1+1". Does it work?
– Excelosaurus
Nov 14 '18 at 16:10




In Grey_VALUE_AND_RANGE_ALL, replace your formulas by "=1+1". Does it work?
– Excelosaurus
Nov 14 '18 at 16:10












Excelosaurus, thanks, it works
– nikhilsharma
Nov 15 '18 at 1:45




Excelosaurus, thanks, it works
– nikhilsharma
Nov 15 '18 at 1:45

















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%2f53272718%2floop-over-all-worksheets-in-a-workbook-and-add-formulas%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







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3