Solved

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

Posted on 2016-11-16
6
74 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 31

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 31

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

739 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