Excel VBA to copy a cell(s) and paste with varied criteria, appended.

Plexus

New Member
Joined
Jul 20, 2016
Messages
34
Hi,

I have a table that shows products that need to be matched to a set criteria and then pasted into a new table appended. The example below shows the desired result in D&E.

When a product (A) has an action (B) to 'Exclude COS', I want to paste appended the Product & the Macro Area listed in (G) 'STORES COS' to D&E.

When a Product has and action 'Exclude all' I want to paste the product and a repeated row, listing all Macro areas, again appended to D&E.

The 'list of Macro areas' does not need to be a list and can be coded if needed.

ABDEG
SKUAction?SKUMacro AreaList of Macro areas
AALCMSCNExclude AllAALENTENSTORES COSSTORES COS
AALENTENExclude COSAALCMSCNSTORES COSSMB
AALCMSCNSMBEnterprise
AALCMSCNEnterpriseOnline
AALCMSCNOnline


I have spent a couple of hours trying to work this out and watching various videos but have not managed it.

Thanks for any help given!

Plex.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Try this with a copy of your data.

VBA Code:
Sub Rearrange()
  Dim Data As Variant, MacroAreas As Variant
  Dim i As Long, nr As Long, NumMacroAreas As Long, rws As Long
  
  Data = Range("A2", Range("B" & Rows.Count).End(xlUp)).Value
  MacroAreas = Range("G2", Range("G" & Rows.Count).End(xlUp)).Value
  NumMacroAreas = UBound(MacroAreas)
  nr = 2
  For i = 1 To UBound(Data)
    rws = IIf(LCase(Data(i, 2)) = "exclude all", NumMacroAreas, 1)
    Cells(nr, "D").Resize(rws).Value = Data(i, 1)
    Cells(nr, "E").Resize(rws).Value = MacroAreas
    nr = nr + rws
  Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,264
Members
449,075
Latest member
staticfluids

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