Excel Macro: incrementing "Sheets"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • boaring
    New Member
    • Sep 2006
    • 3

    Excel Macro: incrementing "Sheets"

    I need to use Macro's in Excel and always the same changes to 31 Sheets. (31 days in the Month).
    I'm trying to use a FOR loop but cannot get the correct format to have the sheet number to increment by 1 in each loop.
    Below is a silly little Macro but indicates what my problem is.
    I have tried inserting my variable intSheet in various ways including concatenation to no avail.
    Any help would be greatly appreciated.
    Thankyou
    Sub Macro2()
    '
    ' Macro2 Macro
    ' Macro recorded 02 09 2006
    '
    Dim intCounter As Integer
    Dim intSheet As Integer

    For intCounter = 1 To 31

    Range("C5").Sel ect
    ActiveCell.Form ulaR1C1 = "hello"
    Range("C6").Sel ect
    Sheets("Sheet1" ).Select 'I need this sheet to increment by 1 each loop
    Range("C6").Sel ect ' I tried various ways of using intSheet to replace "Sheet1"

    intSheet = intSheet + 1

    Next

    End Sub
  • dave ellis
    New Member
    • Sep 2006
    • 3

    #2
    Sheets("Sheet1" ).Select 'I need this sheet to increment by 1 each loop
    Range("C6").Sel ect ' I tried various ways of using intSheet to replace "Sheet1"


    this is a much simplified version of your pilot script:


    Sub Macro2()
    '
    ' Macro2 Macro
    ' Macro recorded 9/3/06 by dave ellis
    '

    '
    Dim intSheet As Integer

    For intSheet = 0 To 30
    intSheet = intSheet + 1

    Range("C5").For mulaR1C1 = "hello"
    Range("C6").Sel ect
    Sheets(intSheet ).Select 'I need this sheet to increment by 1 each loop
    Range("C6").Sel ect ' I tried various ways of using intSheet to replace "Sheet1"

    Next intSheet
    End Sub

    voila!

    Comment

    • dave ellis
      New Member
      • Sep 2006
      • 3

      #3
      a correction to the previous reply

      Sub Macro2()
      '
      ' Macro2 Macro
      ' Macro recorded 9/3/06 by dave ellis
      '

      '
      Dim intSheet As Integer

      For intSheet = 1 To 31 ' restored your original idea here, and ...
      'intSheet = intSheet + 1 ' notice, you need not literally increment your variable

      Range("C5").For mulaR1C1 = "hello"
      Range("C6").Sel ect
      Sheets(intSheet ).Select 'I need this sheet to increment by 1 each loop
      Range("C6").Sel ect ' I tried various ways of using intSheet to replace "Sheet1"

      Next intSheet
      End Sub

      sorry for the misstep, and finally, voila!
      Last edited by dave ellis; Sep 4 '06, 02:48 AM. Reason: consistency in commenting

      Comment

      • boaring
        New Member
        • Sep 2006
        • 3

        #4
        Hi Dave,
        Thankyou very much! Works like a dream. Now I can use any macro and have it repeated 31 times :)

        Comment

        Working...