+ Reply to Thread
Results 1 to 14 of 14

condense VBA

Hybrid View

dantray02 condense VBA 03-11-2014, 01:02 PM
Kaper Re: condense VBA 03-11-2014, 01:09 PM
TMS Re: condense VBA 03-11-2014, 01:15 PM
Kaper Re: condense VBA 03-11-2014, 01:16 PM
TMS Re: condense VBA 03-11-2014, 01:26 PM
Kaper Re: condense VBA 03-11-2014, 05:48 PM
jindon Re: condense VBA 03-11-2014, 09:18 PM
TMS Re: condense VBA 03-11-2014, 09:29 PM
jindon Re: condense VBA 03-11-2014, 10:11 PM
dantray02 Re: condense VBA 03-25-2014, 05:27 AM
TMS Re: condense VBA 03-25-2014, 05:45 AM
dantray02 Re: condense VBA 03-25-2014, 06:13 AM
TMS Re: condense VBA 03-25-2014, 06:27 AM
snb Re: condense VBA 03-25-2014, 07:24 AM
  1. #1
    Registered User
    Join Date
    01-08-2014
    Location
    wigan
    MS-Off Ver
    2007
    Posts
    78

    condense VBA

    Hi, is there a way of making the following more efficient:

    Basically, I am exporting a selection of pages from a workbook. THe selection is determined by the following
    If Sheets("input sheet").Range("b47") = 2 Then
    The value in B47 will either be a 0,1,2

    2 - selects minimum range, plus two additional sheets
    1 - selects minimum range, plus one additional sheet
    0 - selects minimum range only.

    the code is:

     If Sheets("input sheet").Range("b47") = 2 Then
        Sheets(Array("validation", "Cover Sheet", "Management Report", "Management Accounts", _
            "Balance Sheet", "Tax projection - Director 1", "Tax projection - Director 2")). _
            Select
        Selection.Activate
    End If
    
    
    'if one director is to receive a personal tax projection
    If Sheets("input sheet").Range("b47") = 1 Then
        Sheets(Array("validation", "Cover Sheet", "Management Report", "Management Accounts", _
            "Balance Sheet", "Tax projection - Director 1")). _
            Select
        Selection.Activate
    End If
    
    
    
    If Sheets("input sheet").Range("b47") = 0 Then
        Sheets(Array("validation", "Cover Sheet", "Management Report", "Management Accounts", _
            "Balance Sheet")). _
            Select
        Selection.Activate
        End If
    after which point a pdf is generated.

    as always thank you in advance.

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,863

    Re: condense VBA

    You can start with
    select case Sheets("input sheet").Range("b47")
      Case 2 
        Sheets(Array("validation", "Cover Sheet", "Management Report", "Management Accounts", _
            "Balance Sheet", "Tax projection - Director 1", "Tax projection - Director 2")). _
            Select
      Case 1 
        Sheets(Array("validation", "Cover Sheet", "Management Report", "Management Accounts", _
            "Balance Sheet", "Tax projection - Director 1")). _
            Select
      Case 0
        Sheets(Array("validation", "Cover Sheet", "Management Report", "Management Accounts", _
            "Balance Sheet")). _
            Select
    End select
    Selection.Activate
    Best Regards,

    Kaper

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,313

    Re: condense VBA

    Maybe:

    'if two directors are to receive a personal tax projection
    If Sheets("input sheet").Range("b47") = 2 Then
        Sheets(Array("validation", "Cover Sheet", "Management Report", "Management Accounts", _
        "Balance Sheet", "Tax projection - Director 1", "Tax projection - Director 2")).Select
    'if one director is to receive a personal tax projection
    ElseIf Sheets("input sheet").Range("b47") = 1 Then
        Sheets(Array("validation", "Cover Sheet", "Management Report", "Management Accounts", _
        "Balance Sheet", "Tax projection - Director 1")).Select
    ElseIf Sheets("input sheet").Range("b47") = 0 Then
        Sheets(Array("validation", "Cover Sheet", "Management Report", "Management Accounts", _
        "Balance Sheet")).Select
    End If
    Selection.Activate      ' ??? redundant ???

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,863

    Re: condense VBA

    Then next step could be:
    dim sheetsstring as string
    dim sheetslist
    sheetsstring= "validation,Cover Sheet,Management Report,Management Accounts,Balance Sheet"
    select case Sheets("input sheet").Range("b47")
      Case 2 
        sheetsstring = sheetsstring & ",Tax projection - Director 1,Tax projection - Director 2"
      Case 1 
        sheetsstring = sheetsstring & ",Tax projection - Director 1"
    End select
    sheetslist = split(sheetsstring,",")
    Sheets(sheetlist).Select

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,313

    Re: condense VBA

    @Kaper: that's neat. Do you need a Case Else for the "0" option?

    And the double post is the forum, not you. Report the post to get it removed as a" forum induced double post"

    Regards, TMS

  6. #6
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,863

    Re: condense VBA

    Hi,

    Thanks for advice with double post.

    As for case 0 (or case else) - I do not think is needed.
    initial value of sheetsstring has already all needed sheet names.
    Something has to be added to this string only in case 1 or 2.

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: condense VBA

    I would write like this.
        Dim myArray, x As Long
        myArray = Array("validation", "Cover Sheet", "Management Report", "Management Accounts", _
            "Balance Sheet", "Tax projection - Director 1", "Tax projection - Director 2")
        x = Sheets("input sheet").Range("b47").Value
        If x Like "[12]" Then ReDim Preserve myArray(UBound(myArray) - x)
        Sheets(myArray).Select
        Selection.Activate

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,313

    Re: condense VBA

    So, if x=2, you would reduce the upper bound by 2 ... . Feels wrong but I might be missing something.

    I like the concept but maybe it should be -(2-x)

    Regards, TMS

  9. #9
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: condense VBA

    Ahhh, it was other way around... thanks.

    Perhaps
        Dim myArray, x As Long
        myArray = Array("validation", "Cover Sheet", "Management Report", "Management Accounts", _
            "Balance Sheet", "Tax projection - Director 1", "Tax projection - Director 2")
        x = Sheets("input sheet").Range("b47").Value
        ReDim Preserve myArray(UBound(myArray) - 2 + x)
        Sheets(myArray).Select
        Selection.Activate

  10. #10
    Registered User
    Join Date
    01-08-2014
    Location
    wigan
    MS-Off Ver
    2007
    Posts
    78

    Re: condense VBA

    Hi, sorry for the delay - I have been away for a week.

    Can I please ask - if I needed to include an additional worksheet at the end of
         "Balance Sheet", "Tax projection - Director 1", "Tax projection - Director 2")
    based on the same criteria how would I go about that?

    I have tried

         "Balance Sheet", "Tax projection - Director 1", "Tax projection - Director 2", "Tax Calculations")
    but this does not work.

    thanks.
    dan.

  11. #11
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,313

    Re: condense VBA

    Well, the Director sheets are the optional ones, so they have to stay at the end.

    Stick it in the middle somewhere before those Director items. I guess where depends on the sequence you want them printed.

    Regards, TMS

  12. #12
    Registered User
    Join Date
    01-08-2014
    Location
    wigan
    MS-Off Ver
    2007
    Posts
    78

    Re: condense VBA

    Hi,

    The additional pages would also be optional. So if either director has a tax projection then i would also want to show the calculation at the end

    thanks.

  13. #13
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,313

    Re: condense VBA

    Then you need to look at your OP and adapt the code to reflect another variation.

    The value in B47 will either be a 0,1,2

    2 - selects minimum range, plus two additional sheets
    1 - selects minimum range, plus one additional sheet
    0 - selects minimum range only.

    Regards, TMS

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

    Re: condense VBA

    Sub M_snb()
        With Sheets("input sheet").Range("b47")
          Sheets(Split("validation|Cover Sheet|Management Report|Management Accounts|Balance Sheet" & IIf(.Value > 0, "|Tax projection - Director 1" & IIf(.Value > 1, "|Tax projection - Director 2", ""), ""), "|")).Select
        End With
    End Sub



+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Condense Information
    By ace2go in forum Excel General
    Replies: 14
    Last Post: 07-12-2013, 11:06 AM
  2. Condense Data
    By jomili in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-10-2011, 04:02 PM
  3. Condense code
    By tone640 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-02-2011, 08:25 AM
  4. [SOLVED] Condense Code
    By Nigel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-22-2005, 08:10 PM
  5. Condense formula
    By Derek Y via OfficeKB.com in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 11-17-2005, 11:10 PM

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