Error Handling for Creating New Sheets Based on File Names

Hi All,

Below is code to create and name worksheets according to files in a folder:

Link: https://stackoverflow.com/questions/49509661/creating-worksheets-based-on-filenames

I needed some error handling code to help with the solution, hence this post - however, I got the solution at the link below before this question could be published..

Link: https://stackoverflow.com/questions/49514258/error-handling-for-creating-new-sheets-based-on-file-names/49514537#49514537

Apologies - I will provide the code solution once this post is published and mark as solved.
Eitel DagninIT Security AdministratorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Roy CoxGroup Finance ManagerCommented:
Not sure whether you want an answer, but here's a Function to check if a worksheet already exists. Place it in a Stabdard Module

Function WksExists(wksName As String) As Boolean
    On Error Resume Next
    WksExists = CBool(Len(Worksheets(wksName).Name) > 0)
   On Error GoTo 0
End Function

Open in new window


Use in code like this

   If WksExists(" whatever name here") Then
MsgBox "That name has been used"
Else
''/// code to add and name new sheet
End If

Open in new window

0
Eitel DagninIT Security AdministratorAuthor Commented:
Hi Roy,

Thank you for the reply. I tested your code and applied it to my work and it worked well!

Unfortunately I wasn't able to "cancel" my question before it was published and I managed to get the solution below:

Link: https://stackoverflow.com/questions/49514258/error-handling-for-creating-new-sheets-based-on-file-names/49514537#49514537

Option Explicit

Sub test()

Dim strFilename As String
Dim strPath As String
Dim WB As Workbook

Set WB = ThisWorkbook ' define which workbook you want to add the sheets
strPath = "D:\myPath\"
strFilename = Dir(strPath & "*.xlsx")

Do Until strFilename = ""
    strFilename = Split(strFilename, ".")(0)

    ' check is strFilename already exists in existing sheets

    If sheetExists(strFilename) = False Then ' doesn't exist
        WB.Sheets.Add(After:=WB.Sheets(WB.Sheets.Count)).Name = strFilename
    Else
        ' just raise a message box
        MsgBox "Worksheet " & strFilename & " already exists.", vbInformation
    End If
    strFilename = Dir
Loop

End Sub

Open in new window

0
Roy CoxGroup Finance ManagerCommented:
Presumably your code has a Function similar to mine. I wouldn't bother declaring a variable for ThisWorkBook, but handle it with With...End With statement.
Option Explicit

Sub test()

    Dim strFilename As String
    Dim strPath As String

    strPath = "D:\myPath\"
    strFilename = Dir(strPath & "*.xlsx")

    Do Until strFilename = ""
        strFilename = Split(strFilename, ".")(0)

        ' check is strFilename already exists in existing sheets
        With ThisWorkbook
            If sheetExists(strFilename) = False Then    ' doesn't exist
                .Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = strFilename
            Else
                ' just raise a message box
                MsgBox "Worksheet " & strFilename & " already exists.", vbInformation
            End If
            strFilename = Dir
        Loop
    End With
End Sub

Open in new window


I also cannot see where myPath is declared and given a value
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Eitel DagninIT Security AdministratorAuthor Commented:
Hi Roy,

I get the Error: Loop without do?
0
Roy CoxGroup Finance ManagerCommented:
Sorry, put end with in the wrong place

Option Explicit

Sub test()

    Dim strFilename As String
    Dim strPath As String

    strPath = "D:\myPath\"
    strFilename = Dir(strPath & "*.xlsx")

    Do Until strFilename = ""
        strFilename = Split(strFilename, ".")(0)

        ' check is strFilename already exists in existing sheets
        With ThisWorkbook
            If sheetExists(strFilename) = False Then    ' doesn't exist
                .Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = strFilename
            Else
                ' just raise a message box
                MsgBox "Worksheet " & strFilename & " already exists.", vbInformation
            End If
        End With
        strFilename = Dir
    Loop

End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Eitel DagninIT Security AdministratorAuthor Commented:
Hi Roy,

Thank you, this works perfectly! :)
0
Roy CoxGroup Finance ManagerCommented:
Pleased to help.

Actually, I've just looked at the source of your code and I think my Function is neater - it doesn't use a Loop which are best avoided because Loops generally slow the code operation. The Function that you found Loops through all the sheets in the workbook until a match is found.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Office

From novice to tech pro — start learning today.