Copy the data into template for each unique value of a row

salmabanu

New Member
Joined
Jan 27, 2009
Messages
14
HI,

I have a standard template of two worksheets and i have a data file with unique id in column "A" and data related to that in column "B" and "C". What i want to achieve is for each unique id i want to copy the rows from data file of column "B" to Sheet1 of template, and column "C" to sheet2 of template and save that template with unique id name found in data file.

I have attached the images of data file and template.

This is little urgent any help around this would be appreciated.
 

Attachments

  • DATA-File.JPG
    DATA-File.JPG
    44 KB · Views: 14
  • Template.JPG
    Template.JPG
    47.8 KB · Views: 15

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Try:
VBA Code:
Sub CopyData()
    Application.ScreenUpdating = False
    Dim lastRow As Long, rng As Range, srcWS As Worksheet, desWB As Workbook, RngList As Object, key As Variant
    Set srcWS = ThisWorkbook.Sheets("Data")
    Set desWB = Workbooks("Template.xlsx")
    Set RngList = CreateObject("Scripting.Dictionary")
    For Each rng In srcWS.Range("A2", srcWS.Range("A" & srcWS.Rows.Count).End(xlUp))
        If rng <> "" And Not RngList.Exists(rng.Value) Then
            RngList.Add rng.Value, Nothing
        End If
    Next
    With desWB
        .Sheets(1).UsedRange.Offset(4).ClearContents
        .Sheets(2).UsedRange.Offset(4).ClearContents
    End With
    For Each key In RngList
        With srcWS
            lastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            .Range("A1:C" & lastRow).AutoFilter Field:=1, Criteria1:=key
            .Range("B2", .Range("B" & .Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible).Copy
            With desWB.Sheets(1)
                lastRow = .Range("B" & .Rows.Count).End(xlUp).Row + 1
                .Cells(lastRow, 2).PasteSpecial xlPasteValues
                On Error Resume Next
                .Range("B5", .Range("B" & .Rows.Count).End(xlUp)).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
                On Error GoTo 0
            End With
            .Range("C2", .Range("C" & .Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible).Copy
            With desWB.Sheets(2)
                lastRow = .Range("B" & .Rows.Count).End(xlUp).Row + 1
                .Cells(lastRow, 2).PasteSpecial xlPasteValues
                On Error Resume Next
                .Range("B5", .Range("B" & .Rows.Count).End(xlUp)).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
                On Error GoTo 0
            End With
            Application.DisplayAlerts = False
            desWB.SaveAs ThisWorkbook.Path & "\" & "Test" & "\" & key & ".xlsx"
            Application.DisplayAlerts = True
        End With
    Next key
    srcWS.Range("A1").AutoFilter
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
It's still the same it's not copying the unique rows. I have uploaded the files it created after running the macro. Its retaining all the data from all unique ids.

Below is the dropbox link.

 
Upvote 0
Try:
VBA Code:
Sub CopyData()
    Application.ScreenUpdating = False
    Dim lastRow As Long, rng As Range, srcWS As Worksheet, desWB As Workbook, RngList As Object, key As Variant
    Set srcWS = ThisWorkbook.Sheets("Data")
    Set desWB = Workbooks("Template.xlsx")
    Set RngList = CreateObject("Scripting.Dictionary")
    For Each rng In srcWS.Range("A2", srcWS.Range("A" & srcWS.Rows.Count).End(xlUp))
        If rng <> "" And Not RngList.Exists(rng.Value) Then
            RngList.Add rng.Value, Nothing
        End If
    Next
    For Each key In RngList
        With desWB
            .Sheets(1).UsedRange.Offset(4).ClearContents
            .Sheets(2).UsedRange.Offset(4).ClearContents
        End With
        With srcWS
            lastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            .Range("A1:C" & lastRow).AutoFilter Field:=1, Criteria1:=key
            .Range("B2", .Range("B" & .Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible).Copy
            With desWB.Sheets(1)
                lastRow = .Range("B" & .Rows.Count).End(xlUp).Row + 1
                .Cells(lastRow, 2).PasteSpecial xlPasteValues
                On Error Resume Next
                .Range("B5", .Range("B" & .Rows.Count).End(xlUp)).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
                On Error GoTo 0
            End With
            .Range("C2", .Range("C" & .Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible).Copy
            With desWB.Sheets(2)
                lastRow = .Range("B" & .Rows.Count).End(xlUp).Row + 1
                .Cells(lastRow, 2).PasteSpecial xlPasteValues
                On Error Resume Next
                .Range("B5", .Range("B" & .Rows.Count).End(xlUp)).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
                On Error GoTo 0
            End With
            Application.DisplayAlerts = False
            desWB.SaveAs ThisWorkbook.Path & "\" & "Test" & "\" & key & ".xlsx"
            Application.DisplayAlerts = True
        End With
    Next key
    srcWS.Range("A1").AutoFilter
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,963
Messages
6,122,484
Members
449,088
Latest member
Melvetica

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