+ Reply to Thread
Results 1 to 13 of 13

Macro to delete rows based on multiple criteria.

  1. #1
    Registered User
    Join Date
    07-19-2023
    Location
    England
    MS-Off Ver
    365
    Posts
    5

    Macro to delete rows based on multiple criteria.

    Hi,

    I am new to VBA. I am trying to write a macro to delete rows in a specific worksheet (Files) based on a list of criteria. This is what I have so far.

    Sub CondenseGDB2()
    Please Login or Register  to view this content.
    Backstory is creating a macro or series of macros that will list all of the files in a folder and subfolder, but either not list all of the files in a file geodatabase (as I just need to know if the .gdb is there) or list them but then delete them. I have attempted the first option of listing all the files, but I can't find a way to only list the .gdb name and not all of the files under it. Here is the code I found online and modified slightly which does work to list files but I want to exclude it listing all the files under the .gdb.
    Please Login or Register  to view this content.
    Last edited by davesexcel; 07-19-2023 at 07:06 AM. Reason: Please add code tags to your VBA

  2. #2
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    1,980

    Re: Macro to delete rows based on multiple criteria.

    only list the .gdb name and not all of the files
    Does this work for you

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    07-19-2023
    Location
    England
    MS-Off Ver
    365
    Posts
    5

    Re: Macro to delete rows based on multiple criteria.

    Thank you, this ran but only produced the headers and no list within it.

  4. #4
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    1,980

    Re: Macro to delete rows based on multiple criteria.

    It's supposed to list files in the folder and subfolders with an extension of .gdb
    Maybe I misunderstood?

  5. #5
    Registered User
    Join Date
    07-19-2023
    Location
    England
    MS-Off Ver
    365
    Posts
    5

    Re: Macro to delete rows based on multiple criteria.

    Sorry no, I want it to list all files in folders and subflowers, but for the .gdb I only want it to list the .gdb name itself, not all the files under it (TIMESTAMP, ATX, SPX, GDBTABLX, etc)

    So in the folders and subfolders there would be a mixture of xlsx, shp, tif, and a gdb. I hope that makes sense!

  6. #6
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Macro to delete rows based on multiple criteria.


    Hi,

    just scan the folders with FSO and list the files with VBA function Dir …

  7. #7
    Registered User
    Join Date
    07-19-2023
    Location
    England
    MS-Off Ver
    365
    Posts
    5

    Re: Macro to delete rows based on multiple criteria.

    I think that's what my second example is doing? But for the geodatabase I dont want it to list all of the files, I only want it to list that theres a geodatabase there.

  8. #8
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Macro to delete rows based on multiple criteria.


    As there is no second example ! And if it does what I described so you did not ever need to create this thread, already solved …

  9. #9
    Registered User
    Join Date
    07-19-2023
    Location
    England
    MS-Off Ver
    365
    Posts
    5

    Re: Macro to delete rows based on multiple criteria.

    I have posted here. It doesn't do what I want yet, it does most of what I want. It lists all the files, but that means it also lists all the files under the geodatabase and I want it to exclude those. Just list the geodatabase name but not all the files under it.

    Sub List_File()

    Dim PathSpec As String
    PathSpec = "" 'Specify a folder
    If (PathSpec = "") Then PathSpec = SelectSingleFolder 'Browse for Folder to select a folder

    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject") 'Late Binding
    If (fso.FolderExists(PathSpec) = False) Then Exit Sub 'folder exist or not?

    Application.ScreenUpdating = False 'Disable Screen Updating to speed up macro

    Dim MySheetName As String
    MySheetName = "Files" 'Add a Sheet with name "Files"
    AddSheet (MySheetName)

    Dim FileType As String
    FileType = "*" '*:all, or pdf, PDF, XLSX...
    FileType = UCase(FileType)

    Dim queue As Collection, oFolder As Object, oSubfolder As Object, oFile As Object
    Dim LastBlankCell As Long, FileExtension As String

    Set queue = New Collection
    queue.Add fso.GetFolder(PathSpec) 'enqueue

    Do While queue.Count > 0
    Set oFolder = queue(1)
    queue.Remove 1 'dequeue

    For Each oSubfolder In oFolder.SubFolders 'loop all sub-folders
    queue.Add oSubfolder 'enqueue
    '...insert any folder processing code here...
    Next oSubfolder

    LastBlankCell = ThisWorkbook.Sheets(MySheetName).Cells(Rows.Count, 1).End(xlUp).Row + 1 'get the last blank cell of column A

    For Each oFile In oFolder.Files 'loop all files
    FileExtension = UCase(Split(oFile.Name, ".")(UBound(Split(oFile.Name, ".")))) 'get file extension, eg: TXT
    If (FileType = "*" Or FileExtension = FileType) Then
    With ThisWorkbook.Sheets(MySheetName)
    .Cells(LastBlankCell, 1) = oFile.Name 'File Name
    .Cells(LastBlankCell, 2) = FileExtension 'File Extension
    End With
    LastBlankCell = LastBlankCell + 1
    End If
    Next oFile
    Loop

    'Cells.EntireColumn.AutoFit 'Autofit columns width
    Application.ScreenUpdating = True

    End Sub

  10. #10
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Macro to delete rows based on multiple criteria.


    So same as post #6 : just use FSO to scan folders but do not use it to list files !
    As for a specific type of files the easy faster way is the Dir function as explained in VBA help …

    Anyway you can list files with FSO but you need to check each file extension according to the FileType
    like your initial post code does if you well read it and update at least the search FileType …

  11. #11
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Try this if it is only what your really want ...


    Quote Originally Posted by jeaves View Post
    […] list all of the files in a folder and subfolder
    […] as I just need to know if the .gdb is there then delete them
    Just update the root folder path of GetFolder method in this VBA demonstration :

    PHP Code: 
    Sub DeepScanDeleteGDB(oFold As Object)
             
    Dim Obj As Object
        
    For Each Obj In oFold.Files
            
    If LCase(Obj.NameLike "*.gdb" Then Obj.Delete True
        Next
            
    For Each Obj In oFold.SubFolders:  DeepScanDeleteGDB Obj:  Next
    End Sub

    Sub DemoSlow1
    ()
        
    DeepScanDeleteGDB CreateObject("Scripting.FileSystemObject").GetFolder("D:\Tests4Noobs")
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

  12. #12
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb Or this ...


    This VBA demonstration may be faster :

    PHP Code: 
    Dim oFSO As Object

    Sub DeepScanGDBDelete
    (oFold As Object)
        
    Dim P$, oSub As Object
            P 
    oFold.Path "\*.gdb"
            
    If Dir(P) > "" Then oFSO.DeleteFile PTrue
            
    For Each oSub In oFold.SubFolders:  DeepScanGDBDelete oSub:  Next
    End Sub

    Sub Demo2
    ()
        Const 
    "D:\Tests4Noobs"
        
    Set oFSO CreateObject("Scripting.FileSystemObject")
         If 
    oFSO.FolderExists(PThen DeepScanGDBDelete oFSO.GetFolder(P) Else Beep
        Set oFSO 
    Nothing
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

  13. #13
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Or maybe this ...


    If you need to just list the .gdb files within a worksheet rather than delete them
    then paste this VBA demonstration only to the worksheet module :

    PHP Code: 
    Sub DeepScanGDB(oFold As Object)
        
    Dim F$, S$(), R&, oSub As Object
            F 
    Dir$(oFold.Path "\*.gdb")
         If 
    "" Then
            ReDim S
    (1 To oFold.Files.Count0)
            Do:  
    1:  S(R0) = oFold.Path "\" & F:  F = Dir$:  Loop Until F = ""
            Cells(Rows.Count, 1).End(xlUp)(2).Resize(R) = S
         End If
            For Each oSub In oFold.SubFolders:  DeepScanGDB oSub:  Next
    End Sub

    Sub Demo3()
            Const P = "
    D:\Tests4Noobs"
            UsedRange.Clear
            Application.ScreenUpdating = False
        With CreateObject("
    Scripting.FileSystemObject")
            If .FolderExists(P) Then DeepScanGDB .GetFolder(P) Else Beep
        End With
            Application.ScreenUpdating = True
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VBA to delete rows based on multiple criteria
    By jamesmullens in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-28-2017, 06:33 AM
  2. [SOLVED] Macro to delete rows based on multiple criteria
    By Andrew E Smith in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-12-2014, 12:17 PM
  3. Delete Multiple Rows based on Criteria
    By athyeh in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 06-20-2013, 03:13 PM
  4. macro - delete rows based on multiple criteria.
    By ChocksterNo1 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-05-2013, 10:22 AM
  5. [SOLVED] Delete Rows based on Multiple Name Criteria
    By Graham Taylor in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-14-2012, 09:11 AM
  6. Delete rows based on multiple criteria
    By couger77 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-17-2007, 03:29 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1