1

I've been googling this off and on over the last few months and have not been able to find a solution. It is completely possible that I just don't have the right terms to find the results needed.

I have a list of Schools that are being reviewed. There are different stages in this review process and I have a hard time keeping track. Our database in CRM is unable to track these items. So, I have created an excel file that I manually update(hardly the best solution, and though suggestions have been made after a year there has been no improvement).

  1. School names appear at the top
  2. Stages of review are to the left, Many of the stages involve creating/saving a document
  3. When a document is created I type "y" into the appropriate cell

What I would like to do is have the appropriate cell update with "y" when a file appears in a folder if the file's name has a portion of some identifier. Example:

  • if file (with the world "Anoka" in its name) appears in Report Cards folder, B12 = y
  • if file (with the word "Anoka" in its name) appears in Draft Created folder, B15 = y

I would repeat this for rows 16, 17, 18, 20, 25, 26, 27, and 28; accross all columns containing a program (the last column after programs has COUNTIFS to track total number of incompletes vs completes and other things). There are a total of 77 programs being tracked and human error leads to mistakes and takes up a lot of time when I have to go back to open the folders and search for the files and versions, etc.

R3uK
  • 14,417
  • 7
  • 43
  • 77
Kat
  • 11
  • 1
  • The Dir function should help you with that. **[Dir](https://msdn.microsoft.com/en-us/library/aa262726(v=vs.60).aspx)** with examples and an example from SO **[SO Dir example](http://stackoverflow.com/questions/20427832/use-dir-to-find-file-without-aaa/20428231#20428231)**. – simpLE MAn Mar 03 '15 at 16:54
  • @simpLEMAn Forgive my casual understanding, but I see that it pulls the list of files within the folderpath, and I'm guessing the next step would be to figure out what other VBA is needed to have the cell populate with "y" if a file matching the name is found? Or is it only possible to pull the list of files based on a filter and not possible for a cell to update if a file is found? – Kat Mar 03 '15 at 18:06
  • You guessed right :) I put this to point you in the right direction. You should create a dummy folder with some files in it and test the `Dir` function. Then update your question with the code you've tried and where it is not working so we can help. – simpLE MAn Mar 03 '15 at 18:30
  • @simpLEMAn Alright, what terms would I use to search further for "populate cell if" "matches dir file name"? While googling this I frequently come across finding the file name or updating the cell with the file name or the folder path name and I can't figure out what to relate that to when I want the if to match contents of a cell vs match file name and populate cell with file name. I find it very difficult to find results because when I say "casual understanding" its more, I get the big picture but not all the little bits and pieces until their relationship to each other is shown. – Kat Mar 03 '15 at 19:22

2 Answers2

0

Take a look at this for scanning your files in folder, it'll be a good start!

https://stackoverflow.com/questions/29184595/loop-on-all-files-in-the-same-directory-then-detect-extension-type/29187762#29187762

Here are some clues (I'm a bit in a hurry, I'll come back to detail if necessary)

You can can get the Path of your Workbook like that : ThisWorkbook.Path 'You get a string

You can then go to your specific folder by working on the string

Here is a code to check if the folder exist :

If Dir(Path, 16) <> ""

Look at Dir specs to have more info

And after that, I'll need to upload a VBA script (that I don't have on that computer) to have the full list of your docs in that folder and try to find "Akora" (or others) with Instr and it'll be a great procedure to reuse on other lines!

Community
  • 1
  • 1
R3uK
  • 14,417
  • 7
  • 43
  • 77
0

I think this will get you started:

To test this on your side, open a new workbook and arrange the data at same location as in the picture.
enter image description here

Then create a folder like this:
enter image description here

In each folder, create new txt files named using "AAA", "BBB" or "CCC": enter image description here

Then create a new code module:
enter image description here

And paste this code in it:

Sub Put_Y_At_The_Right_Place()

    Dim FolderPath As String: FolderPath = "D:\Testing_Dir\"
    Dim dirTmp As String

    Dim row As Integer
    Dim col As Integer

    For row = 2 To 4

        'Each time we call "Dir" it switches to the next file it finds so we have to
        'put its result in a temp variable like "dirTmp"
        dirTmp = Dir(FolderPath & ThisWorkbook.Worksheets(1).Cells(row, 1).Value2 & "\")

        Do While Len(dirTmp) > 0 'When "Len(dirTmp) = 0" no more files were found

            For col = 2 To 4 'passing through column B to D

                If InStr(dirTmp, ThisWorkbook.Worksheets(1).Cells(1, col).Value2) <> 0 Then
                    ThisWorkbook.Worksheets(1).Cells(row, col).Value2 = "Y"
                End If

            Next col

            dirTmp = Dir 'Calling "Dir" to switch to the next file

        Loop

    Next row

End Sub

After you run this code, you should get:
enter image description here

To see what each line of code does, execute the code line by line using F8.

When you understand this code, try to modify it to suit your needs. If you have trouble doing that, come here and post what you've done and what the problems are.

Hope this helps!

simpLE MAn
  • 1,582
  • 13
  • 22
  • 1
    Wow, this is amazing, I might not get to test this until next week, I'll get back to you if I have issues. This is awesome O_O – Kat Mar 04 '15 at 13:37
  • I'm not really sure where I'm going wrong with this or if there was something I was supposed to change but missed. I tried adding the XXX at the end of the folderpath to dir to the exact folder. Also, unsure what, if anything, would need to be changed in the actual application of this. For example "AAA" might actually be "AAA College Uni - PTA" in the excel sheet in column B. I can't really alter the names in row 1 as it is used when creating reports from this spreadsheet. – Kat Mar 10 '15 at 15:13
  • You just had to do **exactly** as it is written. If your XXX, YYY and ZZZ folders are in "D:\Testing_Dir\", there is nothing to change in my code to test it, just copy and paste in a code module. In my example, AAA, BBB and CCC are your file names (or "Program") and XXX, YYY and ZZ are your folders names (or "Decision"). Now I expect you to come back here with your own code to show how it worked out! – simpLE MAn Mar 11 '15 at 01:20
  • Okay, that is exactly what I did and it did not work. I created the excel file, created the text files, created the folder, pasted the code, changed the "D:\Testing_Dir\", saved the workbook as .xlsm, saved the code, then ran it and nothing happened. Received no errors or notifications after running it, and no data appeared in the cells. Would it make a difference if the file was on a network drive and not a folder? [this shows the code, result, and folder where it was saved.](http://i.imgur.com/sKBJ7jV.png) – Kat Mar 11 '15 at 19:10
  • Sorry for the trouble, I meant to say that I tried it on both a network drive and a local drive and neither made a difference. – Kat Mar 11 '15 at 19:17
  • @Kat; Your `FolderPath` **must** end with a "\" so that later on at the `dirTmp` line it assembles into the actual folder path we want. Let's take my code as an example. If `FolderPath = D:\Testing_Dir` then `dirTmp` would contain `D:\Testing_DirXXX\ ` at the first occurrence which is wrong but won't throw an error. We want `D:\Testing_Dir\XXX\ ` and so on. – simpLE MAn Mar 15 '15 at 01:36
  • I can't believe I missed that, I'm sorry. It worked, I'll update back here after I adjust it to the actual file. Thank you so much. – Kat Mar 16 '15 at 12:34
  • I did get this working, however when running it through my folders I ran into another issue. When I run the macro before all files are in the folder, and then run it again, a "y" is not entered into cells that were previously blank. Maybe this is more of what I need to play with to get it working for what I need though? Is that correct? – Kat Mar 18 '15 at 20:00
  • @Kat; I'm not sure I understand what you're trying to describe but, you could go through your code by running it line by line using F8 to see at any step the values stored whitin your variables (by hovering with mouse cursor over variables). Also, by default my `InStr` is case sensitive, if you want it not to be just change `InStr(dirTmp, ThisWorkbook.Worksheets(1).Cells(1, col).Value2)` by `InStr(1, dirTmp, ThisWorkbook.Worksheets(1).Cells(1, col).Value2, vbTextCompare)` – simpLE MAn Mar 23 '15 at 14:24