VBA - Loop through drop down list and look for different values in corresponding column

ross1

New Member
Joined
Apr 26, 2020
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hi all,
I have a drop down list in cell O2 (color names). Depending on the color I select in the drop down list, column P2:P populates with company names.

Main Purpose -
For each color in the drop down list (O2), I want to make sure that there is only one Company Name attached to it. example for the color Red, Only Microsoft should be shown in column P (no matter how many times). It should not have IBM or any other company name.

1) I would like to create a macro that will automatically go through each color in the drop down list in cell O2.
2) For each color in Cell O2, the macro should check if the corresponding values in column P have different company names.
IF there is only one company name in the column P, THEN do nothing and continue on.
IF there is more than one company name in column P THEN, Copy the color name in cell O2 and paste it to cell S2. Continue on with the code until the very end of the drop down list, cell O2. If the code finds more colors that have multiple company names then copy that color and paste it to the next cell in column S, i.e cell S3, S4, S5 etc.

P.S. I am using a formula in column P that extracts values depending on the color selected in the drop down list. In order to keep the column P dynamic the formula uses IFERROR to blank out cells that give me a NUM error (IFERROR, formula, ""). I would like the macro to only look at company names (values) and ignore the blank cells.

This is the code I used to loop through the drop down list. I'm having trouble with the next part and then merging the whole thing together.

Sub LoopThroughDataValidationList()

Dim rng As Range
Dim dataValidationArray As Variant
Dim i As Integer
Dim rows As Integer
Dim d As Object, c As Variant, b As Long, lr As Long

'Set the cell which contains the Data Validation list
Set rng = Sheets("worksheet1").Range("O2")

'Create an array from the Data Validation formula, without creating
'a multi-dimensional array from the range

rows = Range(Replace(rng.Validation.Formula1, "=", "")).rows.Count
ReDim dataValidationArray(1 To rows)

For i = 1 To rows
dataValidationArray(i) = _
Range(Replace(rng.Validation.Formula1, "=", "")).cells(i, 1)
Next i

'Loop through all the values in the Data Validation Array
For i = LBound(dataValidationArray) To UBound(dataValidationArray)

'Change the value in the data validation cell
rng.Value = dataValidationArray(i)

Next i

End Sub


Thank you for your help.
 

Attachments

  • Untitled.png
    Untitled.png
    108.9 KB · Views: 14

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

Forum statistics

Threads
1,215,734
Messages
6,126,542
Members
449,316
Latest member
sravya

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