adding new values if not exist

Forestq

Active Member
Joined
May 9, 2010
Messages
482
Hi,

I have simple loop:
Code:
For i = 1 To x_country        
For j = 1 To x_rows_data
            If wsS.Range("BM" & i).Value = wsSD.Range("F" & j).Value Then
                'check if is on list
                k = k + 1
                        wsS.Range("BP" & k).Value = wsSD.Range("B" & j).Value
            End If
        Next j
Next i

I`m checking if value BM is on list F. If "yes" then I copy value B to column BP.
On the list values B can exist few times. How to copy only one time value B? (I dont want to have duplicates)
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Hi,

Not sure. This might work:
Code:
Dim dic As Object
Set dic = CreateObject("Scripting.Dictionary")

For i = 1 To x_country        
For j = 1 To x_rows_data
    If wsS.Range("BM" & i).Value = wsSD.Range("F" & j).Value Then
        If not dic.Exists(Cstr(wsS.Range("BM" & i).Value)) Then
            dic.Add CStr(wsS.Range("BM" & i).Value), ""
            k = k + 1
            wsS.Range("BP" & k).Value = wsSD.Range("B" & j).Value
        End If
    End If
Next j
Next i
 
Upvote 0
Hi xenou,

something is wrong.

I add one more loop and now is fine:
Code:
For m = 1 To x_country_1                          

 If wsSD.Range("B" & j).Value = wsS.Range("BP" & m).Value Then
   account_true = account_true + 1
 End If
Next m
                
                    If account_true = 0 Then
                        k = k + 1
                        wsS.Range("BO" & k).Value = wsS.Range("BM" & i).Value
                        wsS.Range("BP" & k).Value = wsSD.Range("B" & j).Value
                    End If

thank you!
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,906
Members
452,366
Latest member
TePunaBloke

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