Excel VBA For Loop and Nested IF statements for Counter -
i have value in cell should match filename of document in directory.
sheet3 column a1 = c:\users\admin\desktop\folder1
sheet3 column a2 = test.xls
‘location of directory scurrentxldirectory = worksheets("sheet3").cells(1, 1).value set currentxlfso = createobject("scripting.filesystemobject") proceednow = true set currentxlfolder = currentxlfso.getfolder(scurrentxldirectory) set currentxlfiles = currentxlfolder.files ‘always 10 files in folder if currentxlfiles.count <> 10 msgbox "wrong directory or folder mismatch" proceednow = false else 'return 1 indentical filename dim namecount integer namecount = 0 each folderidx in currentxlfiles ‘compare file names specified cell value if folderidx.name = worksheets("sheet3").cells(1, 2).value namecount = namecount + 1 if namecount <> 1 msgbox "unable find file” proceednow = false end if end if next end if
for reason, if change test.xls test1.xls, still proceed = true
if nested if statement not preferable way this, please guide me in right direction.
if purpose of procedure verify if file exists or not exist, using dir()
function simpler.
if goal, try following code:
sub test() dim sdirectory string dim sfile string sdirectory = worksheets("sheet3").cells(1, 1).value sfile = worksheets("sheet3").cells(1, 2).value sfile = dir(sdirectory & "\" & sfile, vbdirectory) if len(sfile) = 0 msgbox "unable find file" end if end sub
Comments
Post a Comment