Solved

How do I get the names of each Excel workbook located in a folder with a macro?

Posted on 2016-11-16
6
56 Views
Last Modified: 2016-11-17
Hi,

I have a folder in path G:\Share. In the folder a have a lot of excel workbooks. Is there code that can be run from Excel to capture the name of each workbook without the extension .xlsm? Below is a sample of what is in my folder and what I want.

Folder Contents                               What I Want on Excel

ABC Construction.xlsm                  ABC Construction
River Works.xlsm                            River Works

Thank you.

Conernesto
0
Comment
Question by:Conernesto
6 Comments
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 250 total points
ID: 41890564
Try this...

Sub GetWorkbookNames()

    Const FolderPath As String = "G:\Share\"
    Dim filename As String
    filename = Dir(FolderPath)
    Do
        If InStr(filename, ".xls") > 0 And Right(filename, 4) <> "xlsm" Then
            Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) = Split(filename, ".")(0)
        End If
        filename = Dir()
    Loop While filename <> ""
    
End Sub

Open in new window

0
 
LVL 28

Assisted Solution

by:Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj) earned 250 total points
ID: 41890859
If I understand your requirement correctly, here is the little correction to the Wayne's code...
Sub GetWorkbookNames()

    Const FolderPath As String = "H:\Excel\"
    Dim filename As String
    filename = Dir(FolderPath)
    Do
        If InStr(filename, ".xl") Then
            Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) = Split(filename, ".")(0)
        End If
        filename = Dir()
    Loop While filename <> ""
    
End Sub

Open in new window

As per the line#7 in the code, the code will get the names of all the excel workbooks. But if you want to narrow your search only to .xlsm files, change that line to If InStr(filename, ".xlsm") Then
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 41891256
Neeraj, they need all Excel files except *.xlsm.

capture the name of each workbook without the extension .xlsm
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41891325
That's interesting. If you read the example provided in the description, it seems that the requirement is to get the file names without the extension.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 41891365
You should not use SPLIT() with the ".", since "." is a legal character in the file name.  Instead, use InStrRev() to find the last ".".  Alternatively, you can instantiate a filesystemobject and use it to give you the basename property, which is the file name without the file extension.
0
 

Author Closing Comment

by:Conernesto
ID: 41891740
Thank you very much.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now