0

I have this code that gets all file types. Then if it has the extensions in the array, it should be stored in the excludedFile array and will be displayed after execution.

Dim excludedFile() as String
Const exts = _
  ".ade.adp.app.asp.bas.bat.cer.chm.cmd.com.cpl.crt.csh.der.exe.fxp.gadget" & _
  ".hlp.hta.inf.ins.isp.its.js.jse.ksh.lnk.mad.maf.mag.mam.maq.mar.mas.mat"

Dim file As Variant
file = Application.GetOpenFilename("All Files, *.*", , "Select File", , True)

ReDim data(1 To UBound(file) + 1, 1 To 1)

' filter the list
For i = LBound(file) To UBound(file)
  ext = LCase(Mid(file(i), InStrRev(file(i), ".")))
  If InStr(1, exts, ext & ".") = 0 Then  ' if not blacklisted
    count = count + 1
    data(count, 1) = file(i)
    Else 'I've tried this but returns Subscript out of range error
    excludedFile(UBound(excludedFile)) = file(i)
    ReDim Preserve excludedFile(1 To UBound(excludedFile) + 1) As String
    found = true
  End If
Next
if found then
     MsgBox Join(excludedFile, vbCrLf)
end if

Any help is appreciated. Thanks.

5
  • can you post the rest of your Sub code ? it's missing the excludedFile declaration array. you also have a type error in If InStr(1, exts, ext & ".") = 0 Then should be (1, ext, ext & ".") no? Commented Aug 12, 2016 at 10:38
  • @ShaiRado Updated my question. And its not a typo :) Commented Aug 12, 2016 at 10:44
  • I must be missing something, you are opening a single file, so what array ? Do you have another loop above ? to get all files in a folder don;t you need to use the GetFolder command? Commented Aug 12, 2016 at 10:50
  • @ShaiRado This is the line where I select files in a directory: file = Application.GetOpenFilename("All Files, *.*", , "Select File", , True). Thanks Commented Aug 12, 2016 at 10:53
  • never mind, I saw you have your answer Commented Aug 12, 2016 at 10:54

1 Answer 1

1

Not the most elegant way, but working

Const exts = _
  ".ade.adp.app.asp.bas.bat.cer.chm.cmd.com.cpl.crt.csh.der.exe.fxp.gadget" & _
  ".hlp.hta.inf.ins.isp.its.js.jse.ksh.lnk.mad.maf.mag.mam.maq.mar.mas.mat"


Dim file As Variant
file = Application.GetOpenFilename("All Files, *.*", , "Select File", , True)

'Dim your Array and a Counter
Dim excludedFile() As String
Dim efCount As Integer

ReDim Data(1 To UBound(file) + 1, 1 To 1)

efCount = 0
' filter the list
For i = LBound(file) To UBound(file)
  ext = LCase(Mid(file(i), InStrRev(file(i), ".")))
  If InStr(1, exts, ext & ".") = 0 Then  ' if not blacklisted
    Count = Count + 1
    Data(Count, 1) = file(i)
    Else 'I've tried this but returns Subscript out of range error

    'Use counter to access array
    ReDim Preserve excludedFile(efCount)
    excludedFile(efCount) = file(i)
    efCount = efCount + 1

    found = True
  End If
Next

If found Then
     MsgBox Join(excludedFile, vbCrLf)
End If
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.