VBA Macro - Automatic Update "Master" data sheet from multiple sheet

fahadalambd

New Member
Joined
Sep 16, 2022
Messages
31
Office Version
  1. 2016
Platform
  1. Windows
Good Afternoon everyone,

Hope you are doing well. As you see the attached pics -

"Master Data" is my main data sheet.

I want a VBA script which will automatic update and move data in "Master" data sheet from Sheet1, Sheet2 and Sheet3.

It means if I change "Status" column in Sheet1, Sheet2 and Sheet3 then it will also change in "Master" data sheet.

NB. My actual Report has more than 10000 rows.

Thanks in advance :)
 

Attachments

  • Master Data.PNG
    Master Data.PNG
    9.6 KB · Views: 20
  • Sheet1.PNG
    Sheet1.PNG
    8.3 KB · Views: 19
  • Sheet2.PNG
    Sheet2.PNG
    8 KB · Views: 17
  • Sheet3.PNG
    Sheet3.PNG
    8.5 KB · Views: 19

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hi there,

Put this into a standard module (mine was called Module1)...

VBA Code:
Option Explicit
Sub UpdateStatusMaster(ID As Long, strStatus As String)

    Dim wsMaster As Worksheet
    Dim lngMatchRow As Long
    
    Set wsMaster = ThisWorkbook.Sheets("Master")
    
    lngMatchRow = Evaluate("IFERROR(MATCH(" & ID & ",'" & wsMaster.Name & "'!A:A,0),0)")
    
    If lngMatchRow > 0 Then
        wsMaster.Range("B" & lngMatchRow).Value = strStatus
    End If

End Sub

...and this worksheet event macro on each sheet that will you want to update the Master sheet:

VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)

On Error GoTo ErrTrap

    If Target.Column = 2 And Target.Row >= 2 Then
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
        
        Call Module1.UpdateStatusMaster(Target.Offset(0, -1).Value, Target.Value) '<-Module1 is the name of the module that houses the 'UpdateStatusMaster' macro. Change to suit if necessary.
        
        With Application
            .EnableEvents = True
            .ScreenUpdating = True
        End With
    
    End If
    
Exit Sub

ErrTrap:

    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With

End Sub

Regards,

Robert
 
Upvote 0
Hi there,

Put this into a standard module (mine was called Module1)...

VBA Code:
Option Explicit
Sub UpdateStatusMaster(ID As Long, strStatus As String)

    Dim wsMaster As Worksheet
    Dim lngMatchRow As Long
   
    Set wsMaster = ThisWorkbook.Sheets("Master")
   
    lngMatchRow = Evaluate("IFERROR(MATCH(" & ID & ",'" & wsMaster.Name & "'!A:A,0),0)")
   
    If lngMatchRow > 0 Then
        wsMaster.Range("B" & lngMatchRow).Value = strStatus
    End If

End Sub

...and this worksheet event macro on each sheet that will you want to update the Master sheet:

VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)

On Error GoTo ErrTrap

    If Target.Column = 2 And Target.Row >= 2 Then
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
       
        Call Module1.UpdateStatusMaster(Target.Offset(0, -1).Value, Target.Value) '<-Module1 is the name of the module that houses the 'UpdateStatusMaster' macro. Change to suit if necessary.
       
        With Application
            .EnableEvents = True
            .ScreenUpdating = True
        End With
   
    End If
   
Exit Sub

ErrTrap:

    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With

End Sub

Regards,

Robert
Hi Robert,

Thanks for the reply but this not the answer what I want.

For example, as you in the pic - I want to move the "Status" of the ID 111 (which is "Done") into the "Master Data Sheet".

Please let me know if you know how to solve it. It will be really helpful me.

Thanks
 
Upvote 0
For example, as you in the pic - I want to move the "Status" of the ID 111 (which is "Done") into the "Master Data Sheet".

So my code will copy the status type to the Master tab as long as there's a corresponding ID. If there is do you then want the row to be deleted from the originating sheet i.e. Sheet1 for example?
 
Upvote 0
If it's purely the status of the ID that you want returned, why not just use a VLOOKUP() formula instead of VBA?
VLOOKUP.xlsx
AB
1IDStatus
2111Done
3112Done
4113Not Done
5114Not Done
6115Done
7116Done
8117Withdraw
9118Withdraw
10119Withdraw
11120Done
12121Done
13122Not Done
14123Not Done
15124Withdraw
16125Withdraw
17126Done
18127Done
19128Not Done
20129Not Done
21130Not Found
22131Withdraw
23132Done
24133Not Done
Master
Cell Formulas
RangeFormula
B2:B24B2=IFERROR(VLOOKUP(A2,Sheet1!$A:$B,2,0),IFERROR(VLOOKUP(A2,Sheet2!$A:$B,2,0),IFERROR(VLOOKUP(A2,Sheet3!$A:$B,2,0),"Not Found")))

VLOOKUP.xlsx
AB
1IDStatus
2111Done
3112Done
4113Not Done
5114Not Done
6115Done
7
Sheet1

VLOOKUP.xlsx
AB
1IDStatus
2116Done
3117Withdraw
4118Withdraw
5119Withdraw
6120Done
7121Done
8122Not Done
9123Not Done
10124Withdraw
11125Withdraw
Sheet2

VLOOKUP.xlsx
AB
1IDStatus
2126Done
3127Done
4128Not Done
5129Not Done
6131Withdraw
7132Done
8133Not Done
Sheet3
 
Upvote 0
Solution
Thank you so much Kevin. I didn't think about "Vlookup" formula :D but it is working.

It is working perfectly.Thanks again :D
 
Upvote 0
Yes - thanks Kevin. Based on the thread title I instantly thought of a macro solution not a formula solution.
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,226
Members
452,620
Latest member
dsubash

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