Macro to copy data from a master workbook to other workbooks in the same folder

Tmini

New Member
Joined
Mar 22, 2014
Messages
44
Office Version
  1. 365
Platform
  1. Windows
Hi Guys
I have been trying to work out how to write a script to be able to copy a price from my main spreadsheet to all of my other spreadsheets in the same folder. Tee data I want to change will only be one specific cell and all of the spreadsheets will have the exact same data just the spreadsheets relate to different items. Rather than open up each individual spreadsheet I thought a macro would be perfect for doing this. I have been trying to do this and to find a solution but I can never seem to find something specific. I am trying to learn how to create my own macros but I am finding that rather difficult too. I know I need a loop command but I am not sure how to get it to open the next spreadsheet and then alter the required data. I have linked 3 spreadsheets, (https://www.dropbox.com/sh/8qbow8ixw1lxrud/CHG2K-sU2g ), as an example of what I am trying to do. For example I would like to change cell E4 from $12.00 to $22.00 in all the workbooks in the same folder. All formatting is exactly the same and there is only one worksheet in each file that I would like to change which I have named pricing. Eventually though I will be adding extra worksheets to the workbooks and giving them different labels but for now I am only using the one worksheet in each workbook. My macro enabled worksheet has a macro which I have copied from elsewhere but I cant seem to figure out what changes I need to do to get it to work. Any help on this would be greatly appreciated. I am still trying to learn VBA coding and I have a really long way to go
Thanks for any help it is greatly appreciated
 

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
Hi welcome to the board.

Firstly, I think Application.Filesearch is not available 2007> so code posted not much use to you.

See if code below helps. Place both procedures in your master worksbook Pricing code page (right click tab > View Code)

Code should after each entry in data range (A4 to G lastrow) run UpdateFilesInFolder procedure which hopefully will update all workbooks in the folder. You can change the specified folder where shown in RED.

Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim myrange As Range
    Dim Lastrow As Long
    Dim sFolder As String
    
    'folder where workbooks are stored
     'change as required
    sFolder = "C:\Users\Tristan\Dropbox\excel testing\"
   
    Lastrow = Me.Cells(Me.Rows.Count, "A").End(xlUp).Row + 1
    If Lastrow < 4 Then Lastrow = 4
    Application.ScreenUpdating = False
    Set myrange = Me.Range("A4:G" & Lastrow)
    If Not Intersect(Target, myrange) Is Nothing Then
        UpdateFilesInFolder Target, sFolder
    End If
    Application.ScreenUpdating = True
End Sub

Sub UpdateFilesInFolder(ByVal Target As Range, ByVal sFolder As String)
    Dim strFile As String
    
    strFile = Dir(sFolder & "*.xlsx", vbNormal)

    Do While strFile <> ""
        If strFile <> ThisWorkbook.Name Then
        Set wb = Workbooks.Open(sFolder & strFile, ReadOnly:=False)
        
        With wb
            .Worksheets("Pricing").Range(Target.Address).Value = Target.Value
            .Close True
        End With
        End If
        strFile = Dir
    Loop
End Sub

Dave
 
Upvote 0
Thanks for the help Dave. I have tried running it but it doesn't like this line of code -
Code:
Lastrow = Me.Cells(Me.Rows.Count, "A").End(xlUp).Row + 1
. It says Invalid use of Me keyword. Maybe I have done it wrong I have put the code in a module. Is this correct? I tried user form and class module but with no luck either.
 
Upvote 0
Thanks for the help Dave. I have tried running it but it doesn't like this line of code -
Code:
Lastrow = Me.Cells(Me.Rows.Count, "A").End(xlUp).Row + 1
. It says Invalid use of Me keyword. Maybe I have done it wrong I have put the code in a module. Is this correct? I tried user form and class module but with no luck either.


Place both procedures in your master workbook Pricing code page (right click tab > View Code)

Both places incorrect - Guidance where to place code was clearly given.

Dave
 
Upvote 0
H Dave
Thanks for that. I am such a Noob at all this and didn't realise you didn't need to code into a module. Thank you for your help. I have got a long way to go to learn how to use VBA
 
Upvote 0

Forum statistics

Threads
1,223,247
Messages
6,171,004
Members
452,374
Latest member
keccles

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