PDA

View Full Version : If statement dependent on start of name



Homegrown
10-19-2015, 06:31 AM
I was wondering if vba code can see a file name and then respond if it begins with certain characters. I have a few different categories of documents. and every document that falls within a category will be treated the same.

so every Testplan will be printed in A3 for example.

so providing my file is named "testplanxxxxx" can some code read the first few letters and then perform an action if the letters match?

Cheers, Andy.

HiTechCoach
10-19-2015, 06:59 PM
Sure. Use the Left() function.


If Left(MyFileName,8) = "testplan" Then

' Do Something here

End if

You can also use LIKE and Select Case statement for testing multiple file names



Select Case MyFileName

Case Like "testplan*"

' do something here

Case Like "otherplan*"

' do something here


End Select

Homegrown
10-20-2015, 04:02 AM
So, what are the differences?

Left can be used for multiples too right?

also.. yours says like... which sounds like a better option providing I understand.

will that look for 'testplan' in the entire name? because often the name testplan could be at the end. #

thanks a lot for your help so far.

HiTechCoach
10-20-2015, 03:00 PM
So, what are the differences?

Left can be used for multiples too right?


Correct. You can use multiple If .. Then



also.. yours says like... which sounds like a better option providing I understand.

will that look for 'testplan' in the entire name? because often the name testplan could be at the end. #

thanks a lot for your help so far.

The VBA Like is very similar to the SQL Like int he were clause. You can use wildcards the same way. I used the "*" in my example.

You can also use the VBA Instr() function to see if a string includes something and also get the position where it occurs.



If InStr(MyFileName, "testplan") > 0 then
' Do Something here
End If

Homegrown
10-21-2015, 12:49 AM
awesome help.