• Finding the last in a string (97)

    Author
    Topic
    #390115

    Sorry if this is a repeated common question, lack of Search etc.

    I have a file path set as a string for example:

    c:Documents and settingsdesktopjezzareport.csv

    I am just trying to get the file name report.csv. What is the code to hunt through the original string to find the last so that I can just get the value report.csv. I seem to remeber it uses the right, left and len functions and have seen it in the forum before.

    Viewing 0 reply threads
    Author
    Replies
    • #691728

      If the string is in A1 this will give the file name:

      =MID(A1,FIND(CHAR(1),SUBSTITUTE(A1,"",CHAR(1),LEN(A1)-LEN(SUBSTITUTE(A1,"",""))))+1,LEN(A1))

      Steve

      • #691744

        Hi Steve

        Thanks for your speedy reply. I have this running in VBA

        ……

        FileToOpen = Application.GetOpenFilename(“CSV Files (*.csv), *.csv”, , ” Source File”) ‘ This finds the file to be opened

        Workbooks.Open FileToOpen

        ………

        Obviously FileToOpen is the file path

        The VBA doesn’t like the substitute function… am I doing something wrong?

        Jerry

        • #691750

          Dim FileToOpen As String
          Dim FileName As String
          Dim i As Integer

          FileToOpen = Application.GetOpenFileName(…)
          For i = Len(FileToOpen) To 1 Step -1
          If Mid(FileToOpen, i, 1) = “” Then
          Exit For
          End If
          Next i

          If i = 0 Then
          MsgBox “No found in path!”
          Else
          MsgBox Mid(FileToOpen, i+1)
          End If

          • #691940

            Hi Hans

            I had one of the 11.30pm inspirations last night and went tapping at the laptop. I got a solution not as elegant as yours but I thought I would share it with the forum, just to show I am trying:

            ……..

            While Left(FileName,1) “”

            x=x+1

            FileName=Right(FileToOpen,x)

            Wend

            FileName = Right (FileToOpen, x-1)
            FileLength = Len (FileName)
            NewFileName = Left (FileName, FileLength-4)

            …… etc

            I think I was nearly there, it just seems you have used Step/Next

            • #691944

              Hi Jerry,

              There is no fundamental reason to prefer a For / Next loop over a While loop (or vice versa). Both will work fine.

            • #692001

              InStrRev() finds a string within another one starting from the end, so you could use:

              iPos = InStrRev(FileToOpen, "")
              strPathName =  Left(FileToOpen, iPos - 1)
              strFileName = Right(FileToOpen, Len(FileToOpen) - iPos)
              

              Have fun!
              Ian.

            • #692002

              I can’t seem to find this function in the VB I have. InStr and InStrB but no InStrRev

              Steve

            • #692009

              Sorry sorry – I use Excel 2000 now, but I thought I remembered InStrRev being in earlier versions.

              Memory going with advancing age, I’m afraid!

              Ian.

            • #692004

              I believe that InStrRev was introduced with Office 2K

              Peter

        • #691760

          Darn it, Hans beat me to it, but I’ll offer one useless comment: in my pathetic code, I usually keep the path name and file name as separate strings; it makes them easier to handle, and you can easily concatenate them.

        • #691764

          You got your original question answered. VB is more clear than the megaformula.

          Once you OPEN the file, you can get the path and name directly using:
          sPath = activeworkbook.path
          sFilename = activeworkbook.name

          FYI,
          To use substitute (and other “non-VB” functions preface them with:
          “Application.worksheetfunction.” eg:

          Application.worksheetfunction.SUBSTITUTE(text,old_text,new_text,instance_num)

          Steve

        • #691779

          Slightly easier and more efficient than Hans’ code:

          Dim vPath As Variant
          Dim strFName As String
              vPath = Application.GetOpenFilename
              If Not vPath = False Then
                  strFName = Dir(vPath)
                  MsgBox "File name is " & strFName
              End If
          
    Viewing 0 reply threads
    Reply To: Reply #692009 in Finding the last in a string (97)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information:




    Cancel