sheet name = dir path

wwbwb

Well-known Member
Joined
Oct 20, 2003
Messages
513
Good morning all

the following code lists all files in the directory listed in A1. It works just fine. What I would like to do now is be able to name the automatically name the sheet with the current folder. ie. The sheet name would be Test if the path listed in A1 were c:\temp1\temp2\Test\

I've tried a couple of different solutions, but am shot down each time. Ideas?

Code:
Sub ListFiles()
    Application.ScreenUpdating = False
    Range("a5:b65536").ClearContents
    Range("a5").Select
    Dim fileList() As String
    Dim fName As String
    Dim fPath As String
    Dim I As Integer
    Dim myfind As Integer, mylen As Integer, mynum As Integer
    fPath = Range("A1")
    fName = Dir(fPath & "*.*")
    While fName <> ""
        I = I + 1
        ReDim Preserve fileList(1 To I)
        fileList(I) = fName
        fName = Dir()
    Wend
    If I = 0 Then
        MsgBox "No files found"
        Exit Sub
    End If
     For I = 1 To UBound(fileList)
     myfind = WorksheetFunction.Find(".", fileList(I), 1) - 1
     mylen = Len(fileList(I))
     mynum = mylen - myfind
     If Range("b1") = "x" Then
        Range("A" & I + 4).Value = "=hyperlink(""" & fPath & fileList(I) & """)"
        Range("A1").Select
    Else
        Range("A" & I + 4).Value = Left(fileList(I), myfind)
        Range("B" & I + 4).Value = Right(fileList(I), mynum)
        Range("A:A").Select
        Selection.Font.ColorIndex = 1
        Selection.Font.Underline = xlUnderlineStyleNone
        Range("A1").Select
    End If
     Next
ActiveSheet.Cells.EntireColumn.AutoFit

Application.ScreenUpdating = True
    End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
wwbwb.

The following code takes the name of the file in Cell(1,1) and applies it to the containing sheet. Be sure to run this from the sheet containing the file names.

Hope this helps. Ben.

Code:
Sub RenameSheet()

Dim NewName As String, FileName As String, OldName As String

With ActiveSheet
    OldName = .Name 'Get the sheet name which will be changed
    FileName = .Cells(1, 1).Value 'Get the file name from which to generate a new sheet name
End With

NewName = Left(FileName, InStrRev(FileName, "\") - 1) 'Drops the ending "\" from the file name.  The filename is now  "X:\XXX\XXX\...\Name"
NewName = Mid(NewName, Len(Left(NewName, InStrRev(NewName, "\"))) + 1) 'Takes all the characters following the last "\" 

Worksheets(OldName).Name = NewName 'Renames sheet

End Sub
 
Upvote 0

Forum statistics

Threads
1,213,497
Messages
6,113,998
Members
448,539
Latest member
alex78

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top