+ Reply to Thread
Results 1 to 10 of 10

Basic Loop Trouble - need to convert active sheet to integer

Hybrid View

  1. #1
    Registered User
    Join Date
    05-05-2009
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    68

    Question Basic Loop Trouble - need to convert active sheet to integer

    I'm new to usign loops and I've run into a bit of trouble, I need to use a loop to add a value of 1 to the workseets name on each repetition. The worksheets are all named with single numbers but I'm not sure how to convet the active sheet reference into an integer t hat I can use to add a value to. I also tried



    
    Dim Order As String, i As Integer
    Order = ActiveSheet.Name
    
    Dim OrderNum As Integer
    OrderNum = Order
    
    
    For i = 1 To 21
    
    
    Code Goes here
    
        
    Sheets(OrderNum + 1).Activate
    
    Next i
    I also tried

    
    Dim Order A, i As Integer
    Order = ActiveSheet.Name
    
    
    For i = 1 To 21
    
    
    Code Goes here
    
        
    Sheets(Order + 1).Activate
    
    Next i
    I also tried

    
    Dim Order as String = ActiveSheet.Name
    
    Dim OrderNumber As Integer = Convert.ToInt32(Order)
    Any assistance greatly appreciated

    Thanks
    Last edited by mick86; 09-16-2012 at 03:39 PM.

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Basic Loop Trouble - need to convert active sheet to integer

    Hi Mick,

    If it's the name - you've got to use quote marks, because the name is a string - when you just use a number, that's the sheet index.
    Dim S As String:S=Order + 1
    Sheets(S).Activate
    or something similar!
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Registered User
    Join Date
    05-05-2009
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    68

    Re: Basic Loop Trouble - need to convert active sheet to integer

    I tried like this but it gets stuck on Sheet "2"

    
    Sub Reset_All_Orders()
    '
    ' Reset_All_Orders Macro
    ' Reset all orders to default layout
    '
    
    '
    Dim Response As Integer
       
          Response = MsgBox("Do you want to reset all orders?", _
    vbYesNo + vbQuestion + vbDefaultButton2, _
    "Reset Sheets")
    
          If Response = vbYes Then
          
          Sheets("1").Activate         ' As Macro Initiated From A Sheet Outside Of The 1-21 Range
          
        
    Dim Order As String, OrderNum As String, i As Integer
    Order = ActiveSheet.Name
    OrderNum = Order + 1           ' Increase The Active Sheet Number By 1
    
    For i = 1 To 21                     ' Set Loop To Run 21 Times
    
     ' Makes Various Changes To Sheet Here
        
    Sheets(OrderNum).Activate     ' Activate The Next Sheet
    
    Next i                                 ' Repeat Loop
    
    Exit Sub
    
    
        Else
             
             Range("$A1").Select
             
             Exit Sub
          End If
    End Sub
    Last edited by Cutter; 09-18-2012 at 07:19 PM. Reason: Removed whole post quote

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Basic Loop Trouble - need to convert active sheet to integer

    For i = 2 To 21                     ' Set Loop To Run 21 Times
    
     ' Makes Various Changes To Sheet Here
        
    Sheets(i).Activate     ' Activate The Next Sheet
    
    Next i                                 ' Repeat Loop
    It all depends on how your sheets are named - this will give you Sheets(2) to Sheets(21)

    If you want "Sheet2" to "Sheet21" then you could use:

    Worksheets("Sheet" & i)
    Last edited by xladept; 09-17-2012 at 03:06 PM.

  5. #5
    Registered User
    Join Date
    05-05-2009
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    68

    Re: Basic Loop Trouble - need to convert active sheet to integer

    So much simpler, I was perhaps coming at things from the wrong direction.

    My sheet names do not contain the word "sheet', they just have numbers.

    Your code almost works, though the fact that the 1st sheet i created is called "index" seems to be a problem. The reference style you suggested seems to refer to the sheets number derived from its creation time rather than the sheets name.

    As you write it it starts on the sheet named "1" which is (sheet 2) according to the visual basic menu and it runs up until sheet "20" (which is technically sheet 19).

    If I change the code to:


    Dim i As Integer
    
    For i = 2 To 22
    
    Sheets(i).Activate
    
    ' Do Stuff
    
    Next i

    This is a work around for the problem. Perhaps if quotation marks were included with in the brackets I'd be able to refer to the exact sheet name

    Sheets("i").Activate
    or

    Sheets("""&i&""").Activate
    I'll be able to use that in multiple locations where I had previously been doing things manually.
    Last edited by Cutter; 09-18-2012 at 07:20 PM. Reason: Removed whole post quote

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Basic Loop Trouble - need to convert active sheet to integer

    The "i" wont work.

  7. #7
    Registered User
    Join Date
    05-05-2009
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    68

    Re: Basic Loop Trouble - need to convert active sheet to integer

    Neither way seems to work

         
    
    Dim i As Integer
         
            For i = 1 To 21
    
            Dim Sheet
    
            Sheet = """ & i & """
           
            
        Sheets(Sheet).Activate

    This doesn't work either

  8. #8
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Basic Loop Trouble - need to convert active sheet to integer

    How about a mockup? OR:

    Dim ws As Worksheet
    For each ws in ThisWorkbook.Worksheets
    ws.Activate
    
    
    
    Next
    Last edited by xladept; 09-18-2012 at 02:25 PM.

  9. #9
    Registered User
    Join Date
    05-05-2009
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    68

    Re: Basic Loop Trouble - need to convert active sheet to integer

    Quote Originally Posted by xladept View Post
    How about a mockup? OR:

    Dim ws As Worksheet
    For each ws in ThisWorkbook.Worksheets
    ws.Activate
    
    
    
    Next
    This is a good option that works around needing to name the involved worksheets.

  10. #10
    Registered User
    Join Date
    05-05-2009
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    68

    Re: Basic Loop Trouble - need to convert active sheet to integer

    Attached a mock up, it works as is but refers to the sheets index numbers rather than the sheets names. I was initially hoping to do it by sheet name. That said it works alright as it is.
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1