Results 1 to 5 of 5

Removing Inefficent Code

Threaded View

  1. #1
    Registered User
    Join Date
    08-12-2010
    Location
    North Carolina, USA
    MS-Off Ver
    Excel 2007
    Posts
    25

    Removing Inefficent Code

    I have several instances of code that repeats for different variables, cells, and ranges. Being new to vba programming I need help in trying to condense and streamline the code that I am using if possible. This first code sample is repeated 5 times:
    dblMaxSpec = 0
            dblMaxSpec = dblBasisWeightMax
            dblMinSpec = 0
            dblMinSpec = dblBasisWeightMin
            
            With ActiveWorkbook.Worksheets(2)
            
                intLastRow = 0
                intLastRow = .Cells(Rows.Count, 1).End(xlUp).Row
                Set rCalculate = .Range(.Cells(3, 4), .Cells(intLastRow, 4))
            
                Call CalculateAveragesStDev
                
            End With
    
            .Cells(20, 2) = dblCalcAvg
            .Cells(20, 3) = dblCalcStDev
            .Cells(20, 4) = intCountMin
            .Cells(20, 5) = intCountMax
            
            With ActiveWorkbook.Worksheets(2)
            
                Set rCalculate = .Range(.Cells(3, 10), .Cells(intLastRow, 10))
            
                Call CalculateAveragesStDev
                
            End With
    
            .Cells(29, 2) = dblCalcAvg
            .Cells(29, 3) = dblCalcStDev
            .Cells(29, 4) = intCountMin
            .Cells(29, 5) = intCountMax
    This second code sample is repeated 10 times:
    dblMaxSpec = 0
            dblMaxSpec = dblBasisWeightMax
            dblMinSpec = 0
            dblMinSpec = dblBasisWeightMin
            
            With ActiveWorkbook.Worksheets(2)
            
                intLastRow = 0
                intLastRow = .Cells(Rows.Count, 1).End(xlUp).Row
                Set rCalculate = .Range(.Cells(3, 4), .Cells(intLastRow, 4))
            
                Call CalculateAveragesStDev
                
            End With
    
            .Cells(20, 2) = dblCalcAvg
            .Cells(20, 3) = dblCalcStDev
            .Cells(20, 4) = intCountMin
            .Cells(20, 5) = intCountMax
            
            With ActiveWorkbook.Worksheets(2)
            
                Set rCalculate = .Range(.Cells(3, 10), .Cells(intLastRow, 10))
            
                Call CalculateAveragesStDev
                
            End With
    
            .Cells(29, 2) = dblCalcAvg
            .Cells(29, 3) = dblCalcStDev
            .Cells(29, 4) = intCountMin
            .Cells(29, 5) = intCountMax
    I am also attaching a copy of the entire workbook. The above code is in the Sheet1, worksheet activate sub.
    Attached Files Attached Files
    Last edited by hchurch; 11-24-2010 at 02:37 PM. Reason: Issue Resolved

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