VBA: looping in an array and select the corresponding value

Nelson78

Well-known Member
Joined
Sep 11, 2017
Messages
526
Office Version
  1. 2007
Hello everybody.

I've a situation as follows.

https://imgur.com/HLpoOUH


Then, in sheet "Data" I have two values:
cell A2 = 7210210
cell A3 = 7210214

Now, I've to loop in this array and write in B2 = 2958 (corresponding to 7210210) and in B3 = 1020 (corresponding to 7210214).

How can I perform this task?


Thank's in advance.
 
Ok, try
Code:
Sub Nelson78()
   
   Dim vData As Variant
   Dim vOutput() As Variant
   Dim vKey
   Dim dicCount As Object
   Dim i As Long, j As Long
   Dim lr As Long
   
   Set dicCount = CreateObject("scripting.dictionary")

   With Sheets("pcode")
      lr = .Cells(Rows.count, "H").End(xlUp).Row
      vData = .Range("D2:K" & lr).Value2
      
      For i = LBound(vData, 1) + 1 To UBound(vData, 1)
         If Not dicCount.Exists(vData(i, 8)) Then dicCount.Add vData(i, 8), CreateObject("scripting.dictionary")
         dicCount(vData(i, 8))(vData(i, 7)) = dicCount(vData(i, 8))(vData(i, 7)) + 1
      Next i
      
      ReDim vOutput(1 To dicCount.count, 1 To 2)
      i = 1
      For Each vKey In dicCount.Keys
         vOutput(i, 1) = vKey
         vOutput(i, 2) = dicCount(vKey).count
         i = i + 1
      Next vKey
   End With
End Sub
 
Upvote 0

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Forgot the original question.
Try
Code:
Sub Nelson78()
   
   Dim vData As Variant
   Dim dicCount As Object
   Dim i As Long
   Dim lr As Long
   
   Set dicCount = CreateObject("scripting.dictionary")

   With Sheets("[COLOR=#ff0000]Sheet1[/COLOR]")
      lr = .Cells(Rows.count, "H").End(xlUp).Row
      vData = .Range("D2:K" & lr).Value2
   End With
   For i = LBound(vData, 1) + 1 To UBound(vData, 1)
      If Not dicCount.Exists(vData(i, 8)) Then dicCount.Add vData(i, 8), CreateObject("scripting.dictionary")
      dicCount(vData(i, 8))(vData(i, 1)) = Empty
   Next i
   With Sheets("[COLOR=#ff0000]Summary[/COLOR]")
      .Range("B1").Value = dicCount(.Range("A1").Value).count
      .Range("B2").Value = dicCount(.Range("A2").Value).count
   End With
End Sub
Change names in red to suit
 
Upvote 0
Forgot the original question.
Try
Code:
Sub Nelson78()
   
   Dim vData As Variant
   Dim dicCount As Object
   Dim i As Long
   Dim lr As Long
   
   Set dicCount = CreateObject("scripting.dictionary")

   With Sheets("[COLOR=#ff0000]Sheet1[/COLOR]")
      lr = .Cells(Rows.count, "H").End(xlUp).Row
      vData = .Range("D2:K" & lr).Value2
   End With
   For i = LBound(vData, 1) + 1 To UBound(vData, 1)
      If Not dicCount.Exists(vData(i, 8)) Then dicCount.Add vData(i, 8), CreateObject("scripting.dictionary")
      dicCount(vData(i, 8))(vData(i, 1)) = Empty
   Next i
   With Sheets("[COLOR=#ff0000]Summary[/COLOR]")
      .Range("B1").Value = dicCount(.Range("A1").Value).count
      .Range("B2").Value = dicCount(.Range("A2").Value).count
   End With
End Sub
Change names in red to suit

Something is wrong.

Code:
Run Time Error '424' object required

in line

Code:
.Range("B1").Value = dicCount(.Range("A1").Value).count
 
Last edited:
Upvote 0
Try
Code:
   With Sheets("Summary")
      If dicCount.Exists(.Range("A1").Value) Then .Range("B1").Value = dicCount(.Range("A1").Value).Count
      If dicCount.Exists(.Range("A2").Value) Then .Range("B2").Value = dicCount(.Range("A2").Value).Count
   End With
 
Upvote 0
Try
Code:
   With Sheets("Summary")
      If dicCount.Exists(.Range("A1").Value) Then .Range("B1").Value = dicCount(.Range("A1").Value).Count
      If dicCount.Exists(.Range("A2").Value) Then .Range("B2").Value = dicCount(.Range("A2").Value).Count
   End With

Well, now it works fine.

Thank's a lot.
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,198
Members
453,022
Latest member
RobertV1609

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