VBA Code to Find Copy Rows From Source Sheet Based on Text and Paste on Target Sheet Corresponding Number of Times.

JPatty

New Member
Joined
Sep 21, 2022
Messages
14
Office Version
  1. 365
  2. 2019
  3. 2011
Platform
  1. Windows
I have a workbook with three sheets. The first sheet "Blanket_Data" has all of the standard colors that can be ordered by a customer. The second sheet "Cust_Color" contains the colors ordered by customers in column A and the customer account in column B. I need to find and copy the row on the Blanket_Data sheet based on the text in each cell in column A on the Cust_Color sheet, starting on A2. The row copied from Blanket_Data needs to be pasted on the third sheet "Import" and copied down for the number of cells with text in column A on the Cust_Color sheet, starting at A2. The customer account from column B on Cust_Color needs to be copied to the Import sheet in columns C and D. The Import sheet below is an example of what I would like the final result to be, sans the colorful formatting.
Thank you in advance.

BLANKET_DATA
COLORTYPECUST ACCTCUST CODECOLOR CODEUsername 1Credential 1Credential 2Credential 3ACCTACCOUNT FLAGPaymentENABLE OVERRIDE
BLACKSTANDARDBLACKSPECIAL12345888888USD
BLUESTANDARDBLUESPECIAL6789122222777777NoUSDYes
BROWNSTANDARDBROWN444444USDNo
GRAYSTANDARDGRAY12345999999USDNo
GREENSTANDARDGREENSPECIAL12345666666USDNo
ORANGESTANDARDORANGESPECIAL6789122111111USDNo
PINK STANDARDPINK SPECIAL6789133333333333USDNo
PURPLESTANDARDPURPLESPECIAL67891222222USDNo
RED STANDARDRED SPECIAL12345777777USD
WHITESTANDARDWHITE555555USD
YELLOWSTANDARDYELLOW11111555555USDNo


CUST_COLOR
COLORCUST ACCT
GREENTPFB
YELLOWYPHPC
BLUEJPKCJ
BLACKKPPM
RED VPTFMI
ORANGEBPWPC
PINK
PURPLE


IMPORT
COLORTYPECUST ACCTCUST CODECOLOR CODEUsername 1Credential 1Credential 2Credential 3ACCTACCOUNT FLAGPaymentENABLE OVERRIDE
GREENSTANDARDTPFBTPFBGREENSPECIAL12345666666USDNo
YELLOWSTANDARDTPFBTPFBYELLOW11111555555USDNo
BLUESTANDARDTPFBTPFBBLUESPECIAL6789122222777777NoUSDYes
BLACKSTANDARDTPFBTPFBBLACKSPECIAL12345888888USD
RED STANDARDTPFBTPFBRED SPECIAL12345777777USD
ORANGESTANDARDTPFBTPFBORANGESPECIAL6789122111111USDNo
PINK STANDARDTPFBTPFBPINK SPECIAL6789133333333333USDNo
PURPLESTANDARDTPFBTPFBPURPLESPECIAL67891222222USDNo
GREENSTANDARDYPHPCYPHPCGREENSPECIAL12345666666USDNo
YELLOWSTANDARDYPHPCYPHPCYELLOW11111555555USDNo
BLUESTANDARDYPHPCYPHPCBLUESPECIAL6789122222777777NoUSDYes
BLACKSTANDARDYPHPCYPHPCBLACKSPECIAL12345888888USD
RED STANDARDYPHPCYPHPCRED SPECIAL12345777777USD
ORANGESTANDARDYPHPCYPHPCORANGESPECIAL6789122111111USDNo
PINK STANDARDYPHPCYPHPCPINK SPECIAL6789133333333333USDNo
PURPLESTANDARDYPHPCYPHPCPURPLESPECIAL67891222222USDNo
GREENSTANDARDJPKCJJPKCJGREENSPECIAL12345666666USDNo
YELLOWSTANDARDJPKCJJPKCJYELLOW11111555555USDNo
BLUESTANDARDJPKCJJPKCJBLUESPECIAL6789122222777777NoUSDYes
BLACKSTANDARDJPKCJJPKCJBLACKSPECIAL12345888888USD
RED STANDARDJPKCJJPKCJRED SPECIAL12345777777USD
ORANGESTANDARDJPKCJJPKCJORANGESPECIAL6789122111111USDNo
PINK STANDARDJPKCJJPKCJPINK SPECIAL6789133333333333USDNo
PURPLESTANDARDJPKCJJPKCJPURPLESPECIAL67891222222USDNo
GREENSTANDARDKPPMKPPMGREENSPECIAL12345666666USDNo
YELLOWSTANDARDKPPMKPPMYELLOW11111555555USDNo
BLUESTANDARDKPPMKPPMBLUESPECIAL6789122222777777NoUSDYes
BLACKSTANDARDKPPMKPPMBLACKSPECIAL12345888888USD
RED STANDARDKPPMKPPMRED SPECIAL12345777777USD
ORANGESTANDARDKPPMKPPMORANGESPECIAL6789122111111USDNo
PINK STANDARDKPPMKPPMPINK SPECIAL6789133333333333USDNo
PURPLESTANDARDKPPMKPPMPURPLESPECIAL67891222222USDNo
GREENSTANDARDVPTFMIVPTFMIGREENSPECIAL12345666666USDNo
YELLOWSTANDARDVPTFMIVPTFMIYELLOW11111555555USDNo
BLUESTANDARDVPTFMIVPTFMIBLUESPECIAL6789122222777777NoUSDYes
BLACKSTANDARDVPTFMIVPTFMIBLACKSPECIAL12345888888USD
RED STANDARDVPTFMIVPTFMIRED SPECIAL12345777777USD
ORANGESTANDARDVPTFMIVPTFMIORANGESPECIAL6789122111111USDNo
PINK STANDARDVPTFMIVPTFMIPINK SPECIAL6789133333333333USDNo
PURPLESTANDARDVPTFMIVPTFMIPURPLESPECIAL67891222222USDNo
GREENSTANDARDBPWPCBPWPCGREENSPECIAL12345666666USDNo
YELLOWSTANDARDBPWPCBPWPCYELLOW11111555555USDNo
BLUESTANDARDBPWPCBPWPCBLUESPECIAL6789122222777777NoUSDYes
BLACKSTANDARDBPWPCBPWPCBLACKSPECIAL12345888888USD
RED STANDARDBPWPCBPWPCRED SPECIAL12345777777USD
ORANGESTANDARDBPWPCBPWPCORANGESPECIAL6789122111111USDNo
PINK STANDARDBPWPCBPWPCPINK SPECIAL6789133333333333USDNo
PURPLESTANDARDBPWPCBPWPCPURPLESPECIAL67891222222USDNo
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
First, let me thank you for taking the time to set such an explicit example.
I hope the wait and your effort are worth it.

