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.
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
If you would like help, then you need to do a bit more than post images of the locals window, which tell us virtually nothing.
 
Upvote 0
Ok.

Sheet "Sheet1" column K: I have a huge amount of rows with repeated and not repeated values, including 7210210 and 7210214.
Sheet "Sheet1" column D: same situation with repeated and not repeated values.

Sheet "data" cell A2 = 7210210
Sheet "data" cell A3 = 7210214

I have to fill:
Sheet "data" cell B2 = how many unique values in "Sheet1" column D for 7210210 in column K?
Sheet "data" cell B3 = how many unique values in "Sheet1" column D for 7210214 in column K?
 
Upvote 0
Are you trying to get a list of the unique values and the no of times they occur in columns D an K on Sheet1?

Or are you trying to find get a list of all the values in column D where the corresponding value in column K is 7210210 and similarly for 7210214?

Can you post some sample data?
 
Upvote 0
What code do you have?
And in what way doesn't it work?

Finally roughly how many rows do you have in Sheet1?
 
Upvote 0
Are you trying to get a list of the unique values and the no of times they occur in columns D an K on Sheet1?

Or are you trying to find get a list of all the values in column D where the corresponding value in column K is 7210210 and similarly for 7210214?

Can you post some sample data?

By sight, la situation is like this.

https://imgur.com/HgV7pxM

Rows populated: about 10k
 
Upvote 0
Any chance you can post your code?
 
Upvote 0
Any chance you can post your code?

Code:
sub count

    Dim vData As Variant
    Dim vOutput() As Variant
    Dim vKey
    Dim dicCount As Object
    Dim dicTemp As Object
    Dim i As Long, j As Long


Dim lr As Long
    lr = Worksheets("Sheet1").Cells(Rows.Count, "H").End(xlUp).row

    With Sheets("Sheet1").
vData = .Range("D2:K" & lr)
        
For i = LBound(vData, 1) + 1 To UBound(vData, 1)
        
Set dicCount = CreateObject("scripting.dictionary")
            If dicCount.Exists(vData(i, 8)) Then
                dicCount(vData(i, 8))(vData(i, 1)) = dicCount(vData(i, 8))(vData(i, 1)) + 1
            Else
                Set dicTemp = CreateObject("scripting.dictionary")
                dicTemp(vData(i, 1)) = 1
                Set dicCount(vData(i, 8)) = dicTemp
            End If
        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 sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,248
Members
452,623
Latest member
cliftonhandyman

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