Help with if...then

brownnr

New Member
Joined
Nov 25, 2013
Messages
18
I have code that will find and list subfolders, I need help writing an if...then...else statement that basically says if the subfolder is already listed, don't add it to list, if not, then add at end of list. I've tried all I could, but I don't think I'm putting it in the right place in the code, or something. Here is my code:

Code:
'Lists Folders '
    Worksheets(1).Activate
    Dim row As Integer
    Dim SearchFolders As Variant
    row = 3
    LookInTheFolder = "I:\projects2\Koch\58MY8900\PROCESS\2013 Work (Phase 3)\DIH_Sulfolane\Equipment"
    Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
    For Each SearchFolders In FileSystemObject.GetFolder(LookInTheFolder).SubFolders
    Cells(row, 1) = Mid(SearchFolders, InStrRev(SearchFolders, "\") + 1)
    row = row + 1
    Next SearchFolders

Thanks!
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
I have code that will find and list subfolders, I need help writing an if...then...else statement that basically says if the subfolder is already listed, don't add it to list, if not, then add at end of list. I've tried all I could, but I don't think I'm putting it in the right place in the code, or something. Here is my code:

Code:
'Lists Folders '
    Worksheets(1).Activate
    Dim row As Integer
    Dim SearchFolders As Variant
    row = 3
    LookInTheFolder = "I:\projects2\Koch\58MY8900\PROCESS\2013 Work (Phase 3)\DIH_Sulfolane\Equipment"
    Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
    For Each SearchFolders In FileSystemObject.GetFolder(LookInTheFolder).SubFolders
    Cells(row, 1) = Mid(SearchFolders, InStrRev(SearchFolders, "\") + 1)
    row = row + 1
    Next SearchFolders

Thanks!

you can use this
Code:
Public Function FileFolderExists(strFullPath As String) As Boolean
'Author       : Ken Puls (www.excelguru.ca)
'Macro Purpose: Check if a file or folder exists
    On Error GoTo EarlyExit
    If Not Dir(strFullPath, vbDirectory) = vbNullString Then FileFolderExists = True
    
EarlyExit:
    On Error GoTo 0
End Function
 
Upvote 0
so the code would be
Code:
File = filefolderexists("../Desktop/file.txt")
if File then 
     msgbox "True, File does exist"
elseif not File then 
     msgbox "False, File Does not exist"
end if
 
Upvote 0
Dude if you need any if, else, then, etc. then search yourprogrammingnetwork on youtube and he has great tutorials. :)
 
Upvote 0
Solosideshow, thanks, but it looks like that will either exit the code, or give me a msgbox? I want it to either not add the subfolder name to the list if it alreadys exists, or add it at the end if it does.

Epuron, thanks for the tip, but I'm at work, they don't allow streaming or social networks.
 
Upvote 0
Solosideshow, thanks, but it looks like that will either exit the code, or give me a msgbox? I want it to either not add the subfolder name to the list if it alreadys exists, or add it at the end if it does.

Epuron, thanks for the tip, but I'm at work, they don't allow streaming or social networks.
i gave that code as an example of how it works..

Code:
if File then
     ' do thing the file is there
elseif not File then 
     'code here if the file does not exist and needed to be created
end if
 
Upvote 0
Yeah, I understand the basic form of the if...then statement, I'm having trouble putting it in use correctly inside my code, and I can't figure out if I'm putting it in the wrong place(I've tried several), or what I'm doing wrong. Thanks for the help, but I know the basics of the statements, I need something specific to my situation.
 
Upvote 0
ok i'll se if i can help.
1) whats the folder or file location and name you are trying to fine
2) whats the command you need to happen if the File IS there
3) Whats the command you need to happen if the File is NOT there
 
Last edited:
Upvote 0
That's what I need help with, sorry I'm very new at VBA so I probably wasn't explaining as well as I should have. I posted the code in the original post, I don't know what commands are used to have it do what I need. I'll try to explain as best I can...

This finds and lists the subfolders with no problem:

Code:
'Lists Folders ' 
   Worksheets(1).Activate 
   Dim row As Integer 
   Dim SearchFolders As Variant 
   row = 3 
   LookInTheFolder = "I:\projects2\Koch\58MY8900\PROCESS\2013 Work (Phase 3)\DIH_Sulfolane\Equipment" 
   Set FileSystemObject = CreateObject("Scripting.FileSystemObject") 
   For Each SearchFolders In FileSystemObject.GetFolder(LookInTheFolder).SubFolders 
   Cells(row, 1) = Mid(SearchFolders, InStrRev(SearchFolders, "\") + 1) 
   row = row + 1 
   Next SearchFolders

I need help with the what and where. What do I tell it to get it to:
1. find if the subfolder name already exists in the list
2.if it does, do not input subfolder name
3. if it does not, move down to first blank and input subfolder name.

I kept running into errors when I tried all I could find, or knew(which is little). And it would be great if I could get it to also strikethrough the text if the subfolder name is in the list, but no longer found in the folder. Thanks!
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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