modification to Rgonzo's VBA earlier post

I had this question after viewing Vba to search in column B and if unique value is just one then return it to cell A1 of sheet1.


this code

gives error on .  if there one unqiue value in column B starting from B2 then it should return that unique value in Cell A1 of Sheet1

currenty it gives error.  runtime 13 sh1.Range("A1").Resize(UBound(aDict) + 1) = Application.Transpose(aDict)

Sub macro2()
Set objDict = CreateObject("Scripting.Dictionary")
Set sh1 = Sheets(1)
Set sh2 = Sheets(2)
Set Rng = Range(sh2.Range("B2"), sh2.Range("B" & Rows.Count).End(xlUp))
For Each c In Rng
    If WorksheetFunction.CountIf(Rng, c.Value) = 1 Then
        objDict.Add c.Value, c.Value
    End If
Next

aDict = objDict.Items
sh1.Range("A1").Resize(UBound(aDict) + 1) = Application.Transpose(aDict)
End Sub

Open in new window

LVL 6
FloraAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

pls try
Sub macro()
Set objDict = CreateObject("Scripting.Dictionary")
Set sh1 = Sheets(1)
Set sh2 = Sheets(2)
Set Rng = Range(sh2.Range("B2"), sh2.Range("B" & Rows.Count).End(xlUp))
For Each c In Rng
    If WorksheetFunction.CountIf(Rng, c.Value) = 1 Then
        objDict.Add c.Value, c.Value
    End If
Next
If objDict.Count > 1 Then
aDict = objDict.items
sh1.Range("A1").Resize(UBound(aDict) + 1) = Application.Transpose(aDict)
Else
aDict = objDict.items
sh1.Range("A1").Value = aDict(0)
End If
End Sub

Open in new window

Regards
1
FloraAuthor Commented:
getting error.

EE.png
0
FloraAuthor Commented:
subscript out of range.  while my sheet(1) exist.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Rgonzo1971Commented:
Could you send a dummy?
0
FloraAuthor Commented:
please see attached two dummy file.

one of the file is where column B only has one unique string

second example is where COlumn B has multiple unique string
Example-with-Multiple-Unique-value.xlsb
Example-with-One-Unique-value.xlsb
0
Rgonzo1971Commented:
then try
Sub macro()
Set objDict = CreateObject("Scripting.Dictionary")
Set sh1 = Sheets(1)
Set sh2 = Sheets(2)
For Each c In Range(sh2.Range("B2"), sh2.Range("B" & Rows.Count).End(xlUp))
    If Not objDict.Exists(c.Value) Then objDict.Add c.Value, c.Value
Next

aDict = objDict.Items
sh1.Range("A1").Resize(UBound(aDict) + 1) = Application.Transpose(aDict)
End Sub

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
FloraAuthor Commented:
thanks very much. i love the best of use of dictionary script you have used to solve the problem. it is  very fast.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Office

From novice to tech pro — start learning today.