excel - VBA check if file exists -
i have code. supposed check if file exists , open if does. work if file exists, , if doesn't, however, whenever leave textbox blank , click submit button, fails. want, if textbox blank display error message if file didn't exist.
runtime-error "1004"
dim file string file = textbox1.value dim dirfile string dirfile = "c:\documents , settings\administrator\desktop\" & file if dir(dirfile) = "" msgbox "file not exist" else workbooks.open filename:=dirfile end if
something this
best use workbook variable provide further control (if needed) of opened workbook
updated test file name actual workbook - makes initial check redundant, other message user textbox blank
dim strfile string dim wb workbook strfile = trim(textbox1.value) dim dirfile string if len(strfile) = 0 exit sub dirfile = "c:\documents , settings\administrator\desktop\" & strfile if len(dir(dirfile)) = 0 msgbox "file not exist" else on error resume next set wb = workbooks.open(dirfile) on error goto 0 if wb nothing msgbox dirfile & " invalid", vbcritical end if
Comments
Post a Comment