2

First of all, excuse me if the code below does not contain an array but a list. I am new to VBA so I don't know all the basics yet. The code below retrieves an array (or a list) of all Excel files in a certain folder. The code is used to retrieve data from each file and is pasted in a master file.
Okay, here is my MWE:

    Sub ReadDataFromAllWorkbooksInFolder()
    Dim FolderName As String, wbName As String, wbCount As Integer
    FolderName = "C:\Users\Robin\Desktop\Test"
        wbCount = 0
        wbName = Dir(FolderName & "\" & "*.xlsm")
        While wbName <> ""
            wbCount = wbCount + 1
            ReDim Preserve wbList(1 To wbCount)
            wbList(wbCount) = wbName
            wbName = Dir
        Wend
        If wbCount = 0 Then Exit Sub
    End Sub

Now here is the problem: The array contains a template file, and the name of the file contains the word TEMPLATE. How do I filter out (or exclude) the file which file name contains the string "TEMPLATE"?

Any help would be much appreciated! Thanks in advance!

3 Answers 3

2

The variable wbName contains the filenames from the Dir command. Check to see if the string contains what you want to exlude.

Extend the line

While wbName <> ""

into

While wbName <> "" And InStr(1, UCase(wbName), "TEMPLATE") = 0

to exlude any filenames that contain TEMPLATE, regardless of case.

EDIT

The logic in my suggestion above was wrong, it stopped when it encountered the first filename containing "TEMPLATE". Try the following instead:

    While wbName <> ""
        '***** Only add to wbCount if wbName does not contain TEMPLATE
        If InStr(1, UCase(wbName), "TEMPLATE") = 0 Then
            wbCount = wbCount + 1
            ReDim Preserve wbList(1 To wbCount)
            wbList(wbCount) = wbName
        End If
        wbName = Dir
    Wend

EDIT 2

Some general tips on finding errors:

  • Always use Option Explicit at the top of your modules. It will force you to declare your variables, minimizing the risk of typos etc introducing bugs.
  • Step through your code using F8 to identify errors and checking vaules of variables and functions
  • If possible, remove code to simplify it, only keeping the logic like While loops and If statements to make sure the basics work. Then add the details back.

Finally, the key to my solution is finding the files that contain "TEMPLATE" in the name. The statement InStr(1, UCase(wbName), "TEMPLATE") = 0 will be False for all files containing the string "TEMPLATE" anywhere, regardless of case, and True otherwise. (The Ucase part is what makes the statement case insensitive.)

Try to add it to your logic.

Sign up to request clarification or add additional context in comments.

11 Comments

Thanks for your reply. When I extend the line, the macro does not seem to do anything anymore. I checked the result of InStr(1, UCase(wbName), "TEMPLATE") = 0 with a messagebox, but it didn't show anything.
Ah, it does seem to work when it is used in a messagebox. If used in the while statement, it does not work.
@RobinTrietsch Take the name of file in a separate variable x=split(wbName,"."). Then run the loop to UBound of "x" and check for UCase(x)= "TEMPLATE" . I think this should work.
Then you will have to step through the code to see where it hangs. I have run your code, but with the lines containing referecnes to wbList commented out, since I could not get them to compile. Your code with my changes works in my environment (Excel 2010).
Somehow, it didn't work the first time. Now it works! Thanks a bunch!
|
2
Dim fNameList As Variant
Dim storFName As String
Dim i As Integer
Sub ReadDataFromAllWorkbooksInFolder()
Dim FolderName As String, wbName As String, wbCount As Integer
FolderName = "C:\Users\Robin\Desktop\Test"
    wbCount = 0
    wbName = Dir(FolderName & "\" & "*.xlsm")
    While wbName <> ""
        wbCount = wbCount + 1
        ReDim Preserve wbList(1 To wbCount)
        fNameList = Split(wbName, ".")
        For i = 0 To UBound(fNameList) '<- Here i is being use
            If (UCase(InStr(fNameList(i), "TEMPLATE"))) Then
               UCase(storFName) = fNameList (i)
            End If
        Next i '<- Here i is being increase
        If storFName <> "TEMPLATE" Then
            wbList(wbCount) = wbName
            wbName = Dir
        End If
    Wend
    If wbCount = 0 Then Exit Sub
End Sub

Will it work for you?

13 Comments

I don't think you need to remove the file ending, since the question says to exlude files where the name contains the string "TEMPLATE". The InStr function will take care of all those files.
Okay, That's what I want to know, will file name may be like this e.g. (1) Template XYz.xlsx , (2) Template_Xyz.xlsx?
I think so, but OP would have to say.
@Brain The template file name looks like: "SOMETEXT V2_1 TEMPLATE.xlsm"
@Brain Unfortunately, the macro still uses the TEMPLATE file with your solution.
|
0
Sub test()
    Dim x() As String, i As Integer
    x = ReadDataFromAllWorkbooksInFolder("C:\Users\Robin\Desktop\Test")
    For i = 0 To UBound(x)
        Debug.Print x(i)
    Next i
End Sub

Private Function ReadDataFromAllWorkbooksInFolder(FolderName As String) As Variant
    Dim wbName As String
    Dim wbCount As Integer
    Dim wbList() As String
    wbCount = 0
    ReDim wbList(0)
    wbName = Dir(FolderName & "\*.xlsm")
    While wbName <> vbNullString
        If Not (UCase(wbName) Like "*TEMPLATE*") Then
            ReDim Preserve wbList(wbCount)
            wbList(wbCount) = wbName
            wbCount = wbCount + 1
        End If
        wbName = Dir
    Wend
    ReadDataFromAllWorkbooksInFolder = wbList
End Function

Once you get comfortable with VBA you'll probably switch to Scripting.FileSystemObject and Scripting.Dictionary...

2 Comments

Thanks Apropos. I implemented the code, but like with the solution from Olle Sjögren, the macro does not do anything anymore.
Thanks for your effort Apropos. Eventually, Olle's solution worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.