PC Review


Reply
Thread Tools Rate Thread

Configure macro to run on all sheets Excel 2k7

 
 
kevlarmcc
Guest
Posts: n/a
 
      15th Mar 2010
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
 
Reply With Quote
 
 
 
 
Gary Brown
Guest
Posts: n/a
 
      15th Mar 2010
I'd say you probably need to declare the worksheet....

Sub CalcDates()
Dim worksheet as worksheet
'stuff
End Sub

--
Hope this helps.
If it does, please click the Yes button.
Thanks in advance for your feedback.
Gary Brown



"kevlarmcc" wrote:

> 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

 
Reply With Quote
 
JLatham
Guest
Posts: n/a
 
      15th Mar 2010
That will work, but (in my opinion) bad form to try to use reserved words as
variables or constants. But I think I know why you did it in this case.

Probably better for OP to do it this way:

Sub CalcDates()
Dim anySheet as Worksheet
For Each anySheet in ThisWorkbook.Worksheets
'.... the other code here
Next ' end of anySheet loop
End Sub

"Gary Brown" wrote:

> I'd say you probably need to declare the worksheet....
>
> Sub CalcDates()
> Dim worksheet as worksheet
> 'stuff
> End Sub
>
> --
> Hope this helps.
> If it does, please click the Yes button.
> Thanks in advance for your feedback.
> Gary Brown
>
>
>
> "kevlarmcc" wrote:
>
> > 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

 
Reply With Quote
 
Gary Brown
Guest
Posts: n/a
 
      15th Mar 2010
I totally agree but was working with what he had.
Thanks,
Gary

-



"JLatham" wrote:

> That will work, but (in my opinion) bad form to try to use reserved words as
> variables or constants. But I think I know why you did it in this case.
>
> Probably better for OP to do it this way:
>
> Sub CalcDates()
> Dim anySheet as Worksheet
> For Each anySheet in ThisWorkbook.Worksheets
> '.... the other code here
> Next ' end of anySheet loop
> End Sub
>
> "Gary Brown" wrote:
>
> > I'd say you probably need to declare the worksheet....
> >
> > Sub CalcDates()
> > Dim worksheet as worksheet
> > 'stuff
> > End Sub
> >
> > --
> > Hope this helps.
> > If it does, please click the Yes button.
> > Thanks in advance for your feedback.
> > Gary Brown
> >
> >
> >
> > "kevlarmcc" wrote:
> >
> > > 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

 
Reply With Quote
 
kevlarmcc
Guest
Posts: n/a
 
      15th Mar 2010
With both solutions I still have the same result. The macro never moves on to
the next sheet.

"JLatham" wrote:

> That will work, but (in my opinion) bad form to try to use reserved words as
> variables or constants. But I think I know why you did it in this case.
>
> Probably better for OP to do it this way:
>
> Sub CalcDates()
> Dim anySheet as Worksheet
> For Each anySheet in ThisWorkbook.Worksheets
> '.... the other code here
> Next ' end of anySheet loop
> End Sub
>
> "Gary Brown" wrote:
>
> > I'd say you probably need to declare the worksheet....
> >
> > Sub CalcDates()
> > Dim worksheet as worksheet
> > 'stuff
> > End Sub
> >
> > --
> > Hope this helps.
> > If it does, please click the Yes button.
> > Thanks in advance for your feedback.
> > Gary Brown
> >
> >
> >
> > "kevlarmcc" wrote:
> >
> > > 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

 
Reply With Quote
 
Gary Brown
Guest
Posts: n/a
 
      16th Mar 2010
Forgot to go to the next worksheet :O>

Sub CalcDates()
Dim wks As Worksheet
For Each wks 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
wks.Activate
Next wks
End Sub

--
Hope this helps.
If it does, please click the Yes button.
Thanks in advance for your feedback.
Gary Brown



"kevlarmcc" wrote:

