VBA Macro to add Header arrays and insert 2 rows

slohman

Board Regular
Joined
Mar 31, 2012
Messages
110
I am trying to create price list from data in sheet Products and copy to sheet Price List with 2 rows of headers not the same and then have 2 blanks rows add to each for each rcell.

Each time I try to do this Headers get replaced even though they are not blank with the next and I cant seem to find how to add rows before the next for each rcell begins.

The below image is what I need. TIA

VBA Code:
Dim rCell As Range
Dim i As Long
Dim ws As Worksheet

'Set the variables
Set DataSh = ThisWorkbook.Sheets("Products")
Set PriceList = ThisWorkbook.Sheets("Price List")

Set CategoriesAA2 = DataSh.Range(DataSh.Cells(3, 1), DataSh.Cells(Rows.Count, 11).End(xlUp))
    'I went from the cell row3/column6 (or F3) and go down until the last non empty cell

    i = 2

    For Each rCell In CategoriesAA2 'loop through each cell in the range

        If rCell = "Play > Elevate, Play > Multi-Age Combinations, Play" Then
       
        Worksheets("Price List").Cells(1).Resize(1, 5).Value = Array("Play > Elevate, Play > Multi-Age Combinations, Play")
        Worksheets("Price List").Cells(1).Resize(1, 5).Value = Array("Code", "SKU", "Name", "Price", "Hyperlink to Web")

            i = i + 1                                'Row number
            PriceList.Cells(i, 2) = rCell.Offset(0, -8) 
            PriceList.Cells(i, 3) = rCell.Offset(0, -7) 
            PriceList.Cells(i, 4) = rCell.Offset(0, -4)  
            PriceList.Cells(i, 5) = rCell.Offset(0, -3)  

    End If

    Next rCell
           
    For Each rCell In CategoriesAA2 'loop through each cell in the range

        If rCell = "Fitness > Fitness Track"
       
        Worksheets("Price List").Cells(1).Resize(1, 5).Value = Array("Fitness > Fitness Track")
        Worksheets("Price List").Cells(1).Resize(1, 5).Value = Array("Code", "SKU", "Name", "Price", "Hyperlink to Web")

            i = i + 1  
            PriceList.Cells(i, 2) = rCell.Offset(0, -8) 
            PriceList.Cells(i, 3) = rCell.Offset(0, -7) 
            PriceList.Cells(i, 4) = rCell.Offset(0, -4)  
            PriceList.Cells(i, 5) = rCell.Offset(0, -3)  

        End If

    Next rCell
   
    Worksheets("Price List").Activate
    Range("D:D").HorizontalAlignment = xlRight
    Range("C:C").HorizontalAlignment = xlLeft
    Range("D:D").NumberFormat = "$#,##0.00"
    Range("A:F").Font.Size = 10
    Range("A:F").Font.Color = vbBlack
    Range("A:F").Font.FontStyle = "Calibri Light"

End Sub
 

Attachments

  • Screenshot 2023-06-01 121031.jpg
    Screenshot 2023-06-01 121031.jpg
    92.4 KB · Views: 18

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Can you show us what the Products worksheet looks like and provide us with some data.

Thanks.
 
Upvote 0
Can you paste the data instead of an image of the data.
 
Upvote 0
The easiest way although not recommended way is to to highlight the range, copy and then paste it into your post.

In future use XL2BB, the link is in the Mr Excel editor ribbon.
 
