+ Reply to Thread
Results 1 to 3 of 3

Merge similar codes into one

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-07-2005
    Posts
    280

    Merge similar codes into one

    Hi everyone,

    I have the following seven codes used to fill a ListBox with data retrieved form a range (DACNRange).. they are almost the same .. the only difference between them is the range column number (highlighted with red) ..

    Instead of these seven similar codes, Can we create a general code replaces them all?


    Sub ByContNum()
    
    With DACNRange
        W = 0
        F = 1
        Do Until F = .Rows.Count + 1
            If InStr(1, .Cells(F, 1), Frame1.ActiveControl, 1) = 1 And .Cells(F, 4) = UserUnit Then
                ListBox1.AddItem .Cells(F, 1)
                ListBox1.List(W, 1) = MDF(.Cells(F, 2))
                ListBox1.List(W, 2) = .Cells(F, 7)
                ListBox1.List(W, 3) = .Cells(F, 5)
                ListBox1.List(W, 4) = .Cells(F, 6)
                ListBox1.List(W, 5) = .Cells(F, 9)
            W = W + 1
            End If
            F = F + 1
        Loop
    End With
    
    End Sub
    Sub ByDate()
    
    With DACNRange
        W = 0
        F = 1
        Do Until F = .Rows.Count + 1
            If InStr(1, .Cells(F, 2), MyDate, 1) = 1 And .Cells(F, 4) = UserUnit Then
                ListBox1.AddItem .Cells(F, 1)
                ListBox1.List(W, 1) = MDF(.Cells(F, 2))
                ListBox1.List(W, 2) = .Cells(F, 7)
                ListBox1.List(W, 3) = .Cells(F, 5)
                ListBox1.List(W, 4) = .Cells(F, 6)
                ListBox1.List(W, 5) = .Cells(F, 9)
            W = W + 1
            End If
            F = F + 1
        Loop
    End With
    
    End Sub
    Sub ByActType()
    
    With DACNRange
        W = 0
        F = 1
        Do Until F = .Rows.Count + 1
            If InStr(1, .Cells(F, 3), MyDate, 1) = 1 And .Cells(F, 4) = UserUnit Then
                ListBox1.AddItem .Cells(F, 1)
                ListBox1.List(W, 1) = MDF(.Cells(F, 2))
                ListBox1.List(W, 2) = .Cells(F, 7)
                ListBox1.List(W, 3) = .Cells(F, 5)
                ListBox1.List(W, 4) = .Cells(F, 6)
                ListBox1.List(W, 5) = .Cells(F, 9)
            W = W + 1
            End If
            F = F + 1
        Loop
    End With
    
    End Sub
    
    Sub ByTechName()
    
    With DACNRange
        W = 0
        F = 1
        Do Until F = .Rows.Count + 1
            If InStr(1, .Cells(F, 5), Frame1.ActiveControl, 1) = 1 And .Cells(F, 4) = UserUnit Then
                ListBox1.AddItem .Cells(F, 1)
                ListBox1.List(W, 1) = MDF(.Cells(F, 2))
                ListBox1.List(W, 2) = .Cells(F, 7)
                ListBox1.List(W, 3) = .Cells(F, 5)
                ListBox1.List(W, 4) = .Cells(F, 6)
                ListBox1.List(W, 5) = .Cells(F, 9)
            W = W + 1
            End If
            F = F + 1
        Loop
    End With
    
    End Sub
    Sub ByArea()
    
    With DACNRange
        W = 0
        F = 1
        Do Until F = .Rows.Count + 1
            If InStr(1, .Cells(F, 6), Frame1.ActiveControl, 1) = 1 And .Cells(F, 4) = UserUnit Then
                ListBox1.AddItem .Cells(F, 1)
                ListBox1.List(W, 1) = MDF(.Cells(F, 2))
                ListBox1.List(W, 2) = .Cells(F, 7)
                ListBox1.List(W, 3) = .Cells(F, 5)
                ListBox1.List(W, 4) = .Cells(F, 6)
                ListBox1.List(W, 5) = .Cells(F, 9)
            W = W + 1
            End If
            F = F + 1
        Loop
    End With
    
    End Sub
    Sub BySite()
    
    With DACNRange
        W = 0
        F = 1
        Do Until F = .Rows.Count + 1
            If InStr(1, .Cells(F, 7), Frame1.ActiveControl, 1) = 1 And .Cells(F, 4) = UserUnit Then
                ListBox1.AddItem .Cells(F, 1)
                ListBox1.List(W, 1) = MDF(.Cells(F, 2))
                ListBox1.List(W, 2) = .Cells(F, 7)
                ListBox1.List(W, 3) = .Cells(F, 5)
                ListBox1.List(W, 4) = .Cells(F, 6)
                ListBox1.List(W, 5) = .Cells(F, 9)
            W = W + 1
            End If
            F = F + 1
        Loop
    End With
    
    End Sub
    Sub BySub()
    
    With DACNRange
        W = 0
        F = 1
        Do Until F = .Rows.Count + 1
            If InStr(1, .Cells(F, 9), Frame1.ActiveControl, 1) = 1 And .Cells(F, 4) = UserUnit Then
                ListBox1.AddItem .Cells(F, 1)
                ListBox1.List(W, 1) = MDF(.Cells(F, 2))
                ListBox1.List(W, 2) = .Cells(F, 7)
                ListBox1.List(W, 3) = .Cells(F, 5)
                ListBox1.List(W, 4) = .Cells(F, 6)
                ListBox1.List(W, 5) = .Cells(F, 9)
            W = W + 1
            End If
            F = F + 1
        Loop
    End With
    
    End Sub
    I hope that my question is clear for all,

    Thanks,
    Last edited by LoveCandle; 06-09-2009 at 05:56 AM.

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Merge similar codes into one

    How do you call each procedure/ You need to create a Public Variable to replace the column number then set it with say the button click event
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Forum Contributor
    Join Date
    11-07-2005
    Posts
    280

    Re: Merge similar codes into one

    Hi,

    Thank you royUK for your advice, I actually reached the following solution:



    CBLI = ComboBox1.ListIndex
    CBLC = ComboBox1.ListCount
    
    If OptionButton2 Then MyTarget = 1
    If CBLI = 0 Then MyTarget = 2
    If CBLI = 1 Then MyTarget = 3
    If CBLI = 2 And CBLC = 6 Then MyTarget = 5
    If CBLI = 3 And CBLC = 6 Then MyTarget = 6
    If CBLI = 5 And CBLC = 6 Then MyTarget = 7
    If CBLI = 4 And CBLC = 6 Then MyTarget = 9
    
    With DACNRange
        W = 0
        F = 1
        Do Until F = .Rows.Count + 1
            If InStr(1, .Cells(F, MyTarget), Frame1.ActiveControl, 1) = 1 And .Cells(F, 4) = UserUnit Then
                ListBox1.AddItem .Cells(F, 1)
                ListBox1.List(W, 1) = MDF(.Cells(F, 2))
                ListBox1.List(W, 2) = .Cells(F, 7)
                ListBox1.List(W, 3) = .Cells(F, 5)
                ListBox1.List(W, 4) = .Cells(F, 6)
                ListBox1.List(W, 5) = .Cells(F, 9)
            W = W + 1
            End If
            F = F + 1
        Loop
    End With

+ 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