Transfer row data from one workbook to another based on cell

keysaf

New Member
Joined
Jan 25, 2022
Messages
2
Office Version
  1. 365
Platform
  1. MacOS
Hey, I’m looking for a way to make a main table; after assigning a row to a specific person (by choosing his name from a drop-down list at the end of the row), all rows will be automatically transferred to a new row in another excel workbook. If possible, changes to the main table will also be updated in the other table.

For example: (All rows assigned to Ben will be copied to a new workbook named Ben, and rows assigned to Adam will be copied to another workbook named Adam)

NameScoreDateAssign
David75612.12.2003Ben
Noam43512/31/2004Ben
Neomi56311.23.2013Adam
Lee53605.25.2000Ben
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Hi keysaf,

Welcome to MrExcel!!

Try the following macro (changing the variables I've put a comment next to) which is virtually a clone of my response to juneau730's request here:

VBA Code:
Option Explicit
Sub Macro1()

    Dim wsSrc As Worksheet
    Dim strSavePath As String, strLastCol As String, strSrcCol As String
    Dim clnMyItems As New Collection
    Dim lngMyRow As Long, lngLastRow As Long, lngLastCol As Long, i As Long
    Dim varItem As Variant
    Dim rngFiltered As Range
    Dim wb As Workbook
    
    Application.ScreenUpdating = False
    
    strSrcCol = "D" 'Column to base the creation of the new workbook(s) on. Change to suit if necessary.
    Set wsSrc = ThisWorkbook.Sheets("Sheet1") 'Sheet name containing the divisions in column 'srcCol'. Change to suit if necessary.
    On Error Resume Next
        wsSrc.ShowAllData
    On Error GoTo 0
    strSavePath = "C:\keysaf\" 'Path to save individual division workbooks. This will need to be changed to meet your specific needs.
    strSavePath = IIf(Right(strSavePath, 1) <> "\", strSavePath & "\", strSavePath)
    lngLastRow = wsSrc.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    lngLastCol = wsSrc.Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
    strLastCol = Split(wsSrc.Cells(lngLastRow, lngLastCol).Address, "$")(1)
    
    'Create an unique list of items
    For lngMyRow = 2 To lngLastRow
        On Error Resume Next
            clnMyItems.Add CStr(wsSrc.Range(strSrcCol & lngMyRow)), wsSrc.Range(strSrcCol & lngMyRow)
        On Error GoTo 0
    Next lngMyRow
    
    'Create and save a new workbook for each division (the new workbook will only have the sheet with the data)
    For Each varItem In clnMyItems
        wsSrc.Range("$A$1:$" & strLastCol & "$" & lngLastRow).AutoFilter Field:=Range(strSrcCol & ":" & strSrcCol).Column, Criteria1:=CStr(varItem), Operator:=xlFilterValues
        Set rngFiltered = wsSrc.Range("$A$1:$" & strLastCol & "$" & lngLastRow).SpecialCells(xlCellTypeVisible)
        If Not rngFiltered Is Nothing Then
            Set wb = Workbooks.Add(1)
            rngFiltered.Copy
            With wb.Sheets(1).Range("A1")
                .PasteSpecial xlPasteFormats
                .PasteSpecial xlPasteValues
                .Select
            End With
            i = i + 1
            Application.DisplayAlerts = False 'Save over a workbook with the same name, no questions asked
                wb.SaveAs strSavePath & CStr(varItem) & ".xlsx", 51 '51 = xlOpenXMLWorkbook (without macro's in 2007-2016, xlsx). Change to suit if necessary.
                wb.Close SaveChanges:=False
            Application.DisplayAlerts = True
        End If
        On Error Resume Next
            wsSrc.ShowAllData
        On Error GoTo 0
    Next varItem
    
    Application.ScreenUpdating = False
    
    If i = 0 Then
        MsgBox "There were no unique entries in Col. E of """ & wsSrc.Name & """ to create any workbooks.", vbExclamation
    Else
        MsgBox Format(i, "#,##0") & " workbooks have now been saved in """ & strSavePath & """", vbInformation
    End If

End Sub

Regards,

Robert
 
Upvote 0
Hi keysaf,

Welcome to MrExcel!!

Try the following macro (changing the variables I've put a comment next to) which is virtually a clone of my response to juneau730's request here:

VBA Code:
Option Explicit
Sub Macro1()

    Dim wsSrc As Worksheet
    Dim strSavePath As String, strLastCol As String, strSrcCol As String
    Dim clnMyItems As New Collection
    Dim lngMyRow As Long, lngLastRow As Long, lngLastCol As Long, i As Long
    Dim varItem As Variant
    Dim rngFiltered As Range
    Dim wb As Workbook
   
    Application.ScreenUpdating = False
   
    strSrcCol = "D" 'Column to base the creation of the new workbook(s) on. Change to suit if necessary.
    Set wsSrc = ThisWorkbook.Sheets("Sheet1") 'Sheet name containing the divisions in column 'srcCol'. Change to suit if necessary.
    On Error Resume Next
        wsSrc.ShowAllData
    On Error GoTo 0
    strSavePath = "C:\keysaf\" 'Path to save individual division workbooks. This will need to be changed to meet your specific needs.
    strSavePath = IIf(Right(strSavePath, 1) <> "\", strSavePath & "\", strSavePath)
    lngLastRow = wsSrc.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    lngLastCol = wsSrc.Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
    strLastCol = Split(wsSrc.Cells(lngLastRow, lngLastCol).Address, "$")(1)
   
    'Create an unique list of items
    For lngMyRow = 2 To lngLastRow
        On Error Resume Next
            clnMyItems.Add CStr(wsSrc.Range(strSrcCol & lngMyRow)), wsSrc.Range(strSrcCol & lngMyRow)
        On Error GoTo 0
    Next lngMyRow
   
    'Create and save a new workbook for each division (the new workbook will only have the sheet with the data)
    For Each varItem In clnMyItems
        wsSrc.Range("$A$1:$" & strLastCol & "$" & lngLastRow).AutoFilter Field:=Range(strSrcCol & ":" & strSrcCol).Column, Criteria1:=CStr(varItem), Operator:=xlFilterValues
        Set rngFiltered = wsSrc.Range("$A$1:$" & strLastCol & "$" & lngLastRow).SpecialCells(xlCellTypeVisible)
        If Not rngFiltered Is Nothing Then
            Set wb = Workbooks.Add(1)
            rngFiltered.Copy
            With wb.Sheets(1).Range("A1")
                .PasteSpecial xlPasteFormats
                .PasteSpecial xlPasteValues
                .Select
            End With
            i = i + 1
            Application.DisplayAlerts = False 'Save over a workbook with the same name, no questions asked
                wb.SaveAs strSavePath & CStr(varItem) & ".xlsx", 51 '51 = xlOpenXMLWorkbook (without macro's in 2007-2016, xlsx). Change to suit if necessary.
                wb.Close SaveChanges:=False
            Application.DisplayAlerts = True
        End If
        On Error Resume Next
            wsSrc.ShowAllData
        On Error GoTo 0
    Next varItem
   
    Application.ScreenUpdating = False
   
    If i = 0 Then
        MsgBox "There were no unique entries in Col. E of """ & wsSrc.Name & """ to create any workbooks.", vbExclamation
    Else
        MsgBox Format(i, "#,##0") & " workbooks have now been saved in """ & strSavePath & """", vbInformation
    End If

End Sub

Regards,

Robert

Thank you for your quick reply! I don't understand a lot in macros, but I have a question by reading your comments and the original request.
Does this macro also update the created workbook after running it the second time? Or will it create a new workbook?

The idea is to share with OneDrive the new workbooks so they work off it, and when there is further information, the macro will copy it to the existing workbook.
 
Upvote 0
Try this (assumes existing workbooks have a xlsx extension):

VBA Code:
Option Explicit
Sub Macro1()

    Dim wsSrc As Worksheet
    Dim strSavePath As String, strLastCol As String, strSrcCol As String
    Dim clnMyItems As New Collection
    Dim lngMyRow As Long, lngLastRow As Long, lngLastCol As Long, i As Long
    Dim varItem As Variant
    Dim rngFiltered As Range
    Dim wb As Workbook
    Dim blnDoesFileExist As Boolean, blnWasFileOpen As Boolean
   
    Application.ScreenUpdating = False
   
    strSrcCol = "D" 'Column to base the creation of the new workbook(s) on. Change to suit if necessary.
    Set wsSrc = ThisWorkbook.Sheets("Sheet1") 'Sheet name containing the divisions in column 'srcCol'. Change to suit if necessary.
    On Error Resume Next
        wsSrc.ShowAllData
    On Error GoTo 0
    strSavePath = "C:\keysaf\" 'Path to save individual division workbooks. This WILL need to be changed to meet your specific needs.
    strSavePath = IIf(Right(strSavePath, 1) <> "\", strSavePath & "\", strSavePath)
    lngLastRow = wsSrc.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    lngLastCol = wsSrc.Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
    strLastCol = Split(wsSrc.Cells(lngLastRow, lngLastCol).Address, "$")(1)
   
    'Create an unique list of items
    For lngMyRow = 2 To lngLastRow
        On Error Resume Next
            clnMyItems.Add CStr(wsSrc.Range(strSrcCol & lngMyRow)), wsSrc.Range(strSrcCol & lngMyRow)
        On Error GoTo 0
    Next lngMyRow
   
    'Create and save a new workbook for each item if the workbook does not exist (the new workbook will only have the sheet with the data) or else append the data to the first sheet
    For Each varItem In clnMyItems
        wsSrc.Range("$A$1:$" & strLastCol & "$" & lngLastRow).AutoFilter Field:=Range(strSrcCol & ":" & strSrcCol).Column, Criteria1:=CStr(varItem), Operator:=xlFilterValues
        blnDoesFileExist = Len(Dir(strSavePath & CStr(varItem) & ".xlsx"))
        If blnDoesFileExist = False Then
            Set rngFiltered = wsSrc.Range("$A$1:$" & strLastCol & "$" & lngLastRow).SpecialCells(xlCellTypeVisible)
            If Not rngFiltered Is Nothing Then
                i = i + 1
                Set wb = Workbooks.Add(1)
                rngFiltered.Copy
                With wb.Sheets(1).Range("A1")
                    .PasteSpecial xlPasteFormats
                    .PasteSpecial xlPasteValues
                    .Select
                End With
                Application.DisplayAlerts = False 'Save over a workbook with the same name, no questions asked
                    wb.SaveAs strSavePath & CStr(varItem) & ".xlsx", 51 '51 = xlOpenXMLWorkbook (without macro's in 2007-2016, xlsx). Change to suit if necessary.
                    wb.Close SaveChanges:=False
                Application.DisplayAlerts = True
            End If
        Else
            On Error Resume Next
                Set wb = Workbooks(CStr(varItem) & ".xlsx")
                If wb Is Nothing Then
                    Set wb = Workbooks.Open(strSavePath & CStr(varItem) & ".xlsx")
                    blnWasFileOpen = False
                Else
                    blnWasFileOpen = True
                End If
            On Error GoTo 0
            Set rngFiltered = wsSrc.Range("$A$2:$" & strLastCol & "$" & lngLastRow).SpecialCells(xlCellTypeVisible)
            If Not rngFiltered Is Nothing Then
                 i = i + 1
                rngFiltered.Copy wb.Sheets(1).Range("A" & Rows.Count).End(xlUp).Offset(1, 0) 'Pastes the data into the first available row in Col. A into the first sheet (furthest left) of 'wb' workbook.
                If blnWasFileOpen = False Then
                    wb.Close True
                End If
            End If
        End If
        On Error Resume Next
            wsSrc.ShowAllData
        On Error GoTo 0
    Next varItem
   
    Application.ScreenUpdating = False
   
    If i = 0 Then
        MsgBox "There were no unique entries in Col. " & strSrcCol & " of """ & wsSrc.Name & """ to create any workbooks.", vbExclamation
    Else
        MsgBox Format(i, "#,##0") & " workbooks have now been updated in """ & strSavePath & """", vbInformation
    End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,431
Messages
6,119,457
Members
448,898
Latest member
drewmorgan128

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