Upvote 0
IDTypeSKUNameALP PriceMark Up%Vic Sell PriceHyperlink to WebSKUCategoriesAA2
14039simpleSTSW50E3.5m Swing Double Polished4362.43321.00%5279blablablaTRUEFreestanding > Freestanding Swings, Play
14040simpleSTSW98E3m Swing Double Polished4093.79321.00%4954blablablaTRUEFreestanding > Freestanding Swings, Play
13988simpleSE72Accessible Big Dipper2821.11121.00%3414blablablaTRUEFreestanding > Freestanding Springs & Rockers, Inclusivity > Inclusive Rockers, Play
6146simpleFS159Accessible Carousel (PC frame)18207.4621.00%22032blablablaTRUEFreestanding > Freestanding Motion, Inclusivity > Inclusive Motion, Play, Inclusivity > Social Play
14007simpleSE71Accessible Dipper2115.12621.00%2560blablablaTRUEFreestanding > Freestanding Springs & Rockers, Inclusivity > Inclusive Rockers, Play
14008simpleSE70Accessible Scuttle2006.38221.00%2428blablablaTRUEFreestanding > Freestanding Springs & Rockers, Inclusivity > Inclusive Rockers, Play
1520simpleFS25Activity Net 4m22376.128.00%24167blablablaTRUEFreestanding > Freestanding Static, Play
1522simpleFS26Activity Net 6m31660.368.00%34194blablablaTRUEFreestanding > Freestanding Static, Play
1518simpleFS81Activity Net 6m Double77734.78.00%83954blablablaTRUEFreestanding > Freestanding Static, Play
1644simpleAero WalkAerobic Walker - ParkfitContact SPAContact SPAContact SPAblablablaTRUEFitness > ParkFit, Fitness > ParkFit > Cardio, Fitness > ParkFit > Flexibility & Mobility
1704simpleSE64Ambulance11941.55421.00%14450blablablaTRUEFreestanding > Freestanding Springs & Rockers, Inclusivity > Inclusive Rockers, Play, Inclusivity > Themed Play
827simpleOF64AFSAnvil Table - Aluminium (free standing)3569.50820.00%4284blablablaTRUEStreet & Park Furniture, Street & Park Furniture > Tables
790simpleOF34TArbor Bench - Timber1918.6620.00%2303blablablaTRUEStreet & Park Furniture, Street & Park Furniture > Seats & Benches
951simpleFT34Arched Monkey Bars2756.15930.00%3584blablablaTRUEFitness > Fitness Track, Fitness
1524simpleFS94Arched Net Climber14872.85821.00%17997blablablaTRUEFreestanding > Freestanding Static, Play
935simpleFT25Arched Roman Triangles2774.92730.00%3608blablablaTRUEFitness > Fitness Track
1576simpleFS65Argonaut16437.04221.00%19889blablablaTRUEFreestanding > Freestanding Motion, Play, Inclusivity > Social Play
843simpleLB37Ascot Bin2206.80420.00%2649blablablaTRUEStreet & Park Furniture, Street & Park Furniture > Bins
1612simpleBack ExtBack Extension - ParkfitContact SPAContact SPAContact SPAblablablaTRUEFitness > ParkFit, Fitness > ParkFit > Strength
10736simpleFS15Backhoe1587.16121.00%1921blablablaTRUEFreestanding > Freestanding Static, Play, Inclusivity > Themed Play
917simpleFT11ABalance Walker1537.25130.00%1999blablablaTRUEFitness > Fitness Track
1526simpleFS22Ball Toss1253.43121.00%1517blablablaTRUEFreestanding > Freestanding Static, Play
1706simpleSE59Beach Buggy13488.5821.00%16322blablablaTRUEFreestanding > Freestanding Springs & Rockers, Inclusivity > Inclusive Rockers, Play, Inclusivity > Themed Play
1614simpleBench PressBench Press - ParkfitContact SPAContact SPAContact SPAblablablaTRUEFitness > ParkFit, Fitness > ParkFit > Strength
872simpleSH01Bibra Shelter6064.11120.00%7277blablablaTRUEStreet & Park Furniture, Street & Park Furniture > Shelters
861simpleBR07Bike Rail453.37620.00%545TRUEStreet & Park Furniture, Street & Park Furniture > Bike Racks & Bollards
1708simpleSE51Boat Rocker2072.34621.00%2508TRUEFreestanding > Freestanding Springs & Rockers, Play, Inclusivity > Themed Play
1660simpleBody TwistBody Twist - ParkfitContact SPAContact SPAContact SPATRUEFitness > ParkFit, Fitness > ParkFit > Flexibility & Mobility
13888simpleFS157Bouncer - Rectangle9431.2621.00%11412TRUEFreestanding > Freestanding Motion, Play, Inclusivity > Social Play
13877simpleFS158Bouncer - Round9727.7421.00%11771TRUEFreestanding > Freestanding Motion, Play, Inclusivity > Social Play
 
