Modifying a macro that splits data into ws's by column value - getting the code to paste to next blank row?

dougmarkham

Active Member
Joined
Jul 19, 2016
Messages
252
Office Version
  1. 365
Platform
  1. Windows
Hi Folks,

Basic problem & goal:

I have a macro that splits data in a master worksheet into existing worksheets i.e., by reference to column 6's values (see Red text in below tables).
However, if I run it twice for different data, new data over-writes previously split data.

Context:
The worksheet "ManifestMaster" is populated by a 'product manifest' where each product is given a pallet number (Pallet 1; Pallet 2 etc).
Products can be on more than one pallet.

The main issue:
My brief is to make a macro that can be run multiple times i.e., to keep adding more products to the master manifest.
Currently, re-runs of the code are over-writing data already existing in the Pallet worksheets.

Details

For instance:
First instance: I add initial set of products to the manifest including "Product 1" that is to go on "Pallet 1"
Second instance: I add additional products to the manifest including "Product 3" that is to go on "Pallet 1"

Thus, on the second instance when I run the macro, currently it over-writes "Product 1's" row on Worksheet("Pallet 1") with "Product 3's row"

First instance
"ManifestMaster"
Product CodeDescriptionQuantityUOMBox numberPallet NumberLoad numberLocation
Product 1Sales ticket £1050Pack 101Pallet 11Isle 1; Bay 3
Product 50Poster A3 Deal 41000Pack 25100Pallet 31Isle 3; Bay 12
Product 8Torch AA batteries100Single5Pallet 91Isle 6; Bay 7

"Pallet 1"
Product CodeDescriptionQuantityUOMBox numberPallet NumberLoad numberLocation
Product 1Sales ticket £1050Pack 101Pallet 11Isle 1; Bay 3

Second instance

"ManifestMaster"
Product CodeDescriptionQuantityUOMBox numberPallet NumberLoad numberLocation
Product 3Sales ticket £2550Pack 101Pallet 11Isle 1; Bay 4
Product 23Poster A2 Deal 11011Pack 2030Pallet 51Isle 20; Bay 18
Product 8Banner 60 ft Summer Sale4Single4Pallet 191Isle 17; Bay 1

"Pallet 1"
Product CodeDescriptionQuantityUOMBox numberPallet NumberLoad numberLocation
Product 3Sales ticket £2550Pack 101Pallet 11Isle 1; Bay 4


What I'm aiming for:

Second instance


"ManifestMaster"
Product CodeDescriptionQuantityUOMBox numberPallet NumberLoad numberLocation
Product 3Sales ticket £2550Pack 101Pallet 11Isle 1; Bay 4
Product 23Poster A2 Deal 11011Pack 2030Pallet 51Isle 20; Bay 18
Product 8Banner 60 ft Summer Sale4Single4Pallet 191Isle 17; Bay 1

"Pallet 1"
Product CodeDescriptionQuantityUOMBox numberPallet NumberLoad numberLocation
Product 1Sales ticket £1050Pack 101Pallet 11Isle 1; Bay 3
Product 3Sales ticket £2550Pack 101Pallet 11Isle 1; Bay 4


Would anyone please help me modify the below code so copied rows don't over-write existing rows in the destination worksheet?

Here is the line which I'm trying to modify so it' doesn't put the copied rows into A2 but looks for the first available blank row in column A.

VBA Code:
        ws.Range("A" & titlerowOffset & ":A" & lr).EntireRow.Copy Sheets(myarr(i) & "").Range("A2")

Here is the full code

VBA Code:
Sub CopyDataToPalletTabs()

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlManual

    Dim lr As Long
    Dim ws As Worksheet
    Dim vcol, i As Integer
    Dim icol As Long
    Dim myarr As Variant
    Dim title As String
    Dim titlerow As Integer
    Dim titlerowOffset As Integer
    
    'vcol designate as the 6th column i.e., Col F
    vcol = 6
    
    'Set ws
    Set ws = Sheets("ManifestMaster")
    
    lr = ws.Cells(ws.Rows.Count, vcol).End(xlUp).Row
    
    title = "A2:H2"
    titlerow = ws.Range(title).Cells(1).Row
    titlerowOffset = ws.Range(title).Cells(1).Offset(1, 0).Row
    
    icol = ws.Columns.Count
    
    ws.Cells(1, icol) = "Unique"
    
    For i = 3 To lr
        On Error Resume Next
        If ws.Cells(i, vcol) <> "" And Application.WorksheetFunction.Match(ws.Cells(i, vcol), ws.Columns(icol), 0) = 0 Then
            ws.Cells(ws.Rows.Count, icol).End(xlUp).Offset(1) = ws.Cells(i, vcol)
        End If
    Next
    
    myarr = Application.WorksheetFunction.Transpose(ws.Columns(icol).SpecialCells(xlCellTypeConstants))
    'Clears the column where icol is
    ws.Columns(icol).Clear
    
    For i = 2 To UBound(myarr)
        ws.Range(title).AutoFilter field:=vcol, Criteria1:=myarr(i) & ""
        If Not Evaluate("=ISREF('" & myarr(i) & "'!A1)") Then
            Sheets.Add(after:=Worksheets(Worksheets.Count)).Name = myarr(i) & ""
        Else
            Sheets(myarr(i) & "").Move after:=Worksheets(Worksheets.Count)
        End If

        ws.Range("A" & titlerow & ":A" & titlerow).EntireRow.Copy Sheets(myarr(i) & "").Range("A1")
        
        THE LINE I'M TRYING TO MODIFY
        ws.Range("A" & titlerowOffset & ":A" & lr).EntireRow.Copy Sheets(myarr(i) & "").Range("A2")
        

        Sheets(myarr(i) & "").Columns.AutoFit
        Call SetPageBreaks
    Next
    'Call SortWorkBook
    ws.AutoFilterMode = False
    ws.Activate
   
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.Calculation = xlAutomatic
   
End Sub

Kind regards,

Doug.
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Hi Folks,

Just a quick note to say that I've worked it out:
For anyone interested...

VBA Code:
        ws.Range("A" & titlerowOffset & ":A" & lr).EntireRow.Copy Sheets(myarr(i) & "").Cells(Rows.Count, 1).End(xlUp).Offset(1)

VBA Code:
Sub CopyDataToPalletTabs()

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlManual

    Dim lr As Long
    Dim Ir2 As Long
    Dim ws As Worksheet
    Dim vcol, i As Integer
    Dim icol As Long
    Dim myarr As Variant
    Dim title As String
    Dim titlerow As Integer
    Dim titlerowOffset As Integer
    
    vcol = 6
    
    'Set ws
    Set ws = Sheets("ManifestMaster")

    lr = ws.Cells(ws.Rows.Count, vcol).End(xlUp).Row

    title = "A2:H2"
    titlerow = ws.Range(title).Cells(1).Row
    titlerowOffset = ws.Range(title).Cells(1).Offset(1, 0).Row
    
    icol = ws.Columns.Count
    
    ws.Cells(1, icol) = "Unique"
    
    For i = 3 To lr
        On Error Resume Next
        If ws.Cells(i, vcol) <> "" And Application.WorksheetFunction.Match(ws.Cells(i, vcol), ws.Columns(icol), 0) = 0 Then
            ws.Cells(ws.Rows.Count, icol).End(xlUp).Offset(1) = ws.Cells(i, vcol)
        End If
    Next
    
    myarr = Application.WorksheetFunction.Transpose(ws.Columns(icol).SpecialCells(xlCellTypeConstants))
    'Clears the column where icol is
    ws.Columns(icol).Clear
    
    Set ws2 = Sheets(myarr(i) & "")
    Ir2 = ws2.Cells(ws2.Rows.Count, 1).End(xlUp).Row
    
    For i = 2 To UBound(myarr)
        ws.Range(title).AutoFilter field:=vcol, Criteria1:=myarr(i) & ""
        If Not Evaluate("=ISREF('" & myarr(i) & "'!A1)") Then
            Sheets.Add(after:=Worksheets(Worksheets.Count)).Name = myarr(i) & ""
        Else
            Sheets(myarr(i) & "").Move after:=Worksheets(Worksheets.Count)
        End If
        'This line of code copies the filtered data from title row down to the last row with data in it and pastes to range("A1") in the corresponding ws matching myarr value
        ws.Range("A" & titlerow & ":A" & titlerow).EntireRow.Copy Sheets(myarr(i) & "").Range("A1")
        ws.Range("A" & titlerowOffset & ":A" & lr).EntireRow.Copy Sheets(myarr(i) & "").Cells(Rows.Count, 1).End(xlUp).Offset(1)
        
        'Fits column width to col cell values for that worksheet [myarr(i)]
        Sheets(myarr(i) & "").Columns.AutoFit
        Call SetPageBreaks
    Next

    ws.AutoFilterMode = False
    ws.Activate
   
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.Calculation = xlAutomatic
   
End Sub

Kind regards,

Doug
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,868
Members
449,053
Latest member
Mesh

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