+ Reply to Thread
Results 1 to 9 of 9

Condense code

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-15-2011
    Location
    Chester, England
    MS-Off Ver
    Excel 2013
    Posts
    117

    Condense code

    I am currently learning to condense code by turning several macros that are similar into one that I can use for all.

    The below is the last bit that I need to change to have a single macro instead of 7.

    At this point, I am copying the ActiveSheet and a sheet called "Invoice" to a new workbook, then adding the formula to the sheet called "Invoice":

    Sub InvComps()
    
        Sheets(Array("Invoice", ActiveSheet.Name)).Copy
        Range("J22").FormulaR1C1 = "=SUM(Complete!C[17])"
        
    End Sub
    
    Sub InvAborts()
    
        Sheets(Array("Invoice", ActiveSheet.Name)).Copy
        Range("J22").FormulaR1C1 = "=SUM(Aborts!C[17])"
        
    End Sub
    
    Sub InvEmergs()
    
        Sheets(Array("Invoice", ActiveSheet.Name)).Copy
        Range("J22").FormulaR1C1 = "=SUM(Emergencies!C[17])"
    
    End Sub
    
    Sub InvADV()
    
        Sheets(Array("Invoice", ActiveSheet.Name)).Copy
        Range("J22").FormulaR1C1 = "=SUM(ADV!C[17])"
    
    End Sub
    
    Sub InvSSE()
    
        Sheets(Array("Invoice", ActiveSheet.Name)).Copy
        Range("J22").FormulaR1C1 = "=SUM(SSE!C[17])"
    
    End Sub
    
    Sub InvUKMA()
    
        Sheets(Array("Invoice", ActiveSheet.Name)).Copy
        Range("J22").FormulaR1C1 = "=SUM(UKMA!C[17])"
    
    End Sub
    
    Sub InvUKGC()
    
        Sheets(Array("Invoice", ActiveSheet.Name)).Copy
        Range("J22").FormulaR1C1 = "=SUM(UKGC!C[17])"
    
    End Sub

    How do I get the sheet name in each formula to change each time so that it refers to the sheet in the workbook that isn't "Invoice"?

    Thanks in advance.
    Last edited by tone640; 09-01-2011 at 11:00 AM.

  2. #2
    Valued Forum Contributor MaczaQ's Avatar
    Join Date
    06-03-2011
    Location
    Poland
    MS-Off Ver
    Excel 2003 / XP
    Posts
    510

    Re: Condense code

    Maybe this way ?

    'main Loop
    Sub LoopSheets()
      For Each WS in Worksheets
        if WS.name <> "Invoice" Then
            InvComps WS 'call your procedure
        end if
      Next WS
    End Sub
    
    'one of Your procedures
    Sub InvComps(byref WS as worksheet)
    
        Sheets(Array("Invoice", WS.Name)).Copy
        Range("J22").FormulaR1C1 = "=SUM(Complete!C[17])"
        
    End Sub
    Best Regards
    MaczaQ

  3. #3
    Forum Contributor
    Join Date
    06-15-2011
    Location
    Chester, England
    MS-Off Ver
    Excel 2013
    Posts
    117

    Re: Condense code

    Thanks for your suggestion MaczaQ, I wasn't quite sure how to implement it, but I used it with Case Select in the end:

    For Each ws In Worksheets
            If ws.Name <> "Invoice" Then
            
                ws.Select
                
                Select Case ActiveSheet.Name
                Case "Complete"
                Sheets("Invoice").Range("J22").FormulaR1C1 = "=SUM(Complete!C[17])"
                Case "Aborts"
                Sheets("Invoice").Range("J22").FormulaR1C1 = "=SUM(Aborts!C[17])"
                Case "Emergencies"
                Sheets("Invoice").Range("J22").FormulaR1C1 = "=SUM(Emergencies!C[17])"
                Case "ADV"
                Sheets("Invoice").Range("J22").FormulaR1C1 = "=SUM(ADV!C[17])"
                Case "SSE"
                Sheets("Invoice").Range("J22").FormulaR1C1 = "=SUM(SSE!C[17])"
                Case "UKMA"
                Sheets("Invoice").Range("J22").FormulaR1C1 = "=SUM(UKMA!C[17])"
                Case "UKGC"
                Sheets("Invoice").Range("J22").FormulaR1C1 = "=SUM(UKGC!C[17])"
                End Select
        
            End If
        Next ws
    What do you think?

  4. #4
    Valued Forum Contributor MaczaQ's Avatar
    Join Date
    06-03-2011
    Location
    Poland
    MS-Off Ver
    Excel 2003 / XP
    Posts
    510

    Re: Condense code

    good solution but I only wonder why you always refer to one cell in sheet "Invoice"
    Sheets("Invoice").Range("J22").FormulaR1C1
    When you will start your loop through all sheets at the end of loop you will get only one result in above mentioned cell.
    I suggest to change something for prevent to overwrite results ;-)

    Best Regards
    MaczaQ

  5. #5
    Forum Contributor
    Join Date
    06-15-2011
    Location
    Chester, England
    MS-Off Ver
    Excel 2013
    Posts
    117

    Re: Condense code

    There are 2 sheets copied to a new workbook each time the macro is run - "Invoice" and the backing data ("Completes", "Aborts" etc).

    Thanks again for your help.

  6. #6
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Condense code

    there's no need to select a sheet:

    For Each ws In Worksheets
      If ws.Name <> "Invoice" Then sheets("Invoice").Range("J22")="=" & ws.name & "!C[17]"
    next
    But this overwrites the same cell 7 times (?)



  7. #7
    Forum Contributor
    Join Date
    06-15-2011
    Location
    Chester, England
    MS-Off Ver
    Excel 2013
    Posts
    117

    Re: Condense code

    Even better snb, thanks for your suggestion.

    I'm using the following now (ADV sheets are a special case that need a bit extra info, and slightly amended formula to SUM):

    For Each ws In Worksheets
            If ws.Name <> "Invoice" Then
                Select Case ws.Name
                Case "ADV"
                Sheets("Invoice").Range("J22") = "=SUM(" & ws.Name & "!C[17])"
                Range("B2", Range("A65536").End(xlUp).Offset(, 1)).FormulaR1C1 = "=VLOOKUP(RC[-1],'[Timesheet import.xlsm]Completions'!C1:C2,2,0)"
                Range("B2", Range("A65536").End(xlUp).Offset(, 1)) = Range("B2", Range("A65536").End(xlUp).Offset(, 1)).Value
                Case Else
                Sheets("Invoice").Range("J22") = "=SUM(" & ws.Name & "!C[17])"
                End Select
            End If
        Next ws
    -edit: The workbook will consist of 1 of the following 7 combinations of sheets:

    "Invoice" and "Completes"
    "Invoice" and "Aborts"
    "Invoice" and ...
    "Invoice" and ...
    "Invoice" and ...
    "Invoice" and ...
    "Invoice" and ...

    So the formula is only entered once on "Invoice" to sum the backing data.

    Again, many thanks both for your help.
    Last edited by tone640; 09-02-2011 at 06:58 AM. Reason: Clarifying, spelling

  8. #8
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Condense code

     
    For Each ws In Worksheets
      If ws.Name <> "Invoice" Then
        Sheets("Invoice").Range("J22") = "=SUM(" & ws.Name & "!C[17])"
        if ws.Name="ADV" then
          with ws.Range("B2", ws.Range("A65536").End(xlUp).Offset(, 1))
           .FormulaR1C1 = "=VLOOKUP(RC[-1],'[Timesheet import.xlsm]Completions'!C1:C2,2,0)"
           .value=.value
          end with
        End If
      end if
    Next
    Last edited by snb; 09-02-2011 at 09:05 AM.

  9. #9
    Forum Contributor
    Join Date
    06-15-2011
    Location
    Chester, England
    MS-Off Ver
    Excel 2013
    Posts
    117

    Re: Condense code

    Thanks yet again, this has really helped my understanding.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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