vba require to populate array with a row position

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
984
Office Version
  1. 2010
Platform
  1. Windows
Hello.
I have a dynamic array at B2:G, and I am looking forward for code able to populate a new array on S:X base on the row position of every number by row.
let me explain.
1594866787933.png

in this images for example
The number 1332 if I count from the next row, then this number was four rows away, and number four will be place on S2.
The next number 13320 also count it from the next row, was three rows away, then number 3 will be at T2 and so on for every number on the array until the lastRow.
so every time the count number will populate a new array on S:X
so here it is the actual numbers
<tbody>
1332​
13320​
15984​
17316​
25308​
31968​
4662​
7992​
8658​
12654​
17316​
29970​
3996​
9990​
11988​
13320​
26640​
27306​
7992​
13320​
15318​
24642​
31968​
33966​
1332​
2664​
3996​
7326​
10656​
25308​
17316​
21978​
25308​
27972​
29304​
32634​
12654​
28638​
29970​
30636​
31968​
33300​
3330​
5328​
15318​
16650​
18648​
23310​
6660​
15318​
17982​
21312​
28638​
33300​
1332​
19980​
21312​
27306​
33966​
34632​
17982​
21312​
23976​
25308​
28638​
33300​
1998​
5994​
9324​
13320​
14652​
27972​
666​
7992​
11988​
15318​
15984​
29970​
11988​
13986​
17982​
20646​
25308​
34632​
3996​
11988​
14652​
21312​
23310​
33966​
17982​
22644​
27306​
29970​
31968​
33300​
5994​
6660​
14652​
17982​
21312​
29304​
3330​
9990​
11988​
22644​
24642​
31302​
3996​
15318​
21312​
21978​
27972​
32634​
10656​
19980​
25308​
28638​
33966​
35298​
3996​
17316​
25308​
27306​
27972​
31302​
4662​
10656​
16650​
21978​
28638​
30636​
3996​
7992​
10656​
17982​
19980​
29304​
6660​
12654​
13320​
16650​
19314​
25308​
5328​
15318​
16650​
22644​
32634​
35298​
1332​
3330​
4662​
20646​
24642​
28638​
7992​
11988​
18648​
22644​
26640​
35298​
666​
7992​
21978​
24642​
26640​
33966​
6660​
11322​
11988​
13320​
19980​
27306​
666​
3330​
5994​
15318​
15984​
31302​
10656​
14652​
21978​
24642​
26640​
27306​
4662​
7326​
9324​
9990​
15318​
25308​
23310​
24642​
25974​
30636​
31968​
34632​
1998​
9324​
14652​
31302​
33300​
35298​
7992​
8658​
25974​
27306​
29304​
33300​
1998​
4662​
26640​
27972​
30636​
33966​
666​
1332​
8658​
20646​
21978​
35298​
666​
8658​
17982​
21978​
27306​
33966​
</tbody>
I was using this code but it is not what I need, it is not real practical for the application I am working on
VBA Code:
Sub Left_to_right_count()
Dim Rng As Range, Ac&, Rw&
Dim Ray
Dim Q
Set Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp)).Resize(, 6)
               Ray = Rng.Value
                           With CreateObject("scripting.dictionary")
                                       .CompareMode = vbTextCompare
                                       ReDim nRay(1 To UBound(Ray, 1), 1 To UBound(Ray, 2))
                                                   For Rw = 1 To UBound(Ray, 1)
                                                                  For Ac = 1 To UBound(Ray, 2)
                                                                                 If Not .Exists(Ray(Rw, Ac)) Then
                                                                                             .Add Ray(Rw, Ac), Array(Rw, Ac, (Rw - 1) * UBound(Ray, 2))
                                                                                             Else
                                                                                             Q = .Item(Ray(Rw, Ac))
                                                                                             nRay(Q(0), Q(1)) = ((Rw - 1) * UBound(Ray, 2) + Ac) - Q(2)
                                                                                             Q(2) = (Rw - 1) * UBound(Ray, 2)
                                                                                             Q(0) = Rw
                                                                                             Q(1) = Ac
                                                                                             .Item(Ray(Rw, Ac)) = Q
                                                                                 End If
                                                                  Next Ac
                                                   Next Rw
                                       Range("H1").Resize(UBound(Ray, 1), UBound(Ray, 2)) = nRay
                           End With
