Consulting

Results 1 to 15 of 15

Thread: Find all hard coded references

  1. #1
    VBAX Tutor
    Joined
    Mar 2010
    Posts
    287
    Location

    Question Find all hard coded references

    Hi all...back for some more sterling advice

    I have a project that opens another 20 workbooks (all with references to a main workbook). So under the VBE I have 20 separate projects.

    Within these project, there is heavy usage of hard coded ranges that I would now like to replace with named ranges e.g.

    $A1$Z54
    B7:B12
    $F$7:$F$12


    When I say heavy usage - I mean hundreds! Spread across 23,000+ lines of code.

    Is there a way to build a macro that will go through every line of code and do a LIKE operation or a REGEX operation so that I can capture all of these hard coded references into an array? If I get to this stage I am sure I can then take it further to dump the results into a worksheet :


    REF....BOOK.........MODULE.....LINE....VALUE
    1......Book1.xls....VB_Main....56......Sheet1!$A7:$Z15
    2......Book1.xls....VB_Main....59......Sheet2!$B$7:$Z14
    3......Book2.xls....VB_Func....5.......wks.Range("A7")

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    How would you distinquish between code like

    Target.EntrieRow.Range("D1")
    and
    Sheet1.Range("D1")

    You could use the .Find method of the .CodeModule object to find all the lines that conain the string ".Cells" or the string ".Range".
    Last edited by Aussiebear; 04-08-2023 at 01:35 PM. Reason: Adjusted the code tags

  3. #3
    VBAX Tutor
    Joined
    Mar 2010
    Posts
    287
    Location

    Question

    That's why i'm here to chat to the experts

    I was thinking it would be a REGEX to analyse items between " " that match the pattern :

    [T][N]...............A1........Single cell
    [T][N] : [T][N]......A1:A10....Range
    [T] : [T]............A:A.......Column
    [N] : [N]............1:1.......Row

    [T] = Text
    [N] = Number


    Anything else will be a named range, which is what I am trying to implement across all of the code

  4. #4
    VBAX Tutor
    Joined
    Mar 2010
    Posts
    287
    Location

    Question

    Rough pseudo, check every line of code for every workbook in VBE :

    Does line contain " * "
    --->Does the text within the " " match any of the REGEX patterns
    ------>If TRUE copy the text between " " and dump it on Book1.xls along with
    ------>workbook, module, line information
    Move on to next line and repeat


  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Waht should be the benefit of using named ranges over hardcoded ranges ?
    Named ranges are as hardcoded as any hardcoded range.
    If you prefer dynamic ranges you better use VBA's facilities: currentregion, areas, usedrange, specialcells(2), etc.
    The UserInterface lacks all those facilities, so a surrogate of named ranges was necessary.
    In my view a named range is a functionality you need in the userinterface; as soon as you turn to VBA it has no advantage.

  6. #6
    VBAX Tutor
    Joined
    Mar 2010
    Posts
    287
    Location

    Question

    I have several different workbooks that are loaded by this project.

    Users can hack and mash them up as much as they want, but there is a dynamic named range that catches the "data" element needed by VBA.

    I am now going through changing all hard coded references to refer to the dynamic named ranges I have not set up in these workbooks.

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by snb
    Named ranges are as hardcoded as any hardcoded range.
    Nonsense!

    If you have a named range rng pointing at say A1:M10, then to start

    Range("rng")

    and

    Range("A1:M10")

    are equivalent.

    Insert a row at row 5 say, and

    Range("rng").Address is now A1:M11

    whereas

    Range("A1:M10").Address is still A1:M10
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    VBAX Tutor
    Joined
    Mar 2010
    Posts
    287
    Location
    So any ideas on this? I can't even find any coding exampes to analyse every line of code within every VBE project.

    If I could do that, the REGEXP shouldn't be too hard :/ (he says)

  9. #9
    VBAX Expert shrivallabha's Avatar
    Joined
    Jan 2010
    Location
    Mumbai
    Posts
    750
    Location
    Maybe: Export all modules. And then analyze exported files as if they were text files.

    You can use following for this:
    http://www.appspro.com/Utilities/CodeCleaner.htm
    Regards,
    --------------------------------------------------------------------------------------------------------
    Shrivallabha
    --------------------------------------------------------------------------------------------------------
    Using Excel 2016 in Home / 2010 in Office
    --------------------------------------------------------------------------------------------------------

  10. #10
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    To retrieve all VBA code in all codemodules:

    Sub snb()
        For Each pr In Application.VBE.VBProjects
       For Each vc In pr.vbcomponents
          c01 = c01 & vbCr & vc.codemodule.Lines(1, vc.codemodule.countoflines)
       Next
        Next
    End Sub
    Last edited by Aussiebear; 04-08-2023 at 01:37 PM. Reason: Adjusted the code tags

  11. #11
    VBAX Tutor
    Joined
    Mar 2010
    Posts
    287
    Location

    Wink

    Thanks snb. I have built a function now and am testing it. Will upload when tested fully - for some advice on making it efficient

  12. #12
    Quote Originally Posted by snb
    Waht should be the benefit of using named ranges over hardcoded ranges ?...
    In my view a named range is a functionality you need in the userinterface; as soon as you turn to VBA it has no advantage.
    I totally disagree. If but for one reason: Readability!

    This is probably a preference thing but I find named ranges far more readable, especially at a future date (when the project is over) OR if someone else is looking at it.

    list_UserNames is more understandable than $B32:$D50 inside a formula. This includes VB code.

    Just one coders opinion
    Toby
    Portland, Oregon

  13. #13
    Quote Originally Posted by shrivallabha
    Maybe: Export all modules. And then analyze exported files as if they were text files.

    You can use following for this:
    http://www.appspro.com/Utilities/CodeCleaner.htm
    MZ-Tools would be better. I use both but for a Find/Replace MZ-Tools is it.
    Toby
    Portland, Oregon

  14. #14
    Won't you need to change all ranges in the cell formulas to the named range as well or are the ranges ONLY used in the VB Project?
    Toby
    Portland, Oregon

  15. #15
    VBAX Tutor
    Joined
    Mar 2010
    Posts
    287
    Location

    Red face

    I have been through all worksheets and created dynamic named ranges. Trying to get to the point where the VBA is "dumb" and just handles the inputs fed to it.

    Do not want it to run like a 'black box' where there is no visibility from the spreadsheet. All variables and ranges are stored in the sheet and fed to the VBA to process.

    Hopefully...

Posting Permissions

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