简体   繁体   中英

Copying Row Info from one sheet to another based on match

I have an excel book that has two sheets: 1) Import 2) Pricing Rules.

Pricing Rules Sheet

The A column is what I need to match on. Example values include STA_PNP4 , STA_PST .. and others. There are potentially around 50 different rows in the sheet, and it will continue to grow over time. Then for each row, there are pricing values in columns B to CF .

Import Sheet

This sheet has the same number of columns, but only Column A is filled out. Example values include STA_PNP4_001_00 , STA_PNP4_007_00 , STA_PST_010_00 .. and many more.

What I need to do:

If the text in Import Sheet Column A before the second "_" matches the column identifer in Pricing Rules Sheet Column A , copy the rest of B to CF of Pricing Rules sheet for that row into the Import sheet for the row it matched on.

Any idea on where to begin with this one?

Why don't you do it using formulas only?

Assuming :

1.) Data in Import Sheet is

(col A)

STA_PNP4_007_00

STA_PNP4_001_00

STA_PNP4_001_00

. .

2.) Data in Pricing Rules Sheet

(Col A) (col B) (ColC) (Col D) .......

STA_PNP4 1 2 3 .....

STA_PST 4 5 6 .....

STA_ASA2 7 8 9 .....

Then write this formula in B1 cell of Import Sheet =IFERROR(VLOOKUP(LEFT(A1,FIND(" ",A1,FIND(" ",A1)+1)-1),PricingRules!$A$1:$CF$100,2,0),"")

Drag it down in column B

and For Column C , D just change index num from 2 to (3 for C) , (4 for D) and like that.

Because it will continue to grow over time you may be best using VBA. However, even with code I would start by applying the 'groups' via formula, so as not to have a spreadsheet overburdened with formulae and hence potentially slow and easy to corrupt. Something like part of @xtremeExcel's solution which I repeat because the underscores have been treated as formatting commands in that answer:

=LEFT(A1,FIND("_",A1,1+FIND("_",A1))-1)  

I'd envisage this (copied down) as an additional column in your Import Sheet - to serve as a key field to link to your Pricing Rules Sheet . Say on the extreme left so available for use by VLOOKUP across the entire sheet.

With that as a key field then either:

  1. Write the code to populate Pricing Rules Sheet as frequently as run/desired. Either populating 'from scratch' each time (perhaps best for low volumes) or incrementally (likely advisable for high volumes).
  2. Use VLOOKUP (as suggested). However with at least 84 columns and, presumably, many more than 50 rows that is a lot of formulae, though may be viable as a temporary 'once off' solution (ie after population Copy/Paste Special/Values).
  3. A compromise. As 2. But preserve a row or a cell with the appropriate formulae/a and copy that to populate the other columns for your additions to your ColumnA and/or ColumnA:B.

Thanks for the input guys.

I got it implemented via a method like this:

{=VLOOKUP(LEFT($A4,7),PricingRules!A3:CF112,{2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84},FALSE)}

That is my ugly function, applied across a whole row, to look up and copy from my pricing rules every column when it finds a match.

Below is the function that I have created for above scenario. Its working as per the requirement that you have mentioned.

Sub CopyData()
Dim wb As Workbook
Dim importws As Worksheet
Dim PricingRulesws As Worksheet
Dim Pricingrowcount As Integer
Dim importRowCount As Integer
Dim FindValue As String
Dim textvalue As String
Dim columncount As Integer
Dim stringarray() As String

'Enter full address of your file ex: "C:\newfolder\datafile.xlsx"
Set wb = Workbooks.Open("C:\newfolder\datafile.xlsx")

'Enter the name of your "import" sheet
Set importws = Sheets("Import")

'Enter the name of your "Pricing" sheet
Set PricingRulesws = Sheets("PricingRules")

For Pricingrowcount = 1 To PricingRulesws.UsedRange.Rows.Count
    FindValue = PricingRulesws.Cells(Pricingrowcount, 1)
    For importRowCount = 1 To importws.UsedRange.Rows.Count
        textvalue = importws.Cells(importRowCount, 1)
        stringarray = Split(textvalue, "_")
        textvalue = stringarray(0) & "_" & stringarray(1)

        If FindValue = textvalue Then
            For columncount = 2 To PricingRulesws.UsedRange.Columns.Count
                importws.Cells(importRowCount, columncount) = PricingRulesws.Cells(Pricingrowcount, columncount)
            Next columncount
        End If
    Next importRowCount
Next Pricingrowcount
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM