Results 1 to 3 of 3

Thread: Shorten VBA Code By Removing Redundant Superfluous Code

  1. #1
    Senior Member
    Join Date
    Mar 2013
    Posts
    107
    Rep Power
    12

    Shorten VBA Code By Removing Redundant Superfluous Code

    hI

    This code below i have modified to get it to work...it does work but could it be made simpler or tidied up

    Code:
    Option Explicit
    
    Sub SubTotalize()
       
        With Sheets("XFLOW A")
           .Unprotect Password:="abc"
           .Range("B3:H" & .Cells(Rows.Count, 2).End(xlUp).Row).Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(2, 3, 7), Replace:=True, PageBreaks:=False, SummaryBelowData:=True
           .Protect UserInterfaceOnly:=True, Password:="abc"
        End With
        End Sub
        Sub Subtotalize_1()
        With Sheets("XFLOW B")
           .Unprotect Password:="abc"
           .Range("B3:H" & .Cells(Rows.Count, 2).End(xlUp).Row).Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(2, 3, 7), Replace:=True, PageBreaks:=False, SummaryBelowData:=True
           .Protect UserInterfaceOnly:=True, Password:="abc"
        End With
        End Sub
        Sub subtotalize_2()
        With Sheets("XFLOW C")
           .Unprotect Password:="abc"
           .Range("B3:H" & .Cells(Rows.Count, 2).End(xlUp).Row).Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(2, 3, 7), Replace:=True, PageBreaks:=False, SummaryBelowData:=True
           .Protect UserInterfaceOnly:=True, Password:="abc"
        End With
    End Sub

  2. #2
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    This could be one way to shorten it

    Code:
    Sub SubTotalize()
       
       Dim var, strarrSheetNames() As String
       strarrSheetNames = Split("XFLOW A,XFLOW B,XFLOW C", ",")
       For Each var In strarrSheetNames
            With Sheets(var)
               .Unprotect Password:="abc"
               .Range("B3:H" & .Cells(Rows.Count, 2).End(xlUp).Row).Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(2, 3, 7), Replace:=True, PageBreaks:=False, SummaryBelowData:=True
               .Protect UserInterfaceOnly:=True, Password:="abc"
            End With
        Next var
        
    End Sub
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  3. #3
    Senior Member
    Join Date
    Mar 2013
    Posts
    107
    Rep Power
    12
    Thank you

    I still have an error when I select Daily Total button on XFLOW A sheet.It still performs the totals but pops up a error message.I think the error could be that both XFLOW B & C sheets are empty of data.
    On the attached sample if the Daily Total button is pressed it will show the error.
    Can the sheets all have the Daily Total option but work independent of each other.Example would be that XFLOW A & B might get data daily but XFLOW C may get data every second or third day..

    Thanks

    Paul
    Attached Files Attached Files

Similar Threads

  1. Excel VBA Code to Add New Sheets
    By cdurfey in forum Excel Help
    Replies: 1
    Last Post: 06-25-2013, 08:05 AM
  2. Removing unused Cell styles - need an efficient code
    By siddharthsindhwani in forum Excel Help
    Replies: 8
    Last Post: 04-15-2013, 07:12 AM
  3. VBA Code to extract subtotals
    By Howardc in forum Excel Help
    Replies: 2
    Last Post: 12-02-2012, 01:15 PM
  4. VBA Code to Extract data
    By Howardc in forum Excel Help
    Replies: 1
    Last Post: 07-24-2012, 11:37 PM
  5. VBA Code to Clear the Immediate Window.
    By technicalupload in forum Excel and VBA Tips and Tricks
    Replies: 0
    Last Post: 09-02-2011, 03:04 PM

Posting Permissions

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