Results 1 to 5 of 5

Thread: Apply code to all sheets in a workbook [resolved, really]

  1. #1

    Thread Starter
    Hyperactive Member thebloke's Avatar
    Join Date
    May 2003
    Posts
    358

    Apply code to all sheets in a workbook [resolved, really]

    Guys, I've written a bit of code that does exactly what I want, that's no problem. What I want though is for the code to apply to every sheet in the workbook from one click of a command button. I know there is a simple way to do this but the syntax just eludes me. I don't really do Excel, or at least I try not to.

    Cheers
    Last edited by thebloke; Feb 18th, 2004 at 07:51 AM.
    The Bloke
    www.blokeinthekitchen.com
    making cooking cool for blokes

  2. #2

    Thread Starter
    Hyperactive Member thebloke's Avatar
    Join Date
    May 2003
    Posts
    358
    I thought I'd resolved this but it doesn't work:

    VB Code:
    1. Sub UpdateForecast()
    2.  
    3. Dim w As Integer, x As Integer, y As Integer, z As Integer
    4. Dim firstrow As Integer, firstcol As Integer
    5. Dim colcount As Integer, rowcount As Integer
    6. Dim forecol As Integer
    7. Dim strmonth As String
    8. Dim myArray As Variant
    9.  
    10. For Each sht In ActiveWorkbook.Sheets
    11.  
    12.  
    13. If ActiveSheet.Cells(1, 1) <> "" Then
    14.  
    15. ActiveSheet.Cells(6, 3).Activate
    16.  
    17. myArray = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
    18.  
    19.     For w = 0 To UBound(myArray)
    20.  
    21.     x = 6
    22.     y = 3
    23.     z = 0
    24.  
    25.     firstrow = x
    26.     firstcol = y
    27.    
    28.     colcount = ActiveCell.CurrentRegion.Columns.Count
    29.     rowcount = ActiveCell.CurrentRegion.Rows.Count
    30.    
    31.     strmonth = myArray(w)
    32.    
    33.     Do Until x = rowcount + 1
    34.    
    35.         If Trim(ActiveSheet.Cells(x, 4)) = strmonth Then
    36.             z = z + ActiveSheet.Cells(x, y)
    37.         End If
    38.        
    39.         x = x + 1
    40.        
    41.     Loop
    42.    
    43.     x = 6
    44.     forecol = 7
    45.    
    46.     Do Until x = rowcount + 1
    47.    
    48.     ActiveSheet.Cells(x, forecol).Activate
    49.    
    50.         If Trim(ActiveSheet.Cells(x, forecol)) = strmonth Then
    51.             ActiveSheet.Cells(x, forecol + 1) = z
    52.         End If
    53.        
    54.         x = x + 1
    55.    
    56.     Loop
    57.    
    58.     ActiveSheet.Cells(6, 3).Activate
    59.    
    60.     Next w
    61.    
    62.     End If
    63.    
    64. Next sht
    65.  
    66. End Sub

    Rather, it does work if I step through it and manually activate each sheet as I'm doing so. I think all I need to do is to activate each sheet programatically but I'm not sure how to do this or if I'm barking up the wrong proverbial.

    Cheers
    The Bloke
    www.blokeinthekitchen.com
    making cooking cool for blokes

  3. #3

    Thread Starter
    Hyperactive Member thebloke's Avatar
    Join Date
    May 2003
    Posts
    358
    For those interested, this did the trick:

    VB Code:
    1. Sub UpdateForecast()
    2.  
    3. Dim v As Integer, w As Integer, x As Integer, y As Integer, z As Integer
    4. Dim firstrow As Integer, firstcol As Integer
    5. Dim colcount As Integer, rowcount As Integer
    6. Dim forecol As Integer, sCount As Integer
    7. Dim strmonth As String
    8. Dim myArray As Variant
    9.  
    10.  
    11. sCount = Worksheets.Count
    12.  
    13.  
    14. v = 1
    15.  
    16. Do Until v = sCount + 1
    17.  
    18. ActiveWorkbook.Sheets(v).Activate
    19.  
    20. If ActiveSheet.Cells(1, 1) <> "" Then
    21.  
    22. ActiveSheet.Cells(6, 3).Activate
    23.  
    24. myArray = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
    25.  
    26.     For w = 0 To UBound(myArray)
    27.  
    28.     x = 6
    29.     y = 3
    30.     z = 0
    31.  
    32.     firstrow = x
    33.     firstcol = y
    34.    
    35.     colcount = ActiveCell.CurrentRegion.Columns.Count
    36.     rowcount = ActiveCell.CurrentRegion.Rows.Count
    37.    
    38.     strmonth = myArray(w)
    39.    
    40.     Do Until x = rowcount + 1
    41.    
    42.         If Trim(ActiveSheet.Cells(x, 4)) = strmonth Then
    43.             z = z + ActiveSheet.Cells(x, y)
    44.         End If
    45.        
    46.         x = x + 1
    47.        
    48.     Loop
    49.    
    50.     x = 6
    51.     forecol = 7
    52.    
    53.     Do Until x = rowcount + 1
    54.    
    55.         If Trim(ActiveSheet.Cells(x, forecol)) = strmonth Then
    56.             ActiveSheet.Cells(x, forecol + 1) = z
    57.         End If
    58.        
    59.         x = x + 1
    60.    
    61.     Loop
    62.    
    63.     ActiveSheet.Cells(6, 3).Activate
    64.    
    65.     Next w
    66.    
    67.     End If
    68.    
    69.  
    70. v = v + 1
    71.  
    72. Loop
    73.  
    74. ActiveWorkbook.Sheets(1).Activate
    75.  
    76. End Sub
    The Bloke
    www.blokeinthekitchen.com
    making cooking cool for blokes

  4. #4
    New Member
    Join Date
    Feb 2004
    Posts
    8
    Looks confusing enough, one would think it would work. Except when I cut and pasted this code into a module in a new work book something happened but there was no visible output. Strange. I'll keep trying to sort through it.

  5. #5

    Thread Starter
    Hyperactive Member thebloke's Avatar
    Join Date
    May 2003
    Posts
    358
    Tempted, this works for me. What are you trying to do with it? It'll need modifying to suit your spreadsheet.
    The Bloke
    www.blokeinthekitchen.com
    making cooking cool for blokes

Posting Permissions

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



Click Here to Expand Forum to Full Width