Upvote 0
Give this a go on a copy of your data.

VBA Code:
Private Sub subCreatePriceList()
Dim WsProducts As Worksheet
Dim WsPriceList As Worksheet
Dim lngLastRow  As Long
Dim strCategory As String
Dim i As Integer
Dim rngCategories As Range
Dim rng As Range
Dim intRow As Integer

    ActiveWorkbook.Save
    
    Call subDeleteWorksheet("ProductsCopied")
    Call subDeleteWorksheet("Price List")
    
    ' Create a copy of Products.
    Worksheets("Products").Copy after:=Sheets("Products")
    Application.CutCopyMode = False
    ActiveSheet.Name = "ProductsCopied"
    
    ' Create Price List sheet.
    Worksheets.Add after:=Worksheets("ProductsCopied")
    ActiveSheet.Name = "Price List"
    
    Set WsPriceList = Worksheets("Price List")
    
    Set WsProducts = Worksheets("ProductsCopied")
    WsProducts.Activate
    
    WsProducts.Range("B1").Value = "Code"
    WsProducts.Range("C1").Value = "Temp"
    WsProducts.Range("J1").Value = "Temp2"
    
    Call subDeleteColumn(WsProducts, "ID")
    Call subDeleteColumn(WsProducts, "Mark Up%")
    Call subDeleteColumn(WsProducts, "Vic Sell Price")
    Call subDeleteColumn(WsProducts, "SKU")
    Call subDeleteColumn(WsProducts, "Temp2")
    WsProducts.Range("B1").Value = "SKU"
        
    lngLastRow = WsProducts.Cells(WsProducts.Rows.Count, 1).End(xlUp).Row
    
    Set rngCategories = WsProducts.Range("F1:F" & lngLastRow)
        
    intRow = 1
    
    strCategory = WsProducts.Range("F1")
        
    For i = 2 To WsProducts.Cells(WsProducts.Rows.Count, 1).End(xlUp).Row
    
        If WsProducts.Range("F" & i).Value <> strCategory Then
        
            strCategory = WsProducts.Range("F" & i).Value
            
            Set rng = WsProducts.Cells(i, 1).Resize(WorksheetFunction.CountIf(rngCategories, strCategory), 5)
            
            WsPriceList.Cells(intRow, 1).Value = strCategory
            WsPriceList.Cells(intRow + 1, 1).Resize(1, 5).Value = Array("Code", "SKU", "Name", "ALP Price", "Hyperlink to Web")
            rng.Copy WsPriceList.Cells(intRow + 2, 1)
            intRow = intRow + rng.Rows.Count + 4
            
        End If
        
    Next i
    
    WsProducts.Cells.EntireColumn.AutoFit
    
    For i = 1 To 5
        WsPriceList.Cells(1, i).EntireColumn.ColumnWidth = WsProducts.Cells(1, i).EntireColumn.ColumnWidth
    Next i
    
    WsPriceList.Cells.VerticalAlignment = xlCenter
    
    subDeleteWorksheet ("ProductsCopied")
    
    ActiveWorkbook.Save
        
    MsgBox "Price List Produced", vbOKOnly, "Confirmation"
    
End Sub

Private Sub subDeleteWorksheet(strWorksheet As String)

    Application.DisplayAlerts = False
    On Error Resume Next
    Worksheets(strWorksheet).Delete
    On Error GoTo 0
    Application.DisplayAlerts = True
    
End Sub

Private Sub subDeleteColumn(Ws As Worksheet, strHeader As String)
Dim rngFound As Range

    Set rngFound = Ws.Rows(1).Find(strHeader, LookIn:=xlValues)

    If Not rngFound Is Nothing Then
        rngFound.EntireColumn.Delete
    End If

End Sub
 
Upvote 0
Is there a way of saving the pricing as values not the formulas in column d (Vic Pricing), also the macro seems to run for a long time before it finishes on Price List is there anything I can do?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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