subscript out of range with array to filling items are not existed

Mussa

Board Regular
Joined
Jul 12, 2021
Messages
245
Office Version
  1. 2019
  2. 2010
Hi
I want matching column BATCH NO(C) for sheet DATA with the same column for FIRST. so every item is missed in column B in FIRST should fill based on is existed in column B for sheet DATA after match items in column C between two sheets .
MATCH.xlsm
ABCDEF
1ITEMDEL NOBATCH NO ITEMTT1QTY
21CC-mm/17LL-1RRL10
32CC-mm/18LL-2TTY2
43CC-mm/19LL-3MMW5
54CC-mm/20LL-4NNW1
65CC-mm/21LL-5TRU10
76CC-mm/13SS-1LTR10
87CC-mm/14SS-2FG20
98CC-mm/15SS-1TRR20
109CC-mm/161cv3MNT20
FIRST


MATCH.xlsm
ABCDE
1ITEMDEL NOBATCH NO ITEMTT1
21CC-1CC-mm/12SS-1TRU
32CC-2CC-mm/13SS-1LTR
43CC-3CC-mm/14SS-2FG
54CC-4CC-mm/15SS-1TRR
65CC-5CC-mm/16SS-1LTR
76CC-6CC-mm/17LL-1RRL
87CC-7CC-mm/18LL-2TTY
98CC-8CC-mm/19LL-3MMW
109CC-9CC-mm/20LL-4NNW
1110CC-10CC-mm/21LL-5TRU
DATA



RESULT IN SHEET FIRST

MATCH.xlsm
ABCDEF
1ITEMDEL NOBATCH NO ITEMTT1QTY
21CC-6CC-mm/17LL-1RRL10
32CC-7CC-mm/18LL-2TTY2
43CC-8CC-mm/19LL-3MMW5
54CC-9CC-mm/20LL-4NNW1
65CC-10CC-mm/21LL-5TRU10
76CC-2CC-mm/13SS-1LTR10
87CC-3CC-mm/14SS-2FG20
98CC-4CC-mm/15SS-1TRR20
109CC-mm/161cv3MNT20
FIRST



this is what I have , but gives error subscript out of range
VBA Code:
c(n, 2) = a(i, 2)
VBA Code:
Sub test2()
    Dim a, b, c, txt As String, temp As String
    Dim i  As Long, ii As Long, n As Long
    a = Sheets("data").Cells(1).CurrentRegion.Value
    b = Sheets("first").Cells(1).CurrentRegion.Value
    c = b: n = 1
    For i = 2 To UBound(a, 1)
        If a(i, 2) = "" Then a(i, 2) = a(i - 2, 1)
        txt = a(i, 3)
      
            If txt = a(i, 3) Then
                n = n + 1
                If temp <> a(i, 2) Then c(n, 2) = a(i, 2): temp = a(i, 2)
               
            End If
       
    Next
       Sheets("first").Cells(1).CurrentRegion.Value = c

End Sub
hope somebody help
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
I was not able to correlate your macro to the described task... It looks more a sorting trial than e matching one...
Anyway, if you need to fill Sheets(FIRST) column B with the values picked from Sheet(Data) column B, my first option would be a formula based on Index & Match

If you prefer doing that via macro, try for example this code:
VBA Code:
Sub LookUUPP()
Dim DBArea As Range, DDelArea As Range
Dim FRows As Long, DRows As Long
Dim myMatch, I As Long
'
FRows = Sheets("FIRST").Range("A1").CurrentRegion.Rows.Count
DRows = Sheets("DATA").Range("A1").CurrentRegion.Rows.Count
Set DBArea = Sheets("DATA").Range("C1").Resize(DRows, 1)
Set DDelArea = Sheets("DATA").Range("B1").Resize(DRows, 1)
'
With Sheets("FIRST")
    For I = 1 To FRows
        If .Cells(I, 2) = "" Then
            myMatch = Application.Match(.Cells(I, 3), DBArea, False)
            If Not IsError(myMatch) Then
                .Cells(I, 2) = DDelArea.Cells(myMatch, 1)
            End If
        End If
    Next I
End With
End Sub
This will look in Sheets(FIRST)-column B; for each "empty" cell it will then search on Sheets(DATA) the corresponding "Batch No" and pick its DelNo
If column B in FIRST is not Empty then the current value will be kept

If you need a different logic then please explain
 
Upvote 0
Hi Anthony ,
it's great !
If you prefer doing that via macro
of course , but I have question may you add array to your code if the data become big . this can be fast & avoiding slowness when increase data
 
Upvote 0
Faster method.
Instead of Macro use the formula.
In B2 of FIRST sheet then copy down.
Excel Formula:
=IFERROR(INDEX(DATA!$B:$B,AGGREGATE(15,6,ROW(DATA!$B$2:$B$11)/(DATA!$C$2:$C$11=$C2),1)),"")
To avoid 0 format cells for Custom #
 
Upvote 0
I have question may you add array to your code if the data become big . this can be fast & avoiding slowness when increase data
If your data spans less that a thousand lines using arrays would be useless
Anyway, this works with arrays
VBA Code:
Sub LookUpArr()
Dim DBArr, DDelArr, FDelArr, FBArr
Dim DBArea As Range, DDelArea As Range
Dim FRows As Long, DRows As Long
Dim myMatch, I As Long
'
FRows = Sheets("FIRST").Range("A1").CurrentRegion.Rows.Count
DRows = Sheets("DATA").Range("A1").CurrentRegion.Rows.Count
DBArr = Sheets("DATA").Range("C1").Resize(DRows, 1).Value
DDelArr = Sheets("DATA").Range("B1").Resize(DRows, 1).Value
FBArr = Sheets("FIRST").Range("C1").Resize(FRows, 1).Value
FDelArr = Sheets("FIRST").Range("B1").Resize(FRows, 1).Value
'
For I = 1 To FRows
    If FDelArr(I, 1) = "" Then
        myMatch = Application.Match(FBArr(I, 1), DBArr, False)
        If Not IsError(myMatch) Then
            FDelArr(I, 1) = DDelArr(myMatch, 1)
        End If
    End If
Next I
Sheets("FIRST").Range("B1").Resize(UBound(FDelArr), 1) = FDelArr
End Sub
 
Upvote 0
Solution
If your data spans less that a thousand lines using arrays would be useless
that's right , but who knows maybe data could be big . so your code will deal with big data .
thanks very much for your assistance :)
 
Upvote 0

Forum statistics

Threads
1,216,143
Messages
6,129,110
Members
449,486
Latest member
malcolmlyle

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