+ Reply to Thread
Results 1 to 6 of 6

How to shorten path in formulas?

Hybrid View

  1. #1
    Registered User
    Join Date
    05-07-2013
    Location
    Celje, Slovenia
    MS-Off Ver
    2016, 365
    Posts
    29

    How to shorten path in formulas?

    Hi!

    I have to insert a several formulas (Index - match) on the sheet, which finds the data in another workbook, like the one in the example below:

    Range("B2").Select
        ActiveCell.FormulaR1C1 = _
            "=INDEX('C:\Users\MyName\Documents\work\public tender - items\Public tender 2021\Additional\[Items-public tender JN004837-2021-repeated.xlsm]Items JN 1. part'!C1:C8,MATCH(RC8,'C:\Users\MyName\Documents\work\public tender - items\Public tender 2021\Additional\[Items-public tender JN004837-2021-repeated.xlsm]Items JN 1. part'!C8,0),2)"
    .

    It works fine, except that the path to another workbook is rather long, so I'd like to shorten it.

    I'd like to do something like:
    dim strFile as string
    strFile = "C:\Users\MyName\Documents\work\public tender - items\Public tender 2021\Additional\[Items-public tender JN004837-2021-repeated.xlsm]Items JN 1. part"
    but this isn't working correctly because it keeps opening a file dialog box and asking to update values for
    strFile & Items JN 1. part
    What is the right way to do it?

    I'm using Excel for Microsoft 365 for small businesses.
    Last edited by frankt68; 02-22-2022 at 05:29 AM. Reason: Solved

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,970

    Re: How to shorten path in formulas?

    The last thing you showed
    strFile & Items JN 1. part
    doesn't make sense to me. I don't know how you are using that.

    If you just want to shorten the code (it won't shorten what you see in the cell) try this:

    const strFile = "C:\Users\MyName\Documents\work\public tender - items\Public tender 2021\Additional\[Items-public tender JN004837-2021-repeated.xlsm]Items JN 1. part"
    
    Range("B2").Select
        ActiveCell.FormulaR1C1 = _
            "=INDEX('" & strFile  & "'!C1:C8,MATCH(RC8,'" & strFile  & "'!C8,0),2)"
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,266

    Re: How to shorten path in formulas?

    Quote Originally Posted by frankt68 View Post
    ... What is the ... way to do it? ...
    Same as above 6StringJazzer, only even more divided into parts:
    Option Explicit
    
    Sub aaa()
        
        Const pth = "C:\Users\MyName\Documents\work\public tender - items\Public tender 2021\Additional\"
        Const fle = "Items-public tender JN004837-2021-repeated.xlsm"
        Const ws = "Items JN 1. part"
        Const rge1 = "C1:C8"
        Const rge2 = "C8"
        
        Dim a As String, frmla As String, spth As String
        
        a = "=INDEX('C:\Users\MyName\Documents\work\public tender - items\Public tender 2021\Additional\" & _
            "[Items-public tender JN004837-2021-repeated.xlsm]Items JN 1. part'!C1:C8,MATCH(RC8," & _
            "'C:\Users\MyName\Documents\work\public tender - items\Public tender 2021\Additional\" & _
            "[Items-public tender JN004837-2021-repeated.xlsm]Items JN 1. part'!C8,0),2)"
        
        spth = "'" & pth & "[" & fle & "]" & ws & "'!"
        frmla = "=INDEX(" & spth & rge1 & ",MATCH(RC8," & spth & rge2 & ",0),2)"
        
        Debug.Print frmla
        Debug.Print a
        
        Application.DisplayAlerts = False
            Range("B2").FormulaR1C1 = frmla
        Application.DisplayAlerts = True
        
    End Sub

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

    Post Re: How to shorten path in formulas?

    (removed misread)
    Last edited by Marc L; 02-21-2022 at 11:54 AM.

  5. #5
    Registered User
    Join Date
    05-07-2013
    Location
    Celje, Slovenia
    MS-Off Ver
    2016, 365
    Posts
    29

    Re: How to shorten path in formulas?

    Thank you both mjr veverka and 6StringJazzer
    I've tried both suggestions and they both work fine.

  6. #6
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,970

    Re: How to shorten path in formulas?

    You are very welcome and thanks for the rep!

+ 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. [SOLVED] How to shorten COUNTIF formulas (without using VBA)
    By janmorris in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 07-24-2021, 12:29 PM
  2. [SOLVED] How to shorten up folder pathway with this workbook.path
    By bdouglas1011 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-17-2018, 07:35 PM
  3. Shorten File Path To Access Workbook
    By JimmiOO in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-24-2010, 11:26 AM
  4. linking path and formulas
    By dvb_24 in forum Excel General
    Replies: 0
    Last Post: 07-22-2009, 07:11 AM
  5. Path in formulas
    By Rob in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-09-2006, 01:25 PM
  6. [SOLVED] Shorten Formulas
    By MichelleExcelBeginner in forum Excel General
    Replies: 4
    Last Post: 09-29-2005, 03:05 PM
  7. How to shorten formulas??
    By dwest100 in forum Excel General
    Replies: 4
    Last Post: 09-29-2005, 06:05 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