Convert Index and match formula sheet to vba macro

RAM1972

Board Regular
Joined
Jun 29, 2014
Messages
217
Hi All
I made a recorded macro, so if any one can help to put the recorded macro in an elegant vba way some modifcations to be made still to end of last row for both imput sheet column A and B as COLUMN C and D may have data of 1500 to 3500 rows to fill columns A and B of imput sheet.

For Product database same lookup from A,B,C.D to last row. (product database can read up to 30000 rows)
recorded macro has been for short range.as below

Formula not to appear in cells.

As below sample of sheet + formula

and recorded macro

Imput sheet
[TABLE="width: 1648"]
<colgroup><col span="2"><col><col></colgroup><tbody>[TR]
[TD]Coding Reference
[/TD]
[TD]Coding Description[/TD]
[TD]Product Code [/TD]
[TD]Product Description[/TD]
[/TR]
[TR]
[TD] =IFERROR(INDEX('Product Database '!$A:$D,MATCH('Imput Datasheet'!C2,'Product Database '!C:C,0),1),"")[/TD]
[TD] =IFERROR(INDEX('Product Database '!$A:$D,MATCH('Imput Datasheet'!C2,'Product Database '!C:C,0),2),"")[/TD]
[TD]0062531[/TD]
[TD]BAC 30L NOIR[/TD]
[/TR]
</tbody>[/TABLE]

Product database
[TABLE="width: 775"]
<colgroup><col><col><col><col></colgroup><tbody>[TR]
[TD]Coding Reference
[/TD]
[TD]Coding Description[/TD]
[TD]Product Code [/TD]
[TD]Product Description[/TD]
[/TR]
[TR]
[TD]83119000[/TD]
[TD]WELDING ELECTRODES[/TD]
[TD]0046371[/TD]
[TD]POUR ASSEMBLAGE TRAVAUX COURANTS (ZINGUERIE, FERBLANTERIE) -[/TD]
[/TR]
</tbody>[/TABLE]

Sub INDEXMATCHIFERROETEST()
'
' INDEXMATCHIFERROETEST Macro
'

'
Range("A2").Select
ActiveCell.FormulaR1C1 = _
"=IFERROR(INDEX('Product Database '!C1:C4,MATCH('Imput Datasheet'!RC[2],'Product Database '!C[2],0),1),"""")"
Range("A2").Select
Selection.AutoFill Destination:=Range("A2:B2"), Type:=xlFillDefault ***(this need to modify to last row of data)***
Range("A2:B2").Select
Range("B2").Select
ActiveCell.FormulaR1C1 = _
"=IFERROR(INDEX('Product Database '!C1:C4,MATCH('Imput Datasheet'!RC[1],'Product Database '!C[2],0),2),"""")"
Range("A2:B2").Select
Selection.AutoFill Destination:=Range("A2:B32")
Range("A2:B32").Select
Columns("A:A").ColumnWidth = 19.29:confused:
Range("B2").Select
ActiveCell.FormulaR1C1 = _
"=IFERROR(INDEX('Product Database '!C1:C4,MATCH('Imput Datasheet'!RC[1],'Product Database '!C[1],0),2),"""")"
Range("A2:B2").Select
Selection.AutoFill Destination:=Range("A2:B32")
Range("A2:B32").Select
Columns("B:B").EntireColumn.AutoFit
Selection.AutoFill Destination:=Range("A2:B35"), Type:=xlFillDefault***(this need to modify to last row of data)***
Range("A2:B35").Select
End Sub
 
Last edited:
Try:
Code:
Sub M1()

    Dim x       As Long
    Dim arr()   As Variant
    Dim temp    As Variant
    Dim ws1     As Worksheet
    Dim ws2     As Worksheet
    Dim dic     As Object
    
    Const delim As String = "|"
    
    Set ws1 = Sheets("Imput Datasheet")
    Set ws2 = Sheets("Product DataBase ")
    Set dic = CreateObject("Scripting.Dictionary")

    Application.ScreenUpdating = False
    
    With ws2
        For x = 1 To .Cells(.rows.count, 3).End(xlUp).row
            dic(.Cells(x, 3).value) = .Cells(x, 1).value & delim & .Cells(x, 2).value & delim & .Cells(x, 4).value
        Next x
    End With
    
    With ws1
        x = .Cells(.rows.count, 3).End(xlUp).row
        arr = .Cells(2, 1).Resize(x - 1, 4).value
        
        For x = LBound(arr, 1) To UBound(arr, 1)
            If dic.exists(.Cells(x + 1, 3).value) Then
                temp = Split(dic(.Cells(x + 1, 3).value), delim)
                arr(x, 1) = temp(0)
                arr(x, 2) = temp(1)
                arr(x, 4) = temp(2)
                Erase temp
            Else
                arr(x, 1) = Empty
                arr(x, 2) = Empty
                arr(x, 4) = Empty
            End If
        Next x
        
        .Cells(2, 1).Resize(UBound(arr, 1), UBound(arr, 2)).value = arr
    End With
    
    Application.ScreenUpdating = True
    
    Set dic = Nothing
    Set ws1 = Nothing
    Set ws2 = Nothing
    Erase arr
    
End Sub
 
Upvote 0
Hi
Try the code, it works,but the oops,is where there is no matching record of column A and B , it remains blank this ok , but the code also removes the data in column D also. there is only data in column C.

It should be Column A and B blank and Column C and D should appeared.

Actually it updates but removes data in column D also when there is no match.

Actually with code

thanks if you could adjust

[TABLE="width: 821"]
<colgroup><col><col><col><col></colgroup><tbody>[TR]
[TD]Coding Reference [/TD]
[TD]Coding Description[/TD]
[TD]Product Code [/TD]
[TD]Product Description[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD="align: right"]62531[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD="align: right"]62531[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]83119000[/TD]
[TD]WELDING ELECTRODES[/TD]
[TD="align: right"]46371[/TD]
[TD]POUR ASSEMBLAGE TRAVAUX COURANTS (ZINGUERIE, FERBLANTERIE) -[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD="align: right"]46373[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD="align: right"]3852[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD="align: right"]46380[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

With formula


[TABLE="width: 821"]
<colgroup><col><col><col><col></colgroup><tbody>[TR]
[TD]Coding Reference [/TD]
[TD]Coding Description[/TD]
[TD]Product Code [/TD]
[TD]Product Description[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]0062531[/TD]
[TD]BAC 30L NOIR[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]0062531[/TD]
[TD]BAC 30L NOIR[/TD]
[/TR]
[TR]
[TD="align: right"]83119000[/TD]
[TD]WELDING ELECTRODES[/TD]
[TD]0046371[/TD]
[TD]POUR ASSEMBLAGE TRAVAUX COURANTS (ZINGUERIE, FERBLANTERIE) -[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]0046373[/TD]
[TD]POUR ASSEMBLAGE RADIO-ELECTRICITE - 50% D’ETAIN - AME DECAPA[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]0003852[/TD]
[TD]PANNE POUR FER A SOUDER EXPRESS 159[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]0046380[/TD]
[TD]FER A SOUDER GALAXY 25 W[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
Try:
Code:
Sub M1()


    Dim x       As Long
    Dim arr()   As Variant
    Dim temp    As Variant
    Dim ws1     As Worksheet
    Dim ws2     As Worksheet
    Dim dic     As Object
    
    Const delim As String = "|"
    
    Set ws1 = Sheets("Imput Datasheet")
    Set ws2 = Sheets("Product DataBase ")
    Set dic = CreateObject("Scripting.Dictionary")


    Application.ScreenUpdating = False
    
    With ws2
        For x = 1 To .Cells(.Rows.Count, 3).End(xlUp).Row
            dic(.Cells(x, 3).Value) = .Cells(x, 1).Value & delim & .Cells(x, 2).Value
        Next x
    End With
    
    With ws1
        x = .Cells(.Rows.Count, 3).End(xlUp).Row
        arr = .Cells(2, 1).Resize(x - 1, 4).Text
        
        For x = LBound(arr, 1) To UBound(arr, 1)
            If dic.exists(.Cells(x + 1, 3).Text) Then
                temp = Split(dic(.Cells(x + 1, 3).Text), delim)
                arr(x, 1) = temp(0)
                arr(x, 2) = temp(1)
                Erase temp
            Else
                arr(x, 1) = Empty
                arr(x, 2) = Empty
            End If
        Next x
        
        .Cells(2, 1).Resize(UBound(arr, 1), UBound(arr, 2)).Text = arr
    End With
    
    Application.ScreenUpdating = True
    
    Set dic = Nothing
    Set ws1 = Nothing
    Set ws2 = Nothing
    Erase arr
    
End Sub
 
Upvote 0
Oops getting

running error mismatch 13

arr = .Cells(2, 1).Resize(x - 1, 4).Text with this line yellow.

Need to adjust thanks
 
Upvote 0
Not at a PC right now, try changing .Text to .Value2

If that doesn't work, I suspect a change is needed to read the data as string values as they appear.
 
Upvote 0
Hi Jack

Tried as per your instruction

Ooops get a runtime error 1004.:confused:

Unable to set text property of range class

.Cells(2, 1).Resize(UBound(arr, 1), UBound(arr, 2)).Text = arr (line in yellow).



 
Upvote 0
It worked both on value2 and Value. but when I delete data in columns A and B and click run for testing nothing happens.

I closed all file and open a fresh file add additional data to it load code in vb , it updated column A and B , but again when I delete column A and B , click run , nothing happens.

So I have to open file each time and run macro manually then it works but reclick again nothing happens .

:confused: Is this normal or some adjustements need to be done.

Sub M1()


Dim x As Long
Dim arr() As Variant
Dim temp As Variant
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim dic As Object

Const delim As String = "|"

Set ws1 = Sheets("Imput Datasheet")
Set ws2 = Sheets("Product DataBase ")
Set dic = CreateObject("Scripting.Dictionary")


Application.ScreenUpdating = False

With ws2
For x = 1 To .Cells(.Rows.Count, 3).End(xlUp).Row
dic(.Cells(x, 3).Value) = .Cells(x, 1).Value & delim & .Cells(x, 2).Value
Next x
End With

With ws1
x = .Cells(.Rows.Count, 3).End(xlUp).Row
arr = .Cells(2, 1).Resize(x - 1, 4).Value2

For x = LBound(arr, 1) To UBound(arr, 1)
If dic.exists(.Cells(x + 1, 3).Text) Then
temp = Split(dic(.Cells(x + 1, 3).Text), delim)
arr(x, 1) = temp(0)
arr(x, 2) = temp(1)
Erase temp
Else
arr(x, 1) = Empty
arr(x, 2) = Empty
End If
Next x

.Cells(2, 1).Resize(UBound(arr, 1), UBound(arr, 2)).Value2 = arr
End With

Application.ScreenUpdating = True

Set dic = Nothing
Set ws1 = Nothing
Set ws2 = Nothing
Erase arr

End Sub
 
Last edited:
Upvote 0
Without your file, I do not know why it's doing what it's doing, code is just a guess.

It should be based on results of column C of both sheets.
 
Upvote 0

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