+ Reply to Thread
Results 1 to 7 of 7

I have 14,900 Lines of code in my Sub, Could someone show me how to be more efficient?

Hybrid View

  1. #1
    Registered User
    Join Date
    05-06-2012
    Location
    New Jersey
    MS-Off Ver
    Excel 2010
    Posts
    2

    Post I have 14,900 Lines of code in my Sub, Could someone show me how to be more efficient?

    I am sure the entire Sub does exactly what I need it to do except for the fact the compiler said No Way! It is too large to compile.
    I have 14,900 lines of code.

    I am not asking for some fish.
    I am looking for some one to teach me how to fish here.

    I am sure there is a way I could use a clever loop here to get this down a few thousand lines of code.
    Loops are not unfamiliar to me as you could see if you peaked at my other modules I have codded.
    But in the instance of this Sub I had a hard time seeing the logic to do what I needed to be done any other way then I did it.

    I would post the code here but that would be insane as there is 14,900 lines of it.
    While that is a lot of code. I promise it is all very easy to follow and straight forward what I am trying to do.
    I will upload the Entire Workbook.
    The sub I have issue with is
    Module5
    Sub UpdateDaily()

    Basically what this does is it takes values from the first sheet and based upon what day of the week it is copies that data to the appropriate sheet under the appropriate day of the month.

    Any help given would surely take my programming up a notch.
    I thank you in advance for your time.
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: I have 14,900 Lines of code in my Sub, Could someone show me how to be more efficient?

    (1) Never used "As Integer" change to "As Long"
    Reason: Computers today find long (integers) easier to use than (short) integers.
    "As Integer" creates a short integer.
    "As Long" creates a long integer.

    (2)
    Range("b5").Select
    actual_new(0) = Range("b5").Value
    There is no reason to "Select" a cell before putting a value from a cell into an array, or before putting a value from an array into a cell.
    That will almost cut your code in half.

    (3)
    x = 0
    Do Until x = 8
    monthly_new(x) = monthly_new(x) + actual_new(x)
    monthly_mbbtab(x) = monthly_mbbtab(x) + actual_mbbtab(x)
    monthly_hpc(x) = monthly_hpc(x) + actual_hpc(x)
    monthly_upgrade(x) = monthly_upgrade(x) + actual_upgrade(x)
    monthly_gwg(x) = monthly_gwg(x) + actual_gwg(x)
    monthly_gross(x) = monthly_gross(x) + actual_gross(x)
    x = x + 1
    Loop
    Should be:

    For x = 0 to 7
        monthly_new(x) = monthly_new(x) + actual_new(x)
        monthly_mbbtab(x) = monthly_mbbtab(x) + actual_mbbtab(x)
        monthly_hpc(x) = monthly_hpc(x) + actual_hpc(x)
        monthly_upgrade(x) = monthly_upgrade(x) + actual_upgrade(x)
        monthly_gwg(x) = monthly_gwg(x) + actual_gwg(x)
        monthly_gross(x) = monthly_gross(x) + actual_gross(x)
    Next x
    (4) I'm not going to copy your code, but I've copied the comments.
    I then added the cells for each group. These cells are five rows apart.
    And so:

    Dim i As Long
    '
    '
    '...
    '(0) BARNEGAT   = N5:S5
    '(1) BRIDGETON  = N10:S10
    '(2) GALLOWAY   = N15:S15
    '(3) HAMMONTON  = N20:S20
    '(4) MANTUA     = N25:S25
    '(5) MILLVILLE  = N30:S30
    '(6) VENTNOR    = N35:S35
    '(7) VINELAND   = N40:S40
    
    For i = 0 To 7
        Range("N" & (5 + i * 5)) = monthly_new(i)
        Range("O" & (5 + i * 5)) = monthly_mbbtab(i)
        Range("P" & (5 + i * 5)) = monthly_hpc(i)
        Range("Q" & (5 + i * 5)) = monthly_upgrade(i)
        Range("R" & (5 + i * 5)) = monthly_gwg(i)
        Range("S" & (5 + i * 5)) = monthly_gross(i)
    Next i
    Multiplication takes precedence over addition, so 5 + i * 5 is the same as 5 + (i * 5).
    "N" & (5 + i * 5) when i = 0 creates a string "N5"
    "N" & (5 + i * 5) when i = 7 creates a string "N40"
    Last edited by StevenM; 05-07-2012 at 04:11 AM.

  3. #3
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: I have 14,900 Lines of code in my Sub, Could someone show me how to be more efficient?

    I am not asking for some fish.
    I am looking for some one to teach me how to fish here.
    (5) You can loop through your Worksheets 2 through 9 with the following code:

    Dim wb As Workbook, i As Long
    '
    '
    '...
    '    
    Set wb = ThisWorkbook
    For i = 2 To 9
        With wb.Worksheets(i)
            ' Do something here
        End With
    Next i
    Between the with & end with you can refer to a Range like so:
    .Range("A1") = whatever or whatever = .Range("A1")

    The dot before Range means connect with: wb.Worksheets(i)

    For example:

    Sheet2.Range("z154").Value = Barnegat_Needed_New_Goal  'Range("z4").Value
    Sheet3.Range("z154").Value = Bridgeton_Needed_New_Goal  ' Range("z9").Value
    Sheet4.Range("z154").Value = Galloway_Needed_New_Goal   ' Range("z14").Value
    Sheet5.Range("z154").Value = Hammonton_Needed_New_Goal  ' Range("z19").Value
    Sheet6.Range("z154").Value = Mantua_Needed_New_Goal     ' I didn't look up the rest,  
    Sheet7.Range("z154").Value = Millville_Needed_New_Goal  ' but if they are five rows apart
    Sheet8.Range("z154").Value = Ventnor_Needed_New_Goal    ' then ...
    Sheet9.Range("z154").Value = Vineland_Needed_New_Goal
    The loop would take care of the left side of equals.
    .Range("Z154").Value =
    The right side of the equals might be:
    Range("Z" & 4 + (i - 2) * 5).Value
    "Z" & 4 + (i - 2) * 5 when i = 2 is a string "Z4"
    "Z" & 4 + (i - 2) * 5 when i = 3 is a string "Z9"
    etc.
    Note: Range without a dot refers to the Active Worksheet.
    Last edited by StevenM; 05-07-2012 at 05:19 AM.

  4. #4
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: I have 14,900 Lines of code in my Sub, Could someone show me how to be more efficient?

    As an afterthought,

    Dim wb As Workbook, i As Long
    '
    '
    '...
    '    
    Set wb = ThisWorkbook
    For i = 2 To 9
        With wb.Worksheets(i)
            ' Do something here
        End With
    Next i
    Can be simplified to:

    Dim i As Long
    '
    '
    '...
    '    
    For i = 2 To 9
        With Worksheets(i)
            ' Do something here
        End With
    Next i
    Last edited by StevenM; 05-07-2012 at 05:41 AM.

  5. #5
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: I have 14,900 Lines of code in my Sub, Could someone show me how to be more efficient?

    (5b)

    Dim i As Long, wb As Workbook
    '
    '
    '...
    '
    ' Worksheet 1 needs to be Active because of Range (without a dot)
    For i = 2 To 9
        With Worksheets(i)
            .Range("Z154").Value = Range("Z" & 4 + (i - 2) * 5).Value
        End With
    Next i
    
    ' Only the Workbook needs to be Active; Any Worksheet can be active
    For i = 2 To 9
        With Worksheets(i)
            .Range("Z154").Value = Worksheets(1).Range("Z" & 4 + (i - 2) * 5).Value
        End With
    Next i
    
    ' Another workbook can be active as long as this code is in the same workbook
    ' as the worksheets
    Set wb = ThisWorkbook
    '
    ' ...
    '
    For i = 2 To 9
        With wb.Worksheets(i)
            .Range("Z154").Value = wb.Worksheets(1).Range("Z" & 4 + (i - 2) * 5).Value
        End With
    Next i

  6. #6
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: I have 14,900 Lines of code in my Sub, Could someone show me how to be more efficient?

    Does this do the same thing as your code?
    Sub UpdateForm()
        Dim n As Long
        
        'Barnegat = 0
        'Bridgeton = 1
        'Galloway = 2
        'Hamonton = 3
        'Mantua = 4
        'Millville = 5
        'Ventnor = 6
        'Vineland = 7
        
        For n = 0 To 7
            Cells((n + 1) * 5, "N") = Cells((n + 1) * 5, "N") + Cells((n + 1) * 5, "B")
            Cells((n + 1) * 5, "O") = Cells((n + 1) * 5, "O") + Cells((n + 1) * 5, "C")
            Cells((n + 1) * 5, "P") = Cells((n + 1) * 5, "P") + Cells((n + 1) * 5, "D")
            Cells((n + 1) * 5, "Q") = Cells((n + 1) * 5, "Q") + Cells((n + 1) * 5, "E")
            Cells((n + 1) * 5, "R") = Cells((n + 1) * 5, "R") + Cells((n + 1) * 5, "F")
            Cells((n + 1) * 5, "S") = Cells((n + 1) * 5, "S") + Cells((n + 1) * 5, "G")
        Next
    End Sub
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: I have 14,900 Lines of code in my Sub, Could someone show me how to be more efficient?

    Or ...
        For i = 5 To 40 Step 5
            Rows(i).Range("B1:G1").Copy
            Rows(i).Range("N1:S1").PasteSpecial Paste:=xlPasteValue, _
                                                Operation:=xlPasteSpecialOperationAdd
        Next i
    Entia non sunt multiplicanda sine necessitate

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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