[Solved] copy rows with same name and save it to a new file

bigme

Member
hi friends,
i need some help for my task.
i have a dynamic data and what i want to do is copy and paste into new file for each rows that contains the same customer name and named the file with it and also sum the amount in column H.
herewith i attach the source file and the result of new file.
thank you for the help.

regards,
bigMe
 

Attachments

Hello bigme,

Thanks for reaching us. I understand that you want to copy and paste rows that contain the same customer name into new files that are named after customer names.

We can apply a VBA Macro to achieve that. From your “source” Workbook, press Alt + F11 to open the Visual Basic Editor. Afterward, Insert a Module and enter the following script:

Code:
Private Function UniqueValues(arr)
    Dim ret() As Variant
    ReDim ret(1 To 1)
    ret(1) = arr(1)

    For i = 2 To UBound(arr)
        test = False
        For j = 1 To UBound(ret)
            If arr(i) = ret(j) Then
                test = True
                Exit For
            End If
        Next j
        If test = False Then
            ReDim Preserve ret(1 To UBound(ret) + 1)
            ret(UBound(ret)) = arr(i)
        End If
    Next i

    UniqueValues = ret

End Function

Sub CopyDataToIndividualWB()

    Dim i As Integer
    Dim j As Integer
    Dim lastRow As Integer
    Dim cRow As Integer
    Dim sum As Variant
    Dim flPath As String
  
    Set sourceSheet = ThisWorkbook.Worksheets("Unit")
    lastRow = sourceSheet.Cells(Rows.Count, 2).End(xlUp).Row
    Set DataRange = sourceSheet.Range("A2:H" & lastRow)

    Dim custArr() As Variant
    Dim unqCustArr() As Variant

    ReDim custArr(1 To DataRange.Rows.Count)
    ReDim unqCustArr(1 To DataRange.Rows.Count)

    For i = 1 To UBound(custArr)
        custArr(i) = DataRange.Cells(i, 2).Value
    Next i

    unqCustArr = UniqueValues(custArr)

    For i = 1 To UBound(unqCustArr)
        'Change the file path according to your computer files
        flPath = "C:\Users\Seemanto Saha\Pictures\Forum Reply\FQ-124\"

        Set wb = Workbooks.Add
        wb.SaveAs flPath & unqCustArr(i) & ".xlsx"
        sourceSheet.Range("A1:H1").Copy wb.Sheets(1).Cells(1, 1)
       
        cRow = 2
        sum = 0

        For j = 1 To DataRange.Rows.Count
            If DataRange.Cells(j, 2) = unqCustArr(i) Then
                DataRange.Rows(j).Copy wb.Sheets(1).Cells(cRow, 1)
                sum = sum + DataRange.Cells(j, DataRange.Columns.Count).Value
                cRow = cRow + 1
            End If
        Next j

        wb.Sheets(1).Cells(cRow, DataRange.Columns.Count).Value = sum
        wb.Sheets(1).Cells.EntireColumn.AutoFit
        wb.Save
        wb.Close
    Next i
End Sub

Don’t forget to change the file path according to your computer file paths. Now, Save and Run the macro.
vJbGZ3HIv2KOQx15LI6nsXJtXTJOeDbgJ-GSntOBTqwpL_iO1pCkt3ZP7OjS_f213NYlp5HqDcnW-zk4lkCkmRKlHGdXuMmNfnCp4RI5YgH1-0_gmHHmNuNkX5IUV7jivcNQZoTQjFIbY4_27gGTJqM
Now, if you update the “source” sheet and want to update the other sheets dynamically, you can apply any of the following two methods:

Method-1:
Delete the previous workbooks and execute the previous VBA macro again.
Method-2:
We can apply another VBA macro incorporating a UserForm. For that, from your “source” Workbook, press Alt + F11 to open the Visual Basic Editor. Then Insert a UserForm and drag a Label, a RefEdit, and a CommandButton into the UserForm.
VaknrQ721yOdGunUupVpVGyS1xZEe9yvC8nI9YvtWdHYrgqnpf_t0QXXnNTH30MsHhY26TJTf6f1nxcgNi3IQZyZGg0Z1UElW9AIEvQFj-dyRRTDUneQDAtz54-klMhkPK9tV_OaNbYXfn8UQ77jMbs
Double-click on the CommandButton and insert the following VBA macro:

Code:
Public Function UniqueValues(arr)
    Dim ret() As Variant
    ReDim ret(1 To 1)
    ret(1) = arr(1)

    For i = 2 To UBound(arr)
        test = False
        For j = 1 To UBound(ret)
            If arr(i) = ret(j) Then
                test = True
                Exit For
            End If
        Next j
        If test = False Then
            ReDim Preserve ret(1 To UBound(ret) + 1)
            ret(UBound(ret)) = arr(i)
        End If
    Next i

    UniqueValues = ret

End Function

Private Sub CommandButton1_Click()

    If UserForm1.RefEdit1.Value = "" Then
        MsgBox "Please Select a Range."
        Exit Sub
    End If

    Set sourceSheet = ThisWorkbook.Worksheets("Unit")
    Set updateRng = Range(UserForm1.RefEdit1.Value)   

    If updateRng.Columns.Count < 8 Then
        MsgBox "Please Select All The Columns in Update Range."
        Exit Sub
    End If

    Dim i As Integer
    Dim j As Integer
    Dim nLastRow As Integer
    Dim newSum As Variant
    Dim cRow As Integer
    Dim arrCust() As Variant
    Dim arrUnqCust() As Variant

    ReDim arrCust(1 To updateRng.Rows.Count)
    ReDim arrUnqCust(1 To updateRng.Rows.Count)

    For i = 1 To updateRng.Rows.Count
        arrCust(i) = updateRng.Cells(i, 2).Value
    Next i

    arrUnqCust = UniqueValues(arrCust)

    Dim flPath As String

    For i = 1 To UBound(arrUnqCust)
        flPath = "C:\Users\Seemanto Saha\Pictures\Forum Reply\FQ-124\" & arrUnqCust(i) & ".xlsx"
       
        On Error Resume Next
        Workbooks.Open flPath

        If Err.Number <> 0 Then
            MsgBox "Failed to open the workbook from the path " & flPath
        End If

        On Error GoTo 0
       
        Set wb = Workbooks(arrUnqCust(i))
        Set ws = wb.Sheets(1)
       
        nLastRow = ws.Cells(Rows.Count, 2).End(xlUp).Row
        cRow = nLastRow + 1
        newSum = ws.Cells(cRow, 8).Value
        ws.Cells(cRow, 8).Value = ""

        For j = 1 To updateRng.Rows.Count
            If updateRng.Cells(j, 2) = arrUnqCust(i) Then
                updateRng.Rows(j).Copy ws.Cells(cRow, 1)
                newSum = newSum + updateRng.Cells(j, updateRng.Columns.Count).Value
                cRow = cRow + 1
            End If
        Next j

        ws.Cells(cRow, updateRng.Columns.Count).Value = newSum
        wb.Save
        wb.Close
        UserForm1.Hide
    Next i
End Sub

Don’t forget to change the file path according to your computer file locations. Now, let’s assume we added the following new Rows in the ‘source’ sheet.
elzfLYo_oJNVr_BGyd-_T5nsQHv3CxrYtnxZfFjimX2-PcffchuIu3qc0E5EtQDIxummV3-Dth7vZwwmy01JhAgJJnhX1qBEhrbJIZIpAohwVMQ3EdesOkd2HGQUvjxHVm9RRCGwItMSo1XGNeNSWr8
Now, Run the macro used in the UserForm. A form like the following will appear. Insert the range to be updated and press the Update button.
VgsyYbPowwHKlm0ne3BkQr4ENpAgfwcl_oZSDKSFn5ftTh3TMOXoLW4sl0yVFQi9W7xSLkyDteMmu1FO5bmOE54FjHtRpVI5zpPVrNt2Ny87vRn4FsrTQtRUzc3VlOZXgtQqNw9FJn6xiN1IKlDaKbY
The required workbooks will be updated. I’ll attach the ‘source’ file in the attachments.
I hope this will be helpful for your problem. Let us know your feedback.

Regards,
Seemanto Saha
ExcelDemy
 

Attachments

Last edited:

Online statistics

Members online
0
Guests online
1,452
Total visitors
1,452

Forum statistics

Threads
456
Messages
2,020
Members
1,886
Latest member
taixiuonlinecab
Back
Top