Copying rows automatically from one spread sheet to another when an entry is made

mikeb7500

Board Regular
Joined
Jul 30, 2014
Messages
98
I have a master payroll sheet(tab1), that when a sale is made by David, I enter David's name, date, amount of sale, commission etc. in a row. I would like to have this information(row) automatically COPY to a spreadsheet(tab2) named David automatically each time David makes a sale. The same when I enter a sale made by Mary, that when it's entered it also COPIES to a spreadsheet(tab3) named Mary...etc. What is the best way to do this...any ideas??? Thanks!
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Hi,

I made a sample master data sheet which looks like this:

ABCD
1NameDateAmount of saleComission
2David2014.01.2514394
3John2014.01.265142
4David2014.01.2712383
5Mary2014.01.287402
6Eva2014.01.2914532
7Lara2014.01.3014873
8John2014.01.3110034
9David2014.02.0114654
10Mary2014.02.0211671

<tbody>
</tbody>

Add master sheet called "MasterPayroll"+ add sheets and name them to "David" and "Mary" before run VBA code below. Code deletes cell content+ add headers to name sheets. You can change person name, header or name of master sheet as indicated by comments in code.

Code:
Sub CopyRowsForNames()


Dim Rng As Range
Dim i As Long, j As Long, Z As Long
Dim PersonName
Dim Header_


Application.ScreenUpdating = False


PersonName = Array("David", "Mary") 'you can add new names
Header_ = Array("Name", "Date", "Amount of sale", "Comission") 'Add new names for header if you want
Set Rng = ThisWorkbook.Sheets("MasterPayroll").UsedRange 'Change name of the master sheet from ""MasterPayroll"" if you want


For j = 0 To UBound(PersonName)
    Application.DisplayAlerts = False
        With Sheets(PersonName(j))
            .Cells.Clear
            .Cells(1, 1).Resize(, 4) = Header_
        End With
    Application.DisplayAlerts = True
Next j


For i = 2 To Rng.Rows.Count
    For j = 0 To UBound(PersonName)
        If Rng.Cells(i, 1) = PersonName(j) Then
            Z = Sheets(PersonName(j)).UsedRange.Rows.Count
            Rng.Rows(Rng.Cells(i, 1).Row).Copy Sheets(PersonName(j)).Cells(Z + 1, 1)
        End If
    Next j
Next i


Application.ScreenUpdating = True


End Sub

So if you enter a new name (add a new row) you can run this macro.

I hope it'll work for you.
 
Last edited:
Upvote 0
Hi,

I made a sample master data sheet which looks like this:

ABCD
1NameDateAmount of saleComission
2David2014.01.2514394
3John2014.01.265142
4David2014.01.2712383
5Mary2014.01.287402
6Eva2014.01.2914532
7Lara2014.01.3014873
8John2014.01.3110034
9David2014.02.0114654
10Mary2014.02.0211671

<tbody>
</tbody>

Add master sheet called "MasterPayroll"+ add sheets and name them to "David" and "Mary" before run VBA code below. Code deletes cell content+ add headers to name sheets. You can change person name, header or name of master sheet as indicated by comments in code.

Code:
Sub CopyRowsForNames()


Dim Rng As Range
Dim i As Long, j As Long, Z As Long
Dim PersonName
Dim Header_


Application.ScreenUpdating = False


PersonName = Array("David", "Mary") 'you can add new names
Header_ = Array("Name", "Date", "Amount of sale", "Comission") 'Add new names for header if you want
Set Rng = ThisWorkbook.Sheets("MasterPayroll").UsedRange 'Change name of the master sheet from ""MasterPayroll"" if you want


For j = 0 To UBound(PersonName)
    Application.DisplayAlerts = False
        With Sheets(PersonName(j))
            .Cells.Clear
            .Cells(1, 1).Resize(, 4) = Header_
        End With
    Application.DisplayAlerts = True
Next j


For i = 2 To Rng.Rows.Count
    For j = 0 To UBound(PersonName)
        If Rng.Cells(i, 1) = PersonName(j) Then
            Z = Sheets(PersonName(j)).UsedRange.Rows.Count
            Rng.Rows(Rng.Cells(i, 1).Row).Copy Sheets(PersonName(j)).Cells(Z + 1, 1)
        End If
    Next j
Next i


Application.ScreenUpdating = True


End Sub

So if you enter a new name (add a new row) you can run this macro.

I hope it'll work for you.

Thanks, KT...it looks like we're on the right track, but not quiet there. I recreated your spreadsheet, named it MasterPayroll, and added a David Tab and a Mary Tab. When I ran the Macro, the only thing that transferred to BOTH David's Tab and Mary's Tab were the Headings in Row 1:
NameDateAmount of saleComission

<tbody>
</tbody>

No Data was added. Sorry it took so long to give this a try. But still need your help...Thanks!
 
Upvote 0
Thanks, KT...it looks like we're on the right track, but not quiet there. I recreated your spreadsheet, named it MasterPayroll, and added a David Tab and a Mary Tab. When I ran the Macro, the only thing that transferred to BOTH David's Tab and Mary's Tab were the Headings in Row 1:
NameDateAmount of saleComission

<tbody>
</tbody>


No Data was added. Sorry it took so long to give this a try. But still need your help...Thanks!
 
Upvote 0
If names are the same in column A are called the same as sheet names ("David", "Mary") then code should work. It's not so easy to figure out why code is not working for you without the workbook. Here I changed code a little bit, you can see update in blue color.

Rich (BB code):
Sub CopyRowsForNames2()


Dim Rng As Range
Dim i As Long, j As Long, Z As Long
Dim PersonName
Dim Header_


Application.ScreenUpdating = False


PersonName = Array("David", "Mary") 'you can add new names
Header_ = Array("Name", "Date", "Amount of sale", "Comission") 'Add new names for header if you want
Set Rng = ThisWorkbook.Sheets("MasterPayroll").UsedRange 'Change name of the master sheet from ""MasterPayroll"" if you want


For j = 0 To UBound(PersonName)
    Application.DisplayAlerts = False
        With Sheets(PersonName(j))
            .Cells.Clear
            .Cells(1, 1).Resize(, 4) = Header_
        End With
    Application.DisplayAlerts = True
Next j


For i = 2 To Rng.Rows.Count
    For j = 0 To UBound(PersonName)
        If Rng.Cells(i, 1) = PersonName(j) Then
            Z = Sheets(PersonName(j)).UsedRange.Rows.Count
            Sheets(PersonName(j)).Range(Cells(Z + 1, 1).Address, Cells(Z + 1, 4).Address) = _
            Rng.Range(Cells(i, 1).Address, Cells(i, 4).Address).Value
        End If
    Next j
Next i


Application.ScreenUpdating = True


End Sub
 
Upvote 0
If names are the same in column A are called the same as sheet names ("David", "Mary") then code should work. It's not so easy to figure out why code is not working for you without the workbook. Here I changed code a little bit, you can see update in blue color.

Rich (BB code):
Sub CopyRowsForNames2()


Dim Rng As Range
Dim i As Long, j As Long, Z As Long
Dim PersonName
Dim Header_


Application.ScreenUpdating = False


PersonName = Array("David", "Mary") 'you can add new names
Header_ = Array("Name", "Date", "Amount of sale", "Comission") 'Add new names for header if you want
Set Rng = ThisWorkbook.Sheets("MasterPayroll").UsedRange 'Change name of the master sheet from ""MasterPayroll"" if you want


For j = 0 To UBound(PersonName)
    Application.DisplayAlerts = False
        With Sheets(PersonName(j))
            .Cells.Clear
            .Cells(1, 1).Resize(, 4) = Header_
        End With
    Application.DisplayAlerts = True
Next j


For i = 2 To Rng.Rows.Count
    For j = 0 To UBound(PersonName)
        If Rng.Cells(i, 1) = PersonName(j) Then
            Z = Sheets(PersonName(j)).UsedRange.Rows.Count
            Sheets(PersonName(j)).Range(Cells(Z + 1, 1).Address, Cells(Z + 1, 4).Address) = _
            Rng.Range(Cells(i, 1).Address, Cells(i, 4).Address).Value
        End If
    Next j
Next i


Application.ScreenUpdating = True


End Sub

KT, I haven't applied this code to my spreadsheet yet, I RECREATED YOUR SPREADSHEET, to see it work. (FYI, I have DAVID in cell B3 and Mary in cell B6). I copied and pasted the revised formula and unfortunately I have the SAME results. The only info that transfers is the same:
NameDateAmount of saleComission


<tbody>
</tbody>

Please check it on yours, because I copied your spreadsheet EXACTLY, as well as copying and pasting your code EXACTLY. PLEASE don't give up on me, we are SO close. Thanks
 
Upvote 0
Ok, I think I found root causes:
- it makes sense if you use "David" or "DAVID". Now I updated my code.
- it makes huge sense that your Name column in column B and not in A (as you originally showed on sample screen)

See revised code below. If column A is empty on sheet "MasterPayroll": no change is necessary. If you have data in column A on sheet "MasterPayroll": update this line as comment says:
Code:
Const ColumnID = 1 'if you have data in column "A" on sheet "MasterPayroll": change 1 to 2

Code:
Sub CopyRowsForNames3()


Dim Rng As Range
Dim i As Long, j As Long, Z As Long
Dim PersonName
Dim Header_
Const ColumnID = 1 'if you have data in column "A" on sheet "MasterPayroll": change 1 to 2


Application.ScreenUpdating = False


PersonName = Array("DAVID", "Mary") 'you can add new names
Header_ = Array("Name", "Date", "Amount of sale", "Comission") 'Add new names for header if you want
Set Rng = ThisWorkbook.Sheets("MasterPayroll").UsedRange 'Change name of the master sheet from ""MasterPayroll"" if you want


For j = 0 To UBound(PersonName)
    Application.DisplayAlerts = False
        With Sheets(PersonName(j))
            .Cells.Clear
            .Cells(1, 2).Resize(, 4) = Header_
        End With
    Application.DisplayAlerts = True
Next j


For i = 2 To Rng.Rows.Count
    For j = 0 To UBound(PersonName)
        If Rng.Cells(i, ColumnID) = PersonName(j) Then
            Z = Sheets(PersonName(j)).UsedRange.Rows.Count
            Sheets(PersonName(j)).UsedRange.Range(Cells(Z + 1, 1).Address, Cells(Z + 1, 4).Address) = _
            Rng.Range(Cells(i, ColumnID).Address, Cells(i, ColumnID + 3).Address).Value
        End If
    Next j
Next i


Application.ScreenUpdating = True


End Sub
 
Upvote 0
KT, I haven't applied this code to my spreadsheet yet, I RECREATED YOUR SPREADSHEET, to see it work. (FYI, I have DAVID in cell B3 and Mary in cell B6). I copied and pasted the revised formula and unfortunately I have the SAME results. The only info that transfers is the same:
NameDateAmount of saleComission

<tbody>
</tbody>

Please check it on yours, because I copied your spreadsheet EXACTLY, as well as copying and pasting your code EXACTLY. PLEASE don't give up on me, we are SO close. Thanks


KT, IT WORKS! Please ignore my last post, When I started, I actually typed rows 1,2,3, etc and columns a,b,c etc...I deleted those and re positioned David in cell A2 and Mary in B5. I'll let you know how it works in MY spreadsheet. Thanks!
 
Upvote 0
Great news, thanks. You can use my latest code on your original layout (as you wrote, names are in column B on sheet "MasterPayroll"). Please let me know if it works.
 
Upvote 0
Great news, thanks. You can use my latest code on your original layout (as you wrote, names are in column B on sheet "MasterPayroll"). Please let me know if it works.


I will KT. I do have a question though, when I add a NEW NAME to the list, with a corresponding TAB, it appears I will have to enter the name in the code manually, or will it do it automatically? If I have to enter it manually, is there a way to get it to work from a Drop Down List of Names. So as I add a new name (Mark) to the drop down list, and I add a (Mark) Tab, that it will work? Just wondering, if this is possible...that is what I'm trying to achieve...thanks I can get a copy of my spreadsheet if you would like to take a look at it...let me know. Thanks!
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,071
Latest member
cdnMech

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