VBA - Searching for EXCEL files in various folders help

epactheactor

New Member
Joined
Sep 9, 2015
Messages
38
Hello!

First, thank you for reading. I've used VBA tons of times in the past to make my life easier, but now I'm actually studying Programming to get a degree in it so I know what it is that I'm doing instead of recording macros or modifying code. The issue is that at this point in my studies, I familiar with Python, so I am having trouble with the VBA language.

I was trying to create a Macro in which the user would input a string, then the macro would search 3 separate folders (A, B & C) and their subfolders for file names that would contain the string (be partial file name). The macro would then open said files.

All my attempts have failed. I've looked around the past few days, but haven't had any luck.

Here is the VBA code for the first folder (Folder A) that I've most recently tried (and failed). And because I'm been programmed to do so (ha, ha) I put the psuedocode I wrote for my macro underneath. Please forgive any rookie mistakes.

Thank you for any help.


Code:
 Sub allFolders()[/COLOR]

[COLOR=#333333]Dim tlcNumber As String[/COLOR]
[COLOR=#333333]Dim folderA As String[/COLOR]
[COLOR=#333333]Dim fileA As String[/COLOR]

[COLOR=#333333]tlcNumber = InputBox("Enter TLC in either X-XX, XX-XX or XX-XXX format()")[/COLOR]
[COLOR=#333333]folderA = "\\file path"[/COLOR]
[COLOR=#333333]fileA = Dir(folderA & "\*.xlsm")[/COLOR]
[COLOR=#333333]Do Until fileA = ""[/COLOR]
[COLOR=#333333]Workbook.Open Filename:=tlcNumber & "" & fileA[/COLOR]
[COLOR=#333333]fileA = Dir[/COLOR]
[COLOR=#333333]Loop[/COLOR]

[COLOR=#333333]End Sub [code][/COLOR]





[COLOR=#333333]All Folders Psuedocode
[/COLOR][Code]
[COLOR=#333333]//Declare variables[/COLOR]
[COLOR=#333333]Declare String tlcNumber[/COLOR]

[COLOR=#333333]//global constants as the location will never change[/COLOR]
[COLOR=#333333]Set folderA = network location[/COLOR]
[COLOR=#333333]Set folderB = network location[/COLOR]
[COLOR=#333333]Set folderC = network location[/COLOR]

[COLOR=#333333]// Call Modules[/COLOR]
[COLOR=#333333]Call Main()[/COLOR]
[COLOR=#333333]Call findTLC(tlcNumber)[/COLOR]
[COLOR=#333333]Call openFiles(tlcNumber)[/COLOR]


[COLOR=#333333]Module findTLC (String tlcNumber)[/COLOR]
[COLOR=#333333]//To input the TLC for the line you are looking for[/COLOR]
[COLOR=#333333]Display “Enter the TLC to identify the line in XX-XX, X-XXX or XX-XXX format.”[/COLOR]
[COLOR=#333333]Input tlcNumber[/COLOR]

[COLOR=#333333]Module openFiles(tlcNumber)[/COLOR]
[COLOR=#333333]// Search the TLC number in the A folder and subfolders then open file[/COLOR]
[COLOR=#333333]Find(tlcNumber folderA)[/COLOR]
[COLOR=#333333]For files in folderA[/COLOR]
[COLOR=#333333]If tlcNumber name in file Then[/COLOR]
[COLOR=#333333]Open(file)[/COLOR]
[COLOR=#333333]Else [/COLOR]
[COLOR=#333333]Display “No file found”[/COLOR]

[COLOR=#333333]// Search the TLC number in the B folder and subfolders then open file[/COLOR]
[COLOR=#333333]Find(tlcNumber, folderB)[/COLOR]
[COLOR=#333333]For files in folderB[/COLOR]
[COLOR=#333333]If tlcNumber name in file Then[/COLOR]
[COLOR=#333333]Open(file)[/COLOR]
[COLOR=#333333]Else [/COLOR]
[COLOR=#333333]Display “No file found”[/COLOR]

[COLOR=#333333]// Search the TLC number in the C folder and subfolders then open file[/COLOR]
[COLOR=#333333]Find(tlcNumber, folderC)[/COLOR]
[COLOR=#333333]For files in folderC[/COLOR]
[COLOR=#333333]If tlcNumber name in file Then[/COLOR]
[COLOR=#333333]Open(file)[/COLOR]
[COLOR=#333333]Else [/COLOR]
[COLOR=#333333]Display “No file found”[/COLOR]
[COLOR=#333333]Call Main():

[Code][/COLOR]
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

Forum statistics

Threads
1,215,960
Messages
6,127,942
Members
449,411
Latest member
sdescharme

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top