+ Reply to Thread
Results 1 to 17 of 17

Mirror cells across worksheets without hardcoding (much)

  1. #1
    Registered User
    Join Date
    02-22-2022
    Location
    Hong Kong
    MS-Off Ver
    Microsoft Office Pro Plus 2021
    Posts
    7

    Mirror cells across worksheets without hardcoding (much)

    I've found many solutions to mirror multiple cells across multiple worksheets in this forum, but they're so dauntingly hardcoded that they look like the equivalent of
    Please Login or Register  to view this content.
    And so on, quickly becoming unmanageable and hell to mantain, what if there're 20 worksheets and 200 mirrored cells? Good luck moving a single row/column, it WILL break everything.
    I'm not familiar with VBA, but is it possible to create the Python equivalent of:
    Please Login or Register  to view this content.
    This is visually much simpler to manage and mantain, each column represents the cells that are to be mirrored with their respective worksheet on the left
    Please Login or Register  to view this content.
    I can add new rows and columns without breaking anything, only one script that handles it all:
    Please Login or Register  to view this content.
    Here's a working Python 3 script you can try (I didn't implement all the logic, but it's to give you an idea)

    Please Login or Register  to view this content.
    This is the output after running it

    Please Login or Register  to view this content.
    Any chance something similar to this can be implemented in VBA?
    Thank you very much!

  2. #2
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: Mirror cells across worksheets without hardcoding (much)

    The basic logic would be using delimiters are the Split() function. Something like this:

    Please Login or Register  to view this content.
    WBD
    Office 365 on Windows 11, looking for rep!

  3. #3
    Registered User
    Join Date
    02-22-2022
    Location
    Hong Kong
    MS-Off Ver
    Microsoft Office Pro Plus 2021
    Posts
    7

    Re: Mirror cells across worksheets without hardcoding (much)

    Thank you for taking time in writing this code, I really appreciate it! I'm trying to do the same thing as the document attached here, but with a more manageable format, if I used your example, I guess it should look like this
    Please Login or Register  to view this content.
    But with the ability to add/remove new rows/columns and reflect these changes, such as.
    Please Login or Register  to view this content.
    Sadly I have zero VBA experience and don't know how implement this logic.
    P.S.= Using AT, otherwise it detects my post as a url and blocks me.
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Mirror cells across worksheets without hardcoding (much)


    According to Excel basics a sample how to build a VBA array :

    PHP Code: 
        Dim V
            V 
    = Array(["Sheet1!"&{"B1","B4","B8"}], ["Sheet2!"&{"B2","B3","B4"}], _
                      
    ["Sheet3!"&{"C5",""  ,"C7"}], ["Sheet4!"&{"C5","C6",""}]) 
    Seems weird you need 'empty cells' but it should be more obvious with an accurate explanation of the process …
    Last edited by Marc L; 02-22-2022 at 11:17 PM.

  5. #5
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Post Re: Mirror cells across worksheets without hardcoding (much)

    (removed 4 another try)
    Last edited by Marc L; 02-22-2022 at 11:56 PM.

  6. #6
    Registered User
    Join Date
    02-22-2022
    Location
    Hong Kong
    MS-Off Ver
    Microsoft Office Pro Plus 2021
    Posts
    7

    Re: Mirror cells across worksheets without hardcoding (much)

    If you consider that each column contains the values that have to be mirrored while first column defines which sheet names those values belong to. Ie. this Python example:
    Please Login or Register  to view this content.
    Look at the first column, if I open "Groceries" and write something in A1, that value would reflect in [Traveling] B1 [Transport] C1 [Treatment] D1 as well.
    The empty cells are required because they represents the values that I DO NOT want reflected, if I open "Groceries" and write something in A4, I do not want that value to be reflected in "Treatment" because it may be something completely unrelated to it.

    P.S.: Actually, if anyone can think of a better strategy to mirror multiple cells across multiple worksheets, I'm open to it.
    Last edited by AndroYD; 02-23-2022 at 12:17 AM.

  7. #7
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Re: Mirror cells across worksheets without hardcoding (much)


    According to your attachment if you really want to play with arrays the event revamped for Sheet1 moodule :

    PHP Code: 
    Private Sub Worksheet_Change(ByVal Target As Range)
        
    Dim V
            
    If Target.CountLarge 1 Then Exit Sub
        With Application
            V 
    = .Match(Target.Address(00), [{"B1","B4"}], 0)
        If 
    IsNumeric(VThen
           
    .EnableEvents False
        
    For Each V In Array([{"Sheet2!B2","Sheet3!C5"}], [{"Sheet2!B3","Sheet3!C6"}])(1)
            
    Range(V).Value2 Target.Value2
        Next
            
    .EnableEvents True
        End 
    If
        
    End With
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

  8. #8
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb Try this !


    According to your post #6 sample 'Groceries' is useless within the array as the event must be in the Groceries worksheet module :

    PHP Code: 
    Private Sub Worksheet_Change(ByVal Target As Range)
        
    Dim VC&, R&
            If 
    Target.CountLarge 1 Then Exit Sub
            V 
    = Array([{"",         "A1","A2","A3","A4"}], _
                      
    [{"Traveling","B1","B2","B3","B4"}], _
                      
    [{"Transport","C1","C2","C3","C4"}], _
                      
    [{"Treatment","D1",""  ,"D3",""}])
        For 
    2 To UBound(V(0))
            If 
    Target.Address(00) = V(0)(CThen
                Application
    .EnableEvents False
            
    For 1 To UBound(V)
                If 
    V(R)(C) > "" Then Sheets(V(R)(1)).Range(V(R)(C)).Value2 Target.Value2
            Next
                Application
    .EnableEvents True
                
    Exit For
            
    End If
        
    Next
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

  9. #9
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb Or this …


    According to Excel basics another way to build the 'Groceries' array :

    PHP Code: 
    Private Sub Worksheet_Change(ByVal Target As Range)
        
    Dim VC&, R&
            If 
    Target.CountLarge 1 Then Exit Sub
            V 
    Evaluate("{"""",         ""A1"",""A2"",""A3"",""A4"";" _
                          
    """Traveling"",""B1"",""B2"",""B3"",""B4"";" _
                          
    """Transport"",""C1"",""C2"",""C3"",""C4"";" _
                          
    """Treatment"",""D1"",""""  ,""D3"",""""}")
        For 
    2 To UBound(V2)
            If 
    Target.Address(00) = V(1CThen
                Application
    .EnableEvents False
            
    For 2 To UBound(V)
                If 
    V(RC) > "" Then Sheets(V(R1)).Range(V(RC)).Value2 Target.Value2
            Next
                Application
    .EnableEvents True
                
    Exit For
            
    End If
        
    Next
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

  10. #10
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Mirror cells across worksheets without hardcoding (much)


    Quote Originally Posted by AndroYD View Post
    Actually, if anyone can think of a better strategy to mirror multiple cells across multiple worksheets, I'm open to it.
    Same strategy but using a 'Settings' worksheet rather than hardcoding anything …

  11. #11
    Registered User
    Join Date
    02-22-2022
    Location
    Hong Kong
    MS-Off Ver
    Microsoft Office Pro Plus 2021
    Posts
    7

    Re: Or this …

    Woah! Thank you so much for your help! This is hands down the best mirroring solution you can find here in this forum by far imo! It's still a bit hardcodey, in the sense you have to modify the code for every sheet on the first array if you want to mirror both ways (ie. Sheet X <-> Sheet Y rather than Sheet X -> Sheet Y) but it's still a huge improvement compared to any other mirroring solutions available in this forum that are impossible to manage on a large scale. I have no experience in VBA so I can't tell if there're any hard limitations with this language, but in Python it's completely possible to keep all the mirroring rules in one place (script/sheet/module) that acts like an information box, while all the other sheets call this information box to collect their mirroring rules. So when I want to make a modification, there's just one script to change, I don't need to edit every script for every sheet. Makes sense?

  12. #12
    Registered User
    Join Date
    02-22-2022
    Location
    Hong Kong
    MS-Off Ver
    Microsoft Office Pro Plus 2021
    Posts
    7

    Re: Mirror cells across worksheets without hardcoding (much)

    Quote Originally Posted by Marc L View Post

    Same strategy but using a 'Settings' worksheet rather than hardcoding anything …

    I have thought of using a 'Settings' worksheet, that wouldn't even require any VBA knowledge, but unfortunately it's very inconvenient when the choice of data is based on what is directly observable on the sheets in real-time, lots of switching.

  13. #13
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Mirror cells across worksheets without hardcoding (much)


    Maybe but no needs to amend the code and best you do not ever need any event procedure within each worksheet module
    but only a single event located in the Thisworkbook module …

  14. #14
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: Mirror cells across worksheets without hardcoding (much)

    I put this into the "ThisWorkbook" module:

    Please Login or Register  to view this content.
    This keeps the cells mirrored both ways. See attached.

    WBD
    Attached Files Attached Files

  15. #15
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Or this …


    Quote Originally Posted by AndroYD View Post
    but in Python it's completely possible to keep all the mirroring rules in one place
    According to Python :


  16. #16
    Registered User
    Join Date
    02-22-2022
    Location
    Hong Kong
    MS-Off Ver
    Microsoft Office Pro Plus 2021
    Posts
    7

    Re: Mirror cells across worksheets without hardcoding (much)

    Woah! This is absolutely perfect, makes the job so much easier to handle! I think you've won the crown for best mirroring solution in this forum, this is by far the smarter and most convenient way to handle it IMO, so simple yet effective!

  17. #17
    Registered User
    Join Date
    02-22-2022
    Location
    Hong Kong
    MS-Off Ver
    Microsoft Office Pro Plus 2021
    Posts
    7

    Re: Or this …

    The problem of using Python to handle Excel is portability, VBA code is embedded and natively working by default as-is without needing any extras and licenses to buy.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. 2-way mirror of multiple cells across multiple worksheets
    By buidiena3 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-19-2020, 01:14 AM
  2. 2-way mirror of multiple cells across multiple worksheets
    By samkeane83 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-20-2020, 03:16 AM
  3. Need to Mirror Worksheets with ability to add or delete cells with limited data
    By chrissy77 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-09-2017, 01:44 AM
  4. mirror data between cells in different worksheets
    By RobbieC255 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-16-2016, 11:12 AM
  5. mirror data between cells in different worksheets
    By dumdum in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 09-19-2016, 02:40 AM
  6. Mirror cells - two worksheets, random cells. Attempted code provided
    By ld2x07 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-18-2014, 09:30 AM
  7. VBA to mirror cells between worksheets conditional to cell color
    By Guy Waggoner in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-25-2013, 11:43 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1