Eliminating For Loops by passing variables in procedures

Prevost

Board Regular
Joined
Jan 23, 2014
Messages
198
Hi there, I am trying to make a program which filters out selections based on inputs and in the end, will come back with a list of possibilities (specifically it is for motors so they enter in whatever they would like, RPM or Frame etc.., and then it gets a price). I have 4 comboboxes that are populated with named ranges and the user chooses values from each. Then my code starts with the first combobox value and returns all the row numbers of the motors that contain that value into an array. I then use that array to get the row numbers of the motors that I am checking the remaining parameter values. I want to just call a procedure instead of writing these for loops out again and again. Here's my code:


Code:
Private Sub UserForm_Initialize()

    Dim NoWindings As Range
    Set NoWindings = Range("nowindings")
    
    'one speed options
    Dim OSHP As Range
    Set OSHP = Range("OSHP")
    Dim OSRPM As Range
    Set OSRPM = Range("osrpm")
    Dim OSEnclosure As Range
    Set OSEnclosure = Range("osenclosure")
    Dim OSFrame As Range
    Set OSFrame = Range("osframe")
    Dim OSOption As Range
    Set OSOption = Range("osoption")
    
    NoWindingsBox.RowSource = "nowindings"
    
    'populate comboboes with ranges
    HPOSBox.RowSource = "oshp"
    RPMOSBox.RowSource = "osrpm"
    EnclosureOSBox.RowSource = "osenclosure"
    FrameOSBox.RowSource = "osframe"
    OptionOSBox.RowSource = "osoption"
    
End Sub

Code:
Private Sub CommandButton1_Click()
    Dim Cell As Range, FindRange As Range
    Dim RPM, Enclosure, FrameSize, FrameOption, HP
    Dim Array1(), Array2(), Array3()
    Dim i As Integer, n  As Integer, x As Integer
    Dim OSMotorCosts As Worksheet
    Set OSMotorCosts = Worksheets("1 Speed Motor Costs")
        
        HP = HPOSBox.Value
        RPM = RPMOSBox.Value
        Enclosure = EnclosureOSBox.Value
        FrameSize = FrameOSBox.Value
        FrameOption = OptionOSBox.Value
                
            'check for matching hp
            n = 0
            x = OSMotorCosts.Range("a65536").End(xlUp).Row
            For i = 3 To x
                If Cells(i, 1).Text = HP Then
                    n = n + 1
                    ReDim Preserve Array1(n)
                    Array1(n) = Cells(i, 1).Row
                End If
            Next i
            
            'check for matching hp & rpm
            Call CreateRowArrayOptions(UBound(Array1, 1), 0, 1, Array1, Array2, RPM, 8)   ' this is what I am trying to insert
            
            'these for loops below are what I am trying to eliminate
            n = 0
            x = UBound(Array1, 1)
            For i = 1 To x
                If Cells(Array1(i), 8) = RPM Then
                    n = n + 1
                    ReDim Preserve Array2(n)
                    Array2(n) = Cells(Array1(i), 8).Row
                End If
            Next i

            'check for matching hp & rpm & enclosure
            n = 0
            x = UBound(Array2, 1)
            For i = 1 To x
                If Cells(Array2(i), 4) = Enclosure Then
                    n = n + 1
                    ReDim Preserve Array3(n)
                    Array3(n) = Cells(Array2(i, 1), 4).Row
                End If
            Next i
            n = 0
End Sub

'this is the procedure that I would like to pass into the CommandButton1_Click() Event
Code:
Private Sub CreateRowArrayOptions(x As Integer, n As Integer, i As Integer, ArrayOld(), ArrayNew(), Parameter, ColumnNo As Integer)
    n = 0
    For i = 1 To x
        If Cells(ArrayOld(i, 1), ColumnNo) = Parameter Then
            n = n + 1
            ReDim ArrayNew(n, 1)
            ArrayNew(n, 1) = Cells(ArrayOld(i, 1), ColumnNo).Row
        End If
    Next i
End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top