删除数组中的项目 [英] Delete an item in an array

查看:90
本文介绍了删除数组中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码可以浏览VBA中的所有文件类型。它已经在工作,但是我现在要做的是删除数组中被阻止的文件类型之一的项目。

I have this code that browse all file types in VBA. It's already working but my what I want to do now is to delete the item in the array if it is one of the blocked file types.

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" & _
  ".mau.mav.maw.mda.mdb.mde.mdt.mdw.mdz.msc.msh.msh1.msh2.mshxml.msh1xml" & _
  ".msh2xml.ade.adp.app.asp.bas.bat.cer.chm.cmd.com.cpl.crt.csh.der.exe.fxp" & _
  ".gadget.hlp.hta.msi.msp.mst.ops.pcd.pif.plg.prf.prg.pst.reg.scf.scr.sct" & _
  ".shb.shs.ps1.ps1xml.ps2.ps2xml.psc1.psc2.tmp.url.vb.vbe.vbs.vsmacros.vsw" & _
  ".ws.wsc.wsf.wsh.xnk."

file = Application.GetOpenFilename(MultiSelect:=True, Title:="Select the files you want to zip")
If IsArray(file) = True Then
    'Create empty Zip File
ReDim Data(1 To UBound(file) + 1, 1 To 1)
efCount = Empty

' filter the list
For j = LBound(file) To UBound(file)
  ext = LCase(Mid(file(j), InStrRev(file(j), ".")))
    If InStr(1, exts, ext & ".") = 0 Then  ' if not blacklisted
        count = count + 1
        Data(count, 1) = file(j)
    Else
        ReDim Preserve excludedFile(efCount)
        excludedFile(efCount) = Dir(file(j))
        efCount = efCount + 1
        file(j - 1) = file(j) 'Ive tried this and other ways bu is not working
        found = True
    End If
Next

感谢

推荐答案

您可以像这样

    Dim file As Variant
    Dim efCount As Long, j As Long, count As Long
    Dim ext As String
    Dim found As Boolean

    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" & _
      ".mau.mav.maw.mda.mdb.mde.mdt.mdw.mdz.msc.msh.msh1.msh2.mshxml.msh1xml" & _
      ".msh2xml.ade.adp.app.asp.bas.bat.cer.chm.cmd.com.cpl.crt.csh.der.exe.fxp" & _
      ".gadget.hlp.hta.msi.msp.mst.ops.pcd.pif.plg.prf.prg.pst.reg.scf.scr.sct" & _
      ".shb.shs.ps1.ps1xml.ps2.ps2xml.psc1.psc2.tmp.url.vb.vbe.vbs.vsmacros.vsw" & _
      ".ws.wsc.wsf.wsh.xnk."

    file = Application.GetOpenFilename(MultiSelect:=True, Title:="Select the files you want to zip")
    If IsArray(file) = True Then
            'Create empty Zip File
        ReDim Data(1 To UBound(file))
        ReDim excludedFile(1 To UBound(file))

        efCount = 0
        ' filter the list
        For j = LBound(file) To UBound(file)
          ext = LCase(Mid(file(j), InStrRev(file(j), ".")))
            If InStr(1, exts, ext & ".") = 0 Then  ' if not blacklisted
                count = count + 1
                Data(count) = file(j)
            Else
                excludedFile(efCount + 1) = Dir(file(j))
                efCount = efCount + 1
            End If
        Next
        found = efCount > 0
    End If
    ReDim Preserve Data(1 To count)
    ReDim Preserve excludedFile(1 To efCount)

    file = Data

这篇关于删除数组中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