> With both solutions I still have the same result. The macro never moves on to
> the next sheet.
>
> "JLatham" wrote:
>
> > That will work, but (in my opinion) bad form to try to use reserved words as
> > variables or constants. But I think I know why you did it in this case.
> >
> > Probably better for OP to do it this way:
> >
> > Sub CalcDates()
> > Dim anySheet as Worksheet
> > For Each anySheet in ThisWorkbook.Worksheets
> > '.... the other code here
> > Next ' end of anySheet loop
> > End Sub
> >
> > "Gary Brown" wrote:
> >
> > > I'd say you probably need to declare the worksheet....
> > >
> > > Sub CalcDates()
> > > Dim worksheet as worksheet
> > > 'stuff
> > > End Sub
> > >
> > > --
> > > Hope this helps.
> > > If it does, please click the Yes button.
> > > Thanks in advance for your feedback.
> > > Gary Brown
> > >
> > >
> > >
> > > "kevlarmcc" wrote:
> > >
> > > > 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

 
Reply With Quote
 
K_Macd
Guest
Posts: n/a
 
      16th Mar 2010
The reason you continually loop thru the first sheet is that you never leave
it - you need a worksheets(nextsheetname).activate statement before the
cells(2,7).select

Get the above working then have a think about changing cell contents
without activating them - ie Cells(2,7).offset(0,-1).value = "Value" - Much
quicker, less screen flash

--
Ken
"Using Dbase dialects since 82"
"Started with Visicalc in the same year"


"Gary Brown" wrote:

> Forgot to go to the next worksheet :O>
>
> Sub CalcDates()
> Dim wks As Worksheet
> For Each wks 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
> wks.Activate
> Next wks
> End Sub
>
> --
> Hope this helps.
> If it does, please click the Yes button.
> Thanks in advance for your feedback.
> Gary Brown
>
>
>
> "kevlarmcc" wrote:
>
> > With both solutions I still have the same result. The macro never moves on to
> > the next sheet.
> >
> > "JLatham" wrote:
> >
> > > That will work, but (in my opinion) bad form to try to use reserved words as
> > > variables or constants. But I think I know why you did it in this case.
> > >
> > > Probably better for OP to do it this way:
> > >
> > > Sub CalcDates()
> > > Dim anySheet as Worksheet
> > > For Each anySheet in ThisWorkbook.Worksheets
> > > '.... the other code here
> > > Next ' end of anySheet loop
> > > End Sub
> > >
> > > "Gary Brown" wrote:
> > >
> > > > I'd say you probably need to declare the worksheet....
> > > >
> > > > Sub CalcDates()
> > > > Dim worksheet as worksheet
> > > > 'stuff
> > > > End Sub
> > > >
> > > > --
> > > > Hope this helps.
> > > > If it does, please click the Yes button.
> > > > Thanks in advance for your feedback.
> > > > Gary Brown
> > > >
> > > >
> > > >
> > > > "kevlarmcc" wrote:
> > > >
> > > > > 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

 
Reply With Quote
 
kevlarmcc
Guest
Posts: n/a
 
      16th Mar 2010

That did it thanks!
"Gary Brown" wrote:

> Forgot to go to the next worksheet :O>
>
> Sub CalcDates()
> Dim wks As Worksheet
> For Each wks 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
> wks.Activate
> Next wks
> End Sub
>
> --
> Hope this helps.
> If it does, please click the Yes button.
> Thanks in advance for your feedback.
> Gary Brown
>
>
>
> "kevlarmcc" wrote:
>
> > With both solutions I still have the same result. The macro never moves on to
> > the next sheet.
> >
> > "JLatham" wrote:
> >
> > > That will work, but (in my opinion) bad form to try to use reserved words as
> > > variables or constants. But I think I know why you did it in this case.
> > >
> > > Probably better for OP to do it this way:
> > >
> > > Sub CalcDates()
> > > Dim anySheet as Worksheet
> > > For Each anySheet in ThisWorkbook.Worksheets
> > > '.... the other code here
> > > Next ' end of anySheet loop
> > > End Sub
> > >
> > > "Gary Brown" wrote:
> > >
> > > > I'd say you probably need to declare the worksheet....
> > > >
> > > > Sub CalcDates()
> > > > Dim worksheet as worksheet
> > > > 'stuff
> > > > End Sub
> > > >
> > > > --
> > > > Hope this helps.
> > > > If it does, please click the Yes button.
> > > > Thanks in advance for your feedback.
> > > > Gary Brown
> > > >
> > > >
> > > >
> > > > "kevlarmcc" wrote:
> > > >
> > > > > 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

 
Reply With Quote
 
kevlarmcc
Guest
Posts: n/a
 
      16th Mar 2010
You and Gary both got it. Thanks! I realize the code may be amateurish but it
is my first try. Thanks for the additional tips.

