K
kevlarmcc
I have some simple code that selects a cell, enters a value, selects some
more cells and enters a formula. When i follow suggestions to configure the
code to run on all worksheets I cannot get it right. What it does is run the
code on the active sheet as many times as there are sheets. So it's looping
but not going on to the next sheet. Can someone tell me what I have wrong? I
have included the code that works (for the active sheet only) and the code i
think would work but won't.
Works:
Sub CalcDates()
ActiveSheet.Cells(2, 7).Select
Selection.Value = "Days"
ActiveSheet.Cells(3, 7).Select
Do While Not IsEmpty(ActiveCell.Offset(0, -1))
ActiveCell.FormulaR1C1 = "=DAYS360(RC[-4],RC[-6])"
ActiveCell.Offset(1, 0).Select
Loop
End Sub
Doesn't work:
Sub CalcDates()
For Each Worksheet In Worksheets
Cells(2, 7).Select
Selection.Value = "Days"
Cells(3, 7).Select
Do While Not IsEmpty(ActiveCell.Offset(0, -1))
ActiveCell.FormulaR1C1 = "=DAYS360(RC[-4],RC[-6])"
ActiveCell.Offset(1, 0).Select
Loop
Next Worksheet
End Sub
more cells and enters a formula. When i follow suggestions to configure the
code to run on all worksheets I cannot get it right. What it does is run the
code on the active sheet as many times as there are sheets. So it's looping
but not going on to the next sheet. Can someone tell me what I have wrong? I
have included the code that works (for the active sheet only) and the code i
think would work but won't.
Works:
Sub CalcDates()
ActiveSheet.Cells(2, 7).Select
Selection.Value = "Days"
ActiveSheet.Cells(3, 7).Select
Do While Not IsEmpty(ActiveCell.Offset(0, -1))
ActiveCell.FormulaR1C1 = "=DAYS360(RC[-4],RC[-6])"
ActiveCell.Offset(1, 0).Select
Loop
End Sub
Doesn't work:
Sub CalcDates()
For Each Worksheet In Worksheets
Cells(2, 7).Select
Selection.Value = "Days"
Cells(3, 7).Select
Do While Not IsEmpty(ActiveCell.Offset(0, -1))
ActiveCell.FormulaR1C1 = "=DAYS360(RC[-4],RC[-6])"
ActiveCell.Offset(1, 0).Select
Loop
Next Worksheet
End Sub