+ Reply to Thread
Results 1 to 6 of 6

VBA Index Match add Row

Hybrid View

  1. #1
    Registered User
    Join Date
    04-12-2021
    Location
    us
    MS-Off Ver
    365
    Posts
    6

    VBA Index Match add Row

    Hello, I am new to VBA and came up with some code that does what I need but after a week I am stuck. I have 700 files that I need to run this on . There are Files that start with PL they need to be updated using the RM File. The file that says complete is what it should look like after it is done.

    Basically want to match a part number from the PL Files to the RM File if match found add row below. Copy PL file Part Number to offset -1 column in new row and the rest of the data will be filled in from the RM file. The COMPLETE file has some VBA I was able to work out but its just not coming together. Below is a image explaining the complete file.

    Thank you to anyone that is able to she some light on this, I am at a dead end.

    Output.png
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,263

    Re: VBA Index Match add Row

    Try the macros below: put the code into a macro-enabled .xlsm file, and save that file in the same folder with the RM file and all the PL files (I have assumed everything is in the same folder). Then run "LoopThroughPLFiles". Try it out on copies of a few files first.... just to be certain. Not sure why, but the file you attached starts with just "P" and not "PL".... I'm guessing that is just a typo. I have also assumed that none of the files have multiple sheets....

    Option Explicit
    Public wkbkRM As Workbook
    Public wsRM As Worksheet
    
    Sub LoopThroughPLFiles()
        Dim strFName As String
        Dim strPath As String
        Dim strWFile As String
        Dim wkbkWF As Workbook
        
        Application.DisplayAlerts = False
           
        strPath = ThisWorkbook.Path & "\"
        
        Set wkbkRM = Workbooks.Open(strPath & "RM File.xlsx")
        Set wsRM = wkbkRM.Worksheets(1)
        
        strWFile = Dir(strPath & "*.xlsx")
        
        Do While strWFile <> ""
            If strWFile Like "PL*" Then
                Set wkbkWF = Workbooks.Open(strPath & strWFile)
                ProcessPLFile wkbkWF
                wkbkWF.Save
                wkbkWF.Close
            End If
            strWFile = Dir()
        Loop
        wkbkRM.Close False
    End Sub
    
    Sub ProcessPLFile(wkbkWB As Workbook)
        Dim lngR As Long
        Dim wsW As Worksheet
        Dim rngF As Range
        
        Set wsW = wkbkWB.Worksheets(1)
        
        For lngR = wsW.Cells(wsW.Rows.Count, "I").End(xlUp).Row To 2 Step -1
            Set rngF = wsRM.Range("D:D").Find(wsW.Cells(lngR, "I"))
            If Not rngF Is Nothing Then
                wsW.Rows(lngR + 1).EntireRow.Insert
                wsW.Cells(lngR + 1, "H").Value = wsW.Cells(lngR, "I").Value
                wsW.Cells(lngR + 1, "I").Value = wsRM.Cells(rngF.Row, "F").Value
                wsW.Cells(lngR + 1, "K").Value = wsRM.Cells(rngF.Row, "G").Value
                wsW.Cells(lngR + 1, "M").Value = wsRM.Cells(rngF.Row, "I").Value
                wsW.Cells(lngR + 1, "N").Value = wsRM.Cells(rngF.Row, "E").Value
            End If
        Next lngR
                        
    End Sub
    Last edited by Bernie Deitrick; 05-26-2022 at 12:40 PM.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Registered User
    Join Date
    04-12-2021
    Location
    us
    MS-Off Ver
    365
    Posts
    6

    Re: VBA Index Match add Row

    Hello Bernie

    Thank you and you assumptions were correct that was a typo and all the files are in the same spot. I ran the code and worked perfectly but I messed up. Is there a way to tweek the create row condition. If match found in column D in RM file then check Column F for value then create row. If column F in Rm file blank do not add row. Then copy Column F & Column G like you the VBA says then preform index match to get values for PL files column M & N. I upload a picture to show the differences the highlighted values are off. Thank you again this was a life safer.

    Attachment 781879
    Attached Images Attached Images
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,263

    Re: VBA Index Match add Row

    Replace this:

            If Not rngF Is Nothing Then
                wsW.Rows(lngR + 1).EntireRow.Insert
                wsW.Cells(lngR + 1, "H").Value = wsW.Cells(lngR, "I").Value
                wsW.Cells(lngR + 1, "I").Value = wsRM.Cells(rngF.Row, "F").Value
                wsW.Cells(lngR + 1, "K").Value = wsRM.Cells(rngF.Row, "G").Value
                wsW.Cells(lngR + 1, "M").Value = wsRM.Cells(rngF.Row, "I").Value
                wsW.Cells(lngR + 1, "N").Value = wsRM.Cells(rngF.Row, "E").Value
            End If
    With this:

            If Not rngF Is Nothing Then
                If wsRM.Cells(lngR, "F").Value <> "" Then
                    wsW.Rows(lngR + 1).EntireRow.Insert
                    wsW.Cells(lngR + 1, "H").Value = wsW.Cells(lngR, "I").Value
                    wsW.Cells(lngR + 1, "I").Value = wsRM.Cells(rngF.Row, "F").Value
                    wsW.Cells(lngR + 1, "K").Value = wsRM.Cells(rngF.Row, "G").Value
                    wsW.Cells(lngR + 1, "M").Value = wsRM.Cells(rngF.Row, "I").Value
                    wsW.Cells(lngR + 1, "N").Value = wsRM.Cells(rngF.Row, "E").Value
                End If
            End If
    Not sure what you want to do with M and N values - add them to the original row as well as the new row?

  5. #5
    Registered User
    Join Date
    04-12-2021
    Location
    us
    MS-Off Ver
    365
    Posts
    6

    Re: VBA Index Match add Row

    Yeah the RM File has values for M & N so they would be added to both new and old rows. All the rows after the new row is added need to get the values from RM File Column I and Column E and place the value in the PL files column M & N.

    I replaced the code but it did nothing to the file.

    I removed all blanks in column F from my RM file and them the code added the rows correctly. And after that action this part should happen to all the parts in column I matched and find and copy this part
    wsW.Cells(lngR + 1, "M").Value = wsRM.Cells(rngF.Row, "I").Value
    wsW.Cells(lngR + 1, "N").Value = wsRM.Cells(rngF.Row, "E").Value

  6. #6
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,263

    Re: VBA Index Match add Row

    This should do what you want... sorry for the delay - took a long weekend

    Option Explicit
    Public wkbkRM As Workbook
    Public wsRM As Worksheet
    
    Sub LoopThroughPLFiles()
        Dim strFName As String
        Dim strPath As String
        Dim strWFile As String
        Dim wkbkWF As Workbook
        
        Application.DisplayAlerts = False
           
        strPath = ThisWorkbook.Path & "\"
        
        Set wkbkRM = Workbooks.Open(strPath & "RM File.xlsx")
        Set wsRM = wkbkRM.Worksheets(1)
        
        strWFile = Dir(strPath & "*.xlsx")
        
        Do While strWFile <> ""
            If strWFile Like "PL*" Then
                Set wkbkWF = Workbooks.Open(strPath & strWFile)
                ProcessPLFile wkbkWF
                wkbkWF.Save
                wkbkWF.Close
            End If
            strWFile = Dir()
        Loop
        wkbkRM.Close False
    End Sub
    
    Sub ProcessPLFile(wkbkWB As Workbook)
        Dim lngR As Long
        Dim wsW As Worksheet
        Dim rngF As Range
        
        Set wsW = wkbkWB.Worksheets(1)
        
        For lngR = wsW.Cells(wsW.Rows.Count, "I").End(xlUp).Row To 2 Step -1
            Set rngF = wsRM.Range("D:D").Find(wsW.Cells(lngR, "I"))
            If Not rngF Is Nothing Then
                If wsRM.Cells(lngR, "F").Value <> "" Then
                    wsW.Rows(lngR + 1).EntireRow.Insert
                    wsW.Cells(lngR + 1, "H").Value = wsW.Cells(lngR, "I").Value
                    wsW.Cells(lngR + 1, "I").Value = wsRM.Cells(rngF.Row, "F").Value
                    wsW.Cells(lngR + 1, "K").Value = wsRM.Cells(rngF.Row, "G").Value
                    wsW.Cells(lngR + 1, "M").Value = wsRM.Cells(rngF.Row, "I").Value
                    wsW.Cells(lngR + 1, "N").Value = wsRM.Cells(rngF.Row, "E").Value
                    wsW.Cells(lngR, "M").Value = wsRM.Cells(rngF.Row, "I").Value
                    wsW.Cells(lngR, "N").Value = wsRM.Cells(rngF.Row, "E").Value
                End If
            End If    Next lngR
                        
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] index match function vs index match vba type mismatch
    By johnstylez in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 02-14-2022, 03:29 PM
  2. [SOLVED] INDEX+MATCH instead of VLOOKUP+MATCH, why is INDEX a better choice and how to re-write?
    By Renejorgensen in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-23-2016, 10:54 AM
  3. [SOLVED] Index / Match - match 3 input values and return the results from the index
    By t83357 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-08-2016, 07:34 PM
  4. [SOLVED] Match-Index in stead of Index-Match lookup Array among Arrays
    By Numnum in forum Excel General
    Replies: 2
    Last Post: 10-15-2015, 02:08 PM
  5. Replies: 6
    Last Post: 04-30-2014, 02:42 AM
  6. Replies: 6
    Last Post: 11-08-2013, 10:29 PM
  7. Replies: 3
    Last Post: 05-02-2013, 01:31 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1