"K_Macd" wrote:

> The reason you continually loop thru the first sheet is that you never leave
> it - you need a worksheets(nextsheetname).activate statement before the
> cells(2,7).select
>
> Get the above working then have a think about changing cell contents
> without activating them - ie Cells(2,7).offset(0,-1).value = "Value" - Much
> quicker, less screen flash
>
> --
> Ken
> "Using Dbase dialects since 82"
> "Started with Visicalc in the same year"
>
>
> "Gary Brown" wrote:
>
> > Forgot to go to the next worksheet :O>
> >
> > Sub CalcDates()
> > Dim wks As Worksheet
> > For Each wks 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
> > wks.Activate
> > Next wks
> > End Sub
> >
> > --
> > Hope this helps.
> > If it does, please click the Yes button.
> > Thanks in advance for your feedback.
> > Gary Brown
> >
> >
> >
> > "kevlarmcc" wrote:
> >
> > > With both solutions I still have the same result. The macro never moves on to
> > > the next sheet.
> > >
> > > "JLatham" wrote:
> > >
> > > > That will work, but (in my opinion) bad form to try to use reserved words as
> > > > variables or constants. But I think I know why you did it in this case.
> > > >
> > > > Probably better for OP to do it this way:
> > > >
> > > > Sub CalcDates()
> > > > Dim anySheet as Worksheet
> > > > For Each anySheet in ThisWorkbook.Worksheets
> > > > '.... the other code here
> > > > Next ' end of anySheet loop
> > > > End Sub
> > > >
> > > > "Gary Brown" wrote:
> > > >
> > > > > I'd say you probably need to declare the worksheet....
> > > > >
> > > > > Sub CalcDates()
> > > > > Dim worksheet as worksheet
> > > > > 'stuff
> > > > > End Sub
> > > > >
> > > > > --
> > > > > Hope this helps.
> > > > > If it does, please click the Yes button.
> > > > > Thanks in advance for your feedback.
> > > > > Gary Brown
> > > > >
> > > > >
> > > > >
> > > > > "kevlarmcc" wrote:
> > > > >
> > > > > > 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

 
Reply With Quote
 
JLatham
Guest
Posts: n/a
 
      17th Mar 2010
Sorry, didn't know you wanted to move to the other sheets. I wrote it to NOT
move on purpose.
One of the slowest actions Excel can do is to constantly update/refresh the
video display, so unless there's a need to go another sheet, I generally
don't do it. Plus some users find it annoying to see a bunch of sheets
flashing on the screen in front of them.

"kevlarmcc" wrote:

> With both solutions I still have the same result. The macro never moves on to
> the next sheet.
>
> "JLatham" wrote:
>
> > That will work, but (in my opinion) bad form to try to use reserved words as
> > variables or constants. But I think I know why you did it in this case.
> >
> > Probably better for OP to do it this way:
> >
> > Sub CalcDates()
> > Dim anySheet as Worksheet
> > For Each anySheet in ThisWorkbook.Worksheets
> > '.... the other code here
> > Next ' end of anySheet loop
> > End Sub
> >
> > "Gary Brown" wrote:
> >
> > > I'd say you probably need to declare the worksheet....
> > >
> > > Sub CalcDates()
> > > Dim worksheet as worksheet
> > > 'stuff
> > > End Sub
> > >
> > > --
> > > Hope this helps.
> > > If it does, please click the Yes button.
> > > Thanks in advance for your feedback.
> > > Gary Brown
> > >
> > >
> > >
> > > "kevlarmcc" wrote:
> > >
> > > > 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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
need a macro to drop excel sheets that were not populated James Microsoft Excel Programming 3 8th Jan 2009 10:43 PM
group sheets with macro in excel =?Utf-8?B?amFubmthdHQ=?= Microsoft Excel Misc 2 18th May 2006 02:43 PM
How do I save macro so it's there for all Excel sheets? =?Utf-8?B?U2F2ZSBhIG1hY3JvIGluIEV4Y2Vs?= Microsoft Excel Misc 2 22nd Sep 2005 08:11 PM
Macro (?) for comparing 2 Excel files/sheets Ex`cited Microsoft Excel Programming 1 25th Oct 2004 12:45 PM
Excel Macro & Dialog Sheets Sri Microsoft Excel Misc 1 24th Aug 2004 04:54 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:41 AM.