End Sub
this code count from left to right until found the number and replace in the next array, but this is exactly what I don't need.
thanks.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
According to your image, the data starts in cell L2, if not, the code can be adapted.
Try this.

VBA Code:
Sub PopulatePosition()
  Dim i As Long, j As Long, lr As Long
  Dim r As Range, f As Range

  lr = Range("L" & Rows.Count).End(3).Row
  For i = 2 To lr - 1
    Set r = Range("L" & i + 1 & ":Q" & lr)
    For j = Columns("L").Column To Columns("Q").Column
      Set f = r.Find(Cells(i, j), , xlValues, xlWhole)
      If Not f Is Nothing Then
        Cells(i, j + 7) = f.Row - i
      End If
    Next
  Next
End Sub

Results:
Dante Amor
LMNOPQRSTUVWX
1
2133213320159841731625308319684212143
346627992865812654173162997020233545
43996999011988133202664027306215101247
5799213320153182464231968339669841436
61332266439967326106562530851027151
71731621978253082797229304326341513561113
812654286382997030636319683330017261592
933305328153181665018648233101017114197
1066601531817982213122863833300842122
111332199802131227306339663463216101654
1217982213122397625308286383330034395
13199859949324133201465227972225201237
14666799211988153181598429970151026173
151198813986179822064625308346321212619
16399611988146522131223310339664322185
171798222644273062997031968333001251718
18599466601465217982213122930413714626
1933309990119882264424642313028149783
203996153182131221978279723263426326
21106561998025308286383396635298231285
2239961731625308273062797231302238159
23466210656166502197828638306364126411
2439967992106561798219980293044815612
25666012654133201665019314253085518
2653281531816650226443263435298522
271332333046622064624642286381146112
28799211988186482264426640352981217
29666799221978246422664033966273338
30666011322119881332019980273062
3166633305994153181598431302724
3210656146522197824642266402730636254
334662732693249990153182530842
3423310246422597430636319683463223
351998932414652313023330035298213
36799286582597427306293043330023
3719984662266402797230636339662
3866613328658206462197835298111
39666865817982219782730633966
Hoja4
 
Upvote 0
Hello, thank you DanteAmor for your reply but what I got is an extra column,
1594896246036.png


VBA Code:
Sub PopulatePosition()
  Dim i As Long, j As Long, lr As Long
  Dim r As Range, f As Range

  lr = Range("B" & Rows.Count).End(3).Row
  For i = 2 To lr - 1
    Set r = Range("B" & i + 1 & ":k" & lr)
    For j = Columns("B").Column To Columns("k").Column
      Set f = r.Find(Cells(i, j), , xlValues, xlWhole)
      If Not f Is Nothing Then
        Cells(i, j + 7) = f.Row - i
      End If
    Next
  Next
End Sub
I change L for K trying to get the new position but, wrong move, beside when I try in the original sheet, I have extra column after G and this code was reading everything instead of just the array ("B2:G"),
please can you check for me
this time I upload the image according to my sheet B2: until the end G thanks.
 
Upvote 0
Set r = Range("B" & i + 1 & ":k" & lr)
For j = Columns("B").Column To Columns("k").Column
Must be G

Try this
VBA Code:
Sub PopulatePosition()
  Dim i As Long, j As Long, lr As Long
  Dim r As Range, f As Range
  
  lr = Range("B" & Rows.Count).End(3).Row
  For i = 2 To lr - 1
    Set r = Range("B" & i + 1 & ":G" & lr)
    For j = Columns("B").Column To Columns("G").Column
      Set f = r.Find(Cells(i, j), , xlValues, xlWhole)
      If Not f Is Nothing Then
        Cells(i, j + 7) = f.Row - i
      End If
    Next
  Next
End Sub
 
Upvote 0
DanteAmor, Thank you so much for your help.
for me and I know for so many people here you are a Microsoft Most Valuable Professional, you really deserve to be MVP.
Thank you, you really know programming and you help anybody with any problem, I hope the best for you
My dear MVP.
 
Upvote 0
I'm glad to help you. I appreciate your kind comments.
 
Upvote 0

Forum statistics

Threads
1,215,510
Messages
6,125,237
Members
449,217
Latest member
Trystel

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