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.
Bookmarks