?
Solved

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

Posted on 2016-11-16
6
Medium Priority
?
81 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 1000 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 32

Assisted Solution

by:Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj) earned 1000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

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 46

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

770 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