+ Reply to Thread
Results 1 to 5 of 5

To get rid of some contents from the path location string

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-20-2011
    Location
    Auckland, NZ
    MS-Off Ver
    Excel 2013
    Posts
    165

    To get rid of some contents from the path location string

    hi guys,

    i am trying to convey something in the message box..which is as follows:

            ' Promt
            strPrompt = ".xls file location titled 'Test.xls' can be found in location " & filespec & "Test.xls" & "." & vbCrLf & _
            "Please click 'OK' or 'Cancel' to exit this message box."
            
            ' Dialog's Title
            strTitle = "Test.xls Location"
            
            'Display MessageBox
            iRet = MsgBox(strPrompt, vbOK, strTitle)
            
            ' Check pressed button
            If iRet = vbOK Then
                Range("A9").Select
            Else
                ' do nothing
            End If
    filespec is "C:\something_else.xls"

    so the message pops up as ".xls file location titled 'Test.xls' can be found in location C:\something_else.xlsTest.xls. Please click 'OK' or 'Cancel' to exit this message box." now

    i need the message to pop up as ".xls file location titled 'Test.xls' can be found in location C:\Test.xls. Please click 'OK' or 'Cancel' to exit this message box."

    i need to get rid of "something_else.xls" from the file location path.

    any suggestion guys..

    thanks in advance guys..

    best regards
    plasma33
    Last edited by plasma33; 12-28-2014 at 09:57 PM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: To get rid of some contents from the path location string

    Hello plasma33,

    Herr is one to split the filespec up...
    Sub SplitFilespec()
    
        Dim File        As String
        Dim Filespec    As String
        Dim Folder      As String
        Dim n           As Long
        
            Filespec = "C:\test.xls"
            
            n = InStrRev(Filespec, "\")
            
            Folder = Left(Filespec, n)
            File = Right(Filespec, Len(Filespec) - n)
            
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Contributor
    Join Date
    06-20-2011
    Location
    Auckland, NZ
    MS-Off Ver
    Excel 2013
    Posts
    165

    Re: To get rid of some contents from the path location string

    Hi Leith,

    thank you for your kind reply.

    my filespec will always be a dynamic string as its chosen by user.

    what if my filespec is C:\Users\ExcelForum\Desktop\Something.exe

    i want to get rid of 'something.exe'. this can be anything from something.exe to something.xls to something.txt

    thanks again, Leith.

    best regards
    plasma33

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: To get rid of some contents from the path location string

    Hello plasma33,

    The macro divides the file specification into two parts: The folder path and the file name. They are saved in the variables Folder and File respectively.

  5. #5
    Forum Contributor
    Join Date
    06-20-2011
    Location
    Auckland, NZ
    MS-Off Ver
    Excel 2013
    Posts
    165

    Re: To get rid of some contents from the path location string

    Hi Leith,

    Thank for your kind reply.

    Your code works fine for me now.

    I found a code on MSDN that does the trick too. Please find the code below:

    Sub Path_Location()
    
    Dim Filespec As String
    Dim Drive_Path As String
    Dim File_Path As String
    
    Filespec = "C:\Users\ExcelForum\Desktop\Something.exe"
    
            Drive_Path = GetAName(Filespec)
            
            File_Path = ShowAbsolutePath(Drive_Path & ".")
    
            ' Promt
            strPrompt = ".xls file location titled 'Test.xls' can be found in location " & File_Path & "\" & "Test.xls" & "." & vbCrLf & _
            "Please click 'OK' or 'Cancel' to exit this message box."
            
            ' Dialog's Title
            strTitle = "Test.xls Location"
            
            'Display MessageBox
            iRet = MsgBox(strPrompt, vbOK, strTitle)
            
            ' Check pressed button
            If iRet = vbOK Then
                Range("A9").Select
            Else
                ' do nothing
            End If
    
    End Sub
    
    Function GetAName(Filespec As String)
       Dim fso
       Set fso = CreateObject("Scripting.FileSystemObject")
       GetAName = fso.GetDriveName(Filespec)
    End Function
    
    Function ShowAbsolutePath(Filespec As String)
       Dim fso
       Set fso = CreateObject("Scripting.FileSystemObject")
       ShowAbsolutePath = fso.GetAbsolutePathName(Filespec)
    End Function
    thanks again, Leith.

    best regards
    plasma33

+ 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. Changing connection string path to a dynamic path
    By Shevi in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 11-11-2014, 05:16 AM
  2. List of Search file location path
    By mohan.r1980 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-29-2013, 06:01 PM
  3. using cell contents as a string to define a folder path
    By RDS in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-13-2012, 04:16 PM
  4. Changing path to a network location
    By talksalot81 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-24-2011, 12:10 PM
  5. Use String Variables in File Path and create path if not existing
    By JanBang in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-26-2007, 09:04 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