Index Match with Concatenate in VBA

Click22

New Member
Joined
Jan 29, 2020
Messages
15
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi,

I am trying to create an index match function within VBA.

The reason for this is that I would like to grab the required information and then for it to not update when a worksheet is calculated.

All of the information is in the same workbook.

The below image is from a sheet called "Stock Codes"

1580298747097.png


The below image is from a sheet called "Full Sheet"

1580299713143.png


What I am trying to achieve is that when I enter a part number in D:D on "Full Sheet" it will bring the Total on Order amount from "Stock Codes" (S:S) into the Total on Order field on "Full Sheet" (K:K)

I would also like to get the Next Del Qty from "Stock Codes" (T:T) and Next Del Date from "Stock Codes" (U:U) to concatenate into the Next Delivery filed on "Full Sheet" (L:L)

I can do this using index match formulas but as mentioned above, I don't want these values to recalculate. I would like them to be taken at a point in time (when the part number is entered).

I already have this code, which time stamps cell C:C when D:D has info entered in "Full Sheet".

1580300355028.png


Any help would be very much appreciated.
 
In column D in Full Sheet, Where is that information located? and what is under the data in column D?
 
Upvote 0

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
In column D in Full Sheet, Where is that information located? and what is under the data in column D?

Sorry, I'm not sure I fully understand the question. In column D in Full Sheet, I will manually enter a part number (this is not necessarily the last line in column D).

That same part number is stored in column A in Stock Codes.
 
Upvote 0
Ok, well try this code, an input box will pop up and you will type your part number in there. But this will paste the answer in the last row of K and last row of L, I dont know where you want me to insert the data, from your example pictures, there is nothing under these rows, hence the reason I have it doing last row.

VBA Code:
Option Explicit
Sub MrExcelDotCom()
Dim cl As Object, lastRow As Long, strCells As String, partNumber As String, _
totalOnOrder As String, nextDelQty As String
With Sheets("Full Sheet")
    lastRow = .Cells(Rows.Count, 4).End(xlUp).Row
    strCells = "D" & lastRow
    partNumber = InputBox("What part number do you want to search for?")
    With Sheets("Stock Codes")
        lastRow = .Cells(Rows.Count, 1).End(xlUp).Row
        strCells = "A1:A" & lastRow
        For Each cl In Sheets("Stock Codes").Range(strCells)
            If cl.Value = partNumber Then
                totalOnOrder = cl.Offset(0, 18).Value
                nextDelQty = cl.Offset(0, 19).Value & " " & cl.Offset(0, 20).Value
            End If
        Next cl
    End With
    lastRow = .Cells(Rows.Count, 11).End(xlUp).Row
    .Cells(lastRow, 11).Offset(1, 0).Value = totalOnOrder
    .Cells(lastRow, 12).Offset(1, 0).Value = nextDelQty
    .Columns("K:L").AutoFit
End With
End Sub
 
Upvote 0
Ok, well try this code, an input box will pop up and you will type your part number in there. But this will paste the answer in the last row of K and last row of L, I dont know where you want me to insert the data, from your example pictures, there is nothing under these rows, hence the reason I have it doing last row.

VBA Code:
Option Explicit
Sub MrExcelDotCom()
Dim cl As Object, lastRow As Long, strCells As String, partNumber As String, _
totalOnOrder As String, nextDelQty As String
With Sheets("Full Sheet")
    lastRow = .Cells(Rows.Count, 4).End(xlUp).Row
    strCells = "D" & lastRow
    partNumber = InputBox("What part number do you want to search for?")
    With Sheets("Stock Codes")
        lastRow = .Cells(Rows.Count, 1).End(xlUp).Row
        strCells = "A1:A" & lastRow
        For Each cl In Sheets("Stock Codes").Range(strCells)
            If cl.Value = partNumber Then
                totalOnOrder = cl.Offset(0, 18).Value
                nextDelQty = cl.Offset(0, 19).Value & " " & cl.Offset(0, 20).Value
            End If
        Next cl
    End With
    lastRow = .Cells(Rows.Count, 11).End(xlUp).Row
    .Cells(lastRow, 11).Offset(1, 0).Value = totalOnOrder
    .Cells(lastRow, 12).Offset(1, 0).Value = nextDelQty
    .Columns("K:L").AutoFit
End With
End Sub

That didn't seem to work either. Is there a way to upload an example file?
 
Upvote 0
Hopefully this will clarify the intended outcome.
This is an example of the "Stock Codes" sheet
1580392520776.png


And this is what I would like the outcome to be when I add a part number into the "Full Sheet" page. Please note that the cell rows can be random as a certain amount of cells are pre-allocated to each user.
Ideally the code will be triggered when the part number is entered in column D.
It is only the H & I calculations that I require via VBA. If this can be added to the pre-existing code on the original post that would be ideal.
1580392591502.png


Thanks
 
Upvote 0
Part of your problem here is that your example of the Stock Codes sheet in post #15 is different from the one in post #1. VBA is rubbish at guessing where your columns may sit.
 
Upvote 0
Anyway for what you posted in post #15.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim rng As Range, myMatch As String

Set rng = Intersect(Target, Columns("D"))

If Not rng Is Nothing Then
    If rng.Cells.Count = 1 Then
        If Len(rng.Value) > 0 Then
            myMatch = Application.Match(rng.Value, Sheets("Stock Codes").Columns("A"), 0)
            If Not IsError(myMatch) Then
                rng.Offset(0, 4) = Sheets("Stock Codes").Cells(myMatch, "I")
                rng.Offset(0, 5) = Sheets("Stock Codes").Cells(myMatch, "J") & " on " & Sheets("Stock Codes").Cells(myMatch, "K")
            End If
        Else
            rng.Offset(0, 4) = vbNullString
            rng.Offset(0, 5) = vbNullString
        End If
    End If
End If

End Sub
 
Upvote 0
Anyway for what you posted in post #15.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim rng As Range, myMatch As String

Set rng = Intersect(Target, Columns("D"))

If Not rng Is Nothing Then
    If rng.Cells.Count = 1 Then
        If Len(rng.Value) > 0 Then
            myMatch = Application.Match(rng.Value, Sheets("Stock Codes").Columns("A"), 0)
            If Not IsError(myMatch) Then
                rng.Offset(0, 4) = Sheets("Stock Codes").Cells(myMatch, "I")
                rng.Offset(0, 5) = Sheets("Stock Codes").Cells(myMatch, "J") & " on " & Sheets("Stock Codes").Cells(myMatch, "K")
            End If
        Else
            rng.Offset(0, 4) = vbNullString
            rng.Offset(0, 5) = vbNullString
        End If
    End If
End If

End Sub


Thanks Steve, that has worked a treat.
Appreciate your help.
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,693
Members
448,979
Latest member
DET4492

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