+ Reply to Thread
Results 1 to 4 of 4

Simplify Excel VB code

Hybrid View

  1. #1
    Registered User
    Join Date
    09-03-2006
    Posts
    8

    Simplify Excel VB code

    Hello all, I am hoping for someone to examine my code below and advise on how to make it run more efficiently. Thanks in advance.

    Sub sFind()
    On Error Resume Next
    
    Dim n As Integer
    Dim lineRow1 As Integer
    Dim lineRow2 As Integer
    Dim lineRow3 As Integer
    Dim lineRow4 As Integer
    Dim lineRow5 As Integer
    
    Dim sLineNo As String
    
    n = 1
    
    Range("D5").Select
    Do
            Cells.Find(What:="Line-" & n, After:=ActiveCell, LookIn:=xlValues, _
            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False).Activate
            
            sLineNo = ActiveCell.Value
                
                Select Case sLineNo
                    Case "Line-1"
                          lineRow1 = ActiveCell.Row
                    Case "Line-2"
                          lineRow2 = ActiveCell.Row
                    Case "Line-3"
                          lineRow3 = ActiveCell.Row
                    Case "Line-4"
                          lineRow4 = ActiveCell.Row
                    Case "Line-5"
                          lineRow5 = ActiveCell.Row
               End Select
    n = n + 1
    Loop Until n >= 5
     
    nN = 1
    Range("D5").Select
    Do
            Cells.Find(What:="Line-" & nN, After:=ActiveCell, LookIn:=xlValues, _
            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False).Activate
            
    		sLineNo = ActiveCell.Value
                
                Select Case sLineNo
                    Case "Line-1"
                        Range("A" & lineRow1 :A" & lineRow2 - 1).Formula = 1stFormula
                    Case "Line-2"
                        Range("A" & lineRow2 :A" & lineRow3 - 1).Formula = 2ndFormula
                    Case "Line-3"
                        Range("A" & lineRow3 :A" & lineRow4 - 1).Formula = 3rdFormula
                    Case "Line-4"
                        Range("A" & lineRow4 :A" & lineRow5 - 1).Formula = 4thFormula
                                
                End Select
    nN = nN + 1
    Loop Until nN >= 5
    
    End Sub

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

    Re: Simplify Excel VB code

    It would help if you'd tell us what you're trying to do.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    09-03-2006
    Posts
    8

    Re: Simplify Excel VB code

    The code needs to loop through all the Production Lines (1 to 15) – between the Prod’ Lines is a list of orders (amount of orders varies) I need to programmatically copy a different formula dependent on the Prod’ Line.
    So between Prod’1 and Prod’2 will be Formula 1, between Prod’2 & Prod’3 will be formula 2, etc etc.
    I hope this makes sense, my VB code works but as I was hoping make it simpler.

  4. #4
    Registered User
    Join Date
    02-17-2009
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    34

    Re: Simplify Excel VB code

    Deleted code in red
    Added code in blue

    Sub sFind()
    On Error Resume Next
    
    Dim lr(5)
    Dim f(4)
    f(1) = 1stFormula
    f(2) = 2ndFormula
    f(3) = 3rdFormula
    f(4) = 4thFormula
    
    
    Dim n As Integer
    Dim lineRow1 As Integer
    Dim lineRow2 As Integer
    Dim lineRow3 As Integer
    Dim lineRow4 As Integer
    Dim lineRow5 As Integer
    
    Dim sLineNo As String
    n = 1
    Range("D5").Select
    
    Do
    For n = 1 to 5
    lr(n) = Cells.Find(What:="Line-" & n, After:=ActiveCell, LookIn:=xlValues, _
            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False).Row
    
            If n > 1 then Range(Cells(lr(n-1),1),Cells(lr(n)-1,1)).formula = f(n-1)
    Next
            
            sLineNo = ActiveCell.Value
                
                Select Case sLineNo
                    Case "Line-1"
                          lineRow1 = ActiveCell.Row
                    Case "Line-2"
                          lineRow2 = ActiveCell.Row
                    Case "Line-3"
                          lineRow3 = ActiveCell.Row
                    Case "Line-4"
                          lineRow4 = ActiveCell.Row
                    Case "Line-5"
                          lineRow5 = ActiveCell.Row
               End Select
    n = n + 1
    Loop Until n >= 5 
    
    nN = 1
    Range("D5").Select
    Do        Cells.Find(What:="Line-" & nN, After:=ActiveCell, LookIn:=xlValues, _
            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False).Activate
            
    		sLineNo = ActiveCell.Value      
    
                 Select Case sLineNo
                    Case "Line-1"
                        Range("A" & lineRow1 :A" & lineRow2 - 1).Formula = 1stFormula
                    Case "Line-2"
                        Range("A" & lineRow2 :A" & lineRow3 - 1).Formula = 2ndFormula
                    Case "Line-3"
                        Range("A" & lineRow3 :A" & lineRow4 - 1).Formula = 3rdFormula
                    Case "Line-4"
                        Range("A" & lineRow4 :A" & lineRow5 - 1).Formula = 4thFormula
                                
                End Select
          
    nN = nN + 1
    Loop Until nN >= 5
    
    End Sub
    Last edited by dustycrockett; 03-16-2009 at 08:51 PM.

+ Reply to Thread

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