Try the following:

VBA Code:
Sub Generate_import_sheet()
  Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet
  Dim rColor As Range, rCust As Range
  Dim i As Long
  Dim f As Range
  
  Set sh1 = Sheets("BLANKET_DATA")
  Set sh2 = Sheets("CUST_COLOR")
  Set sh3 = Sheets("IMPORT")
  sh3.Rows("2:" & Rows.Count).ClearContents
  
  i = 2
  For Each rCust In sh2.Range("B2", sh2.Range("B" & Rows.Count).End(3))
    For Each rColor In sh2.Range("A2", sh2.Range("A" & Rows.Count).End(3))
      Set f = sh1.Range("A:A").Find(rColor.Value, , xlValues, xlWhole, , , False)
      If Not f Is Nothing Then
        sh3.Range("A" & i).Resize(1, 13).Value = sh1.Range("A" & f.Row).Resize(1, 13).Value
        sh3.Range("B" & i).Resize(1, 2).Value = rCust.Value
        i = i + 1
      End If
    Next
  Next
End Sub
 
Upvote 0
First, let me thank you for taking the time to set such an explicit example.
I hope the wait and your effort are worth it.

Try the following:

VBA Code:
Sub Generate_import_sheet()
  Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet
  Dim rColor As Range, rCust As Range
  Dim i As Long
  Dim f As Range
 
  Set sh1 = Sheets("BLANKET_DATA")
  Set sh2 = Sheets("CUST_COLOR")
  Set sh3 = Sheets("IMPORT")
  sh3.Rows("2:" & Rows.Count).ClearContents
 
  i = 2
  For Each rCust In sh2.Range("B2", sh2.Range("B" & Rows.Count).End(3))
    For Each rColor In sh2.Range("A2", sh2.Range("A" & Rows.Count).End(3))
      Set f = sh1.Range("A:A").Find(rColor.Value, , xlValues, xlWhole, , , False)
      If Not f Is Nothing Then
        sh3.Range("A" & i).Resize(1, 13).Value = sh1.Range("A" & f.Row).Resize(1, 13).Value
        sh3.Range("B" & i).Resize(1, 2).Value = rCust.Value
        i = i + 1
      End If
    Next
  Next
End Sub

Hi Dante,

That code worked great! Thank you so much. I didn't realize it at the time but I need to find and copy the row on the "Blanket_Data" sheet based on the text in each cell in col A as well as col B on the Cust_Color sheet. Any help would be appreciated.

Cust_Color
API_Credentials.xlsm
ABCD
1COLORTypeCUST ACCT
2GREENStandardTPFB
3YELLOWStandardYPHPC
4BLUEStandardJPKCJ
5BLACKStandardKPPM
6RED StandardVPTFMI
7ORANGEStandardBPWPC
8PINK Standard
9PURPLEVivid
10
Sheet1


Blanket_Data
Book1
ABCDEFGHIJKLMN
1COLORTYPECUST ACCTCUST CODECOLOR CODEUsername 1Credential 1Credential 2Credential 3ACCTACCOUNT FLAGPaymentENABLE OVERRIDE
2BLACKSTANDARDBLACKSPECIAL12345888888USD
3BLUESTANDARDBLUESPECIAL6789122222777777NoUSDYes
4BROWNSTANDARDBROWN444444USDNo
5GRAYSTANDARDGRAY12345999999USDNo
6GREENSTANDARDGREENSPECIAL12345666666USDNo
7ORANGESTANDARDORANGESPECIAL6789122111111USDNo
8PINK STANDARDPINK SPECIAL6789133333333333USDNo
9PURPLESTANDARDPURPLESPECIAL67891222222USDNo
10RED STANDARDRED SPECIAL12345777777USD
11WHITESTANDARDWHITE555555USD
12YELLOWSTANDARDYELLOW11111555555USDNo
13PURPLEVividYELLOW11111555555USDNo
14
Sheet2
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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