VBA help to Vlookup a single value in a list and return results while ignoring blank cells

MMC78

New Member
Joined
May 26, 2010
Messages
12
Hi,

I've been messing about with this and can't quite seem to get it right (I also think I'm making things much more complicated than they need to be!).

Below is an example of the data I have.....(the real data has many more ref and ID numbers). I want to macro this as this is a task I want to run daily, and the data will change on a day to day basis.

RefID1ID2Ref2
13051add data hereadd data hereadd data here
13242
1434, 5433
14569
13051265377432588
1
2126
2427
2512
2
2512
2569
2
2
2312
3324
3453, 478126
4
4569
4569
5
5377
5432
5432588

<tbody>
</tbody>


Take the list of Ref numbers from column (Ref) and remove duplicates in column (Ref2) as above.

Then for each Ref in Ref2 column run a lookup for that Ref in column (Ref) and return all IDs from columns (ID1 and ID2), while ignoring blanks and duplicates, and to paste them in the row for each ref (see above). I'm not fussed if the data is pasted in individual cells or is a string in a single cell - see column (Ref2) ref 4 and 5 above for a simple example of how this should look.
 
Last edited:

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hi,

I've been messing about with this and can't quite seem to get it right (I also think I'm making things much more complicated than they need to be!).

Below is an example of the data I have.....(the real data has many more ref and ID numbers). I want to macro this as this is a task I want to run daily, and the data will change on a day to day basis.

RefID1ID2Ref2
13051add data hereadd data hereadd data here
13242
1434, 5433
14569
13051265377432588
1
2126
2427
2512
2
2512
2569
2
2
2312
3324
3453, 478126
4
4569
4569
5
5377
5432
5432588

<tbody>
</tbody>


Take the list of Ref numbers from column (Ref) and remove duplicates in column (Ref2) as above.

Then for each Ref in Ref2 column run a lookup for that Ref in column (Ref) and return all IDs from columns (ID1 and ID2), while ignoring blanks and duplicates, and to paste them in the row for each ref (see above). I'm not fussed if the data is pasted in individual cells or is a string in a single cell - see column (Ref2) ref 4 and 5 above for a simple example of how this should look.

Anyone??/
 
Upvote 0
MMC78,

Not sure if the below code will deal with all eventualities but I believe it deals with your example data.

Code:
Sub RefIDs()


Range("G1") = "Ref2"
R2r = 1
lr = Cells(Rows.Count, "A").End(xlUp).Row
For Each Cell In Range("A2:A" & lr)
    If Cell <> PrevCell Then
        R2r = R2r + 1
        Range("G" & R2r) = Cell
        PrevCell = Cell
        NewRef = "Y"
        IDOset = 1
    End If
    
      For CellOset = 1 To 2
            If Not Cell.Offset(0, CellOset) = "" Then
             If NewRef = "Y" Then
                    Range("G" & R2r).Offset(0, IDOset) = Cell.Offset(0, CellOset)
                    PrevID = Cell.Offset(0, CellOset)
                     IDOset = IDOset + 1
                     NewRef = "N"
                     Else
              
                      If Not Cell.Offset(0, CellOset) = PrevID Then
                        Range("G" & R2r).Offset(0, IDOset) = Cell.Offset(0, CellOset)
                        PrevID = Cell.Offset(0, CellOset)
                        IDOset = IDOset + 1
                     End If
                End If
            End If
        
      Next CellOset
        
Next Cell


End Sub

Hope that helps.
 
Upvote 0
MMC78,

Not sure if the below code will deal with all eventualities but I believe it deals with your example data.

Code:
Sub RefIDs()


Range("G1") = "Ref2"
R2r = 1
lr = Cells(Rows.Count, "A").End(xlUp).Row
For Each Cell In Range("A2:A" & lr)
    If Cell <> PrevCell Then
        R2r = R2r + 1
        Range("G" & R2r) = Cell
        PrevCell = Cell
        NewRef = "Y"
        IDOset = 1
    End If
    
      For CellOset = 1 To 2
            If Not Cell.Offset(0, CellOset) = "" Then
             If NewRef = "Y" Then
                    Range("G" & R2r).Offset(0, IDOset) = Cell.Offset(0, CellOset)
                    PrevID = Cell.Offset(0, CellOset)
                     IDOset = IDOset + 1
                     NewRef = "N"
                     Else
              
                      If Not Cell.Offset(0, CellOset) = PrevID Then
                        Range("G" & R2r).Offset(0, IDOset) = Cell.Offset(0, CellOset)
                        PrevID = Cell.Offset(0, CellOset)
                        IDOset = IDOset + 1
                     End If
                End If
            End If
        
      Next CellOset
        
Next Cell


End Sub

Hope that helps.

Hi Snakehips,

Yes, that works great on my example data. I've tried it on the live data and it is working for most records, but no issue there as this is a great starting point for me so I can tidy up from here.

Thanks for you assistance!
 
Upvote 0

Forum statistics

Threads
1,216,109
Messages
6,128,883
Members
449,477
Latest member
panjongshing

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