Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Optimize my code

Status
Not open for further replies.

jackeroo75

Technical User
Aug 26, 2007
34
US
I know I can do this better but I'm not sure how to optimize my code.
Code:
Const sWorkRel1 As String = "1-*"
Const sWorkRel2 As String = "3-*"
Const sWorkRel3 As String = "4-*"
Const sWorkRel4 As String = "49*"
Const sFloora As String = "11*"
Const sFloorb As String = "10*"
Const sFloorc As String = "9*"
Const sFloord As String = "8*"
Const sFloore As String = "7*"
Const sFloorf As String = "4*"
Const sFloorg As String = "3*"
Const sFloorh As String = "2*"
 
Dim rngMyRange As Range, rngCell As Range
Dim pagebreakcount As Integer, floor1 As Integer, floor2 As Integer, floor3 As Integer
Dim floor4 As Integer, floor7 As Integer, floor8 As Integer, floor9, floorall As Integer
    
Dim flag1 As Integer, flag2 As Integer, flag3 As Integer, flag4 As Integer
Dim flag7 As Integer, flag8 As Integer, flag9 As Integer, flagall As Integer, flagrjc As Integer
    
    
pagebreakcount = g_startRow + 1
With Worksheets("List")
  Set rngMyRange = .Range(.Cells(g_startRow + 1, 1), .Cells(endofRow - 1, 1))
  'the range to work with
  For Each rngCell In rngMyRange
  'loop through the range
    If (rngCell.Value Like sWorkRel1 Or rngCell.Value Like sWorkRel2 Or rngCell.Value Like sWorkRel3 Or rngCell.Value Like sWorkRel4) Then
      pagebreakcount = pagebreakcount + 1
      floorall = pagebreakcount
      flagall = 1
    
    ElseIf (rngCell.Value Like sFloora Or rngCell.Value Like sFloorb) Then
      pagebreakcount = pagebreakcount + 1
      floor1 = pagebreakcount
      flag1 = 1
    ElseIf (rngCell.Value Like sFloorh Or rngCell.Value Like sFloorg) Then
      pagebreakcount = pagebreakcount + 1
      floor2 = pagebreakcount
      flag2 = 1
    ElseIf rngCell.Value Like sFloorf Then
      pagebreakcount = pagebreakcount + 1
      floor4 = pagebreakcount
      flag4 = 1
    ElseIf rngCell.Value Like sFloore Then
      pagebreakcount = pagebreakcount + 1
      floor7 = pagebreakcount
      flag7 = 1
    ElseIf rngCell.Value Like sFloord Then
      pagebreakcount = pagebreakcount + 1
      floor8 = pagebreakcount
      flag8 = 1
    ElseIf rngCell.Value Like sFloorc Then
      pagebreakcount = pagebreakcount + 1
      floor9 = pagebreakcount
      flag9 = 1
    Else
      pagebreakcount = pagebreakcount + 1
      floorall = pagebreakcount
      flagall = 1
    End If
  Next
  If flag1 Then
    .HPageBreaks.Add Before:=Cells(floor1, 1)
  End If
  If flag2 Then
    .HPageBreaks.Add Before:=Cells(floor2, 1)
  End If
  If flag4 Then
    .HPageBreaks.Add Before:=Cells(floor4, 1)
  End If
  If flag7 Then
    .HPageBreaks.Add Before:=Cells(floor7, 1)
  End If
  If flag8 Then
    .HPageBreaks.Add Before:=Cells(floor8, 1)
  End If
  If flag9 Then
    .HPageBreaks.Add Before:=Cells(floor9, 1)
  End If
  If flagall Then
    .HPageBreaks.Add Before:=Cells(floorall, 1)
  End If
End With
 




I'd try using arrays and loops. It would reduce the LOC and maybe simplify the logic.



Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Use arrays and loops instead of the if else statement?
 
Use arrays and loops instead of the if else statement? Also, what is LOC?
 



Lines Of Code

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top