+ Reply to Thread
Results 1 to 9 of 9

Condense code

Hybrid View

tone640 Condense code 09-01-2011, 07:10 AM
MaczaQ Re: Condense code 09-01-2011, 08:13 AM
tone640 Re: Condense code 09-01-2011, 09:46 AM
MaczaQ Re: Condense code 09-01-2011, 01:33 PM
tone640 Re: Condense code 09-02-2011, 03:52 AM
snb Re: Condense code 09-02-2011, 04:12 AM
tone640 Re: Condense code 09-02-2011, 06:52 AM
snb Re: Condense code 09-02-2011, 07:29 AM
tone640 Re: Condense code 09-02-2011, 08:25 AM
  1. #1
    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.

  2. #2
    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 (?)



  3. #3
    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

  4. #4
    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.

  5. #5
    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