Excel VBA automatically copy entire row to different sheets based on cell value

nikzulfaizan

New Member
Joined
Oct 9, 2014
Messages
14
Hi ...My excel have multiple sheets as below:

1) Master Data
2) Region A
3) Region B
4) Region C
5) Region D

I need to have a VBA coding that will COPY entire row automatically into a respective sheet if the cell value match according to the "REGION".

Whenever changes was made in the Master Data, it should auto reflect also at the respected sheets.

Below is the sample data for your ease of reference. Hope someone can help me and thank you in advanced... ;)

SHEET : MASTER DATA
ABCDEFG
1Summary
2
3RegionNameFloorSectionCubicleDepartment
4Region AAAA32A12Audit
5Region ABBB23C23Audit
6Region BCCC2A2Operation
7Region CDDD1A3Operation
8Region DEEE4D5Admin
9Region CFFF2C11Admin
10Region AGGG11B14HR
11Region DHHH4A24Admin

<tbody>
</tbody>
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Whenever changes was made in the Master Data
Are these changes to overwrite data in other sheets, or be added to the other sheets.
Does the exisiting data change ?
So do all region sheets need to be cleared each time new ata is added ?
 
Upvote 0
Try this:
This is an auto sheet event script
Your Workbook must be Macro enabled
To install this code:
Right-click on the Master sheet tab
Select View Code from the pop-up context menu
Paste the code in the VBA edit window

When you enter a sheet name in column B that row of data will be copied to that sheet.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Modified  9/10/2019  11:28:43 PM  EDT
If Target.Column = 2 And Target.Row > 3 Then
If Target.Cells.CountLarge > 1 Or IsEmpty(Target) Then Exit Sub
On Error GoTo M
Dim Lastrow As Long
Lastrow = Sheets(Target.Value).Cells(Rows.Count, "B").End(xlUp).Row + 1
Rows(Target.Row).Copy Sheets(Target.Value).Rows(Lastrow)
End If
Exit Sub
M:
MsgBox "The sheet named  " & Target.Value & "  Does not exist"
End Sub
 
Upvote 0
Hello Nikzulfaizan,

Another option:-


Code:
Sub Test()

          Dim ar As Variant, i As Integer
          Dim ws As Worksheet, sh As Worksheet
          ar = [{"Region A","Region B","Region C","Region D";"Region A","Region B","Region C","Region D"}]
          Set ws = Sheets("Master Data")
          
Application.ScreenUpdating = False

    For i = 1 To UBound(ar, 2)
           Set sh = Sheets(ar(1, i))
           sh.UsedRange.Offset(1).Clear
           With ws.Range("A3", ws.Range("A" & ws.Rows.Count).End(xlUp))
                    .AutoFilter 1, ar(2, i)
                    .Offset(1).EntireRow.Copy sh.Range("A" & Rows.Count).End(3)(2)
                    sh.Columns.AutoFit
                    .AutoFilter
           End With
    Next i
    
Application.ScreenUpdating = True

End Sub

The code assumes that data in the Master Data sheet starts in Row4 with headings in Row3.
Each destination sheet is refreshed each time the code is executed. No data is deleted from the Master Data sheet.

I hope that this helps.

Cheerio,
vcoolio.
 
Upvote 0
Are these changes to overwrite data in other sheets, or be added to the other sheets.
Does the exisiting data change ?
So do all region sheets need to be cleared each time new ata is added ?

Hi .. thank you for your response...

My answer : Yes, to overwrite and it's applies to all.
 
Upvote 0
Try this:
This is an auto sheet event script
Your Workbook must be Macro enabled
To install this code:
Right-click on the Master sheet tab
Select View Code from the pop-up context menu
Paste the code in the VBA edit window

When you enter a sheet name in column B that row of data will be copied to that sheet.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Modified  9/10/2019  11:28:43 PM  EDT
If Target.Column = 2 And Target.Row > 3 Then
If Target.Cells.CountLarge > 1 Or IsEmpty(Target) Then Exit Sub
On Error GoTo M
Dim Lastrow As Long
Lastrow = Sheets(Target.Value).Cells(Rows.Count, "B").End(xlUp).Row + 1
Rows(Target.Row).Copy Sheets(Target.Value).Rows(Lastrow)
End If
Exit Sub
M:
MsgBox "The sheet named  " & Target.Value & "  Does not exist"
End Sub

Greattttt ..... I love it ... How can i make it more flexible likes copy the entire data without enter 1 by 1. Actually the data i extract it from mysql database (export to excel format).... From raw data i copy and paste it to the sheet that we work on it. Once i paste it to the worksheet, the coding should be able to identify and run the coding...
 
Last edited:
Upvote 0
Your Subject Title said:
Excel VBA automatically copy entire row

My script runs when you enter the sheet name in column B

What do you mean by automatically?

Nothing happens automatically as far as I know.

I can write a script that runs when you click a button.


Greattttt ..... I love it ... How can i make it more flexible likes copy the entire data without enter 1 by 1. Actually the data i extract it from mysql database (export to excel format).... From raw data i copy and paste it to the sheet that we work on it. Once i paste it to the worksheet, the coding should be able to identify and run the coding...
 
Upvote 0
Hello Nikzulfaizan,

Another option:-


Code:
Sub Test()

          Dim ar As Variant, i As Integer
          Dim ws As Worksheet, sh As Worksheet
          ar = [{"Region A","Region B","Region C","Region D";"Region A","Region B","Region C","Region D"}]
          Set ws = Sheets("Master Data")
          
Application.ScreenUpdating = False

    For i = 1 To UBound(ar, 2)
           Set sh = Sheets(ar(1, i))
           sh.UsedRange.Offset(1).Clear
           With ws.Range("A3", ws.Range("A" & ws.Rows.Count).End(xlUp))
                    .AutoFilter 1, ar(2, i)
                    .Offset(1).EntireRow.Copy sh.Range("A" & Rows.Count).End(3)(2)
                    sh.Columns.AutoFit
                    .AutoFilter
           End With
    Next i
    
Application.ScreenUpdating = True

End Sub

The code assumes that data in the Master Data sheet starts in Row4 with headings in Row3.
Each destination sheet is refreshed each time the code is executed. No data is deleted from the Master Data sheet.

I hope that this helps.

Cheerio,
vcoolio.

Great .... I love it .... How can i copy the entire header and paste it....

My sample header range : "A1:G3"
 
Upvote 0
Your Subject Title said:
Excel VBA automatically copy entire row

My script runs when you enter the sheet name in column B

What do you mean by automatically?

Nothing happens automatically as far as I know.

I can write a script that runs when you click a button.

I can write a script that runs when you click a button. .--> Yes... Really appreciate it .... Btw, my sample header ranges : "A1:G3" .....
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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