+ Reply to Thread
Results 1 to 7 of 7

Use other workbook to act as a 'stop-list'

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-23-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    404

    Use other workbook to act as a 'stop-list'

    Hi all,

    Sorry - not a very good title.

    I have a macro where I have hardcoded which rows are deleted based on the below code. However this list may need to be updated quite frequently. Is there anyway - so that I can have others update it themselves – I can have a link to an external worksheet, where say, whatever is in column A of that worksheet would act as a stoplist.

    So for example if on this 'stop-list' I have the words 'please', 'delete' and 'me' in cells A1, A2 and A3 then these rows would be deleted. I think I would need up to about 50 words (i.e. A1:A50).

    Is this workable?

    On Error Resume Next
    With Range("A1:A" & ActiveSheet.UsedRange.Rows.count)
        .Replace "*please*", "True", MatchCase:=False
        .Replace "*delete*", "True", MatchCase:=False
        .Replace "*me*", "True", MatchCase:=False
        .Columns(1).SpecialCells(2, 4).EntireRow.Delete
    End With

  2. #2
    Valued Forum Contributor natefarm's Avatar
    Join Date
    04-22-2010
    Location
    Wichita, Kansas
    MS-Off Ver
    2016
    Posts
    1,020

    Re: Use other workbook to act as a 'stop-list'

    Would this external workbook be already open?

  3. #3
    Valued Forum Contributor natefarm's Avatar
    Join Date
    04-22-2010
    Location
    Wichita, Kansas
    MS-Off Ver
    2016
    Posts
    1,020

    Re: Use other workbook to act as a 'stop-list'

    Assuming it's already open, this should work for you. In the other workbook, select the range of cells to compare against, and name it "DelList". Code could be added to automatically open and close it if needed.
    Sub DeleteRows()
    Dim Wkbk As String, ColNum As Long, RwCnt As Long
    
        Application.ScreenUpdating = False
        
        Wkbk = "Other Workbook.xlsx"
        ColNum = ActiveSheet.UsedRange.Columns.Count + 1
        RwCnt = ActiveSheet.UsedRange.Rows.Count
    
        Cells(1, ColNum).Formula = "=If(ISNA(Vlookup(A1," & Wkbk & "!DelList,1,False)),"""",True)"
        Cells(1, ColNum).AutoFill Range(Cells(1, ColNum), Cells(RwCnt, ColNum))
        
        On Error Resume Next
        With Columns(ColNum)
            .Copy
            .PasteSpecial (xlPasteValues)
            .SpecialCells(xlCellTypeConstants, xlLogical).EntireRow.Delete
            .Delete
        End With
        Range("A1").Select
        Application.ScreenUpdating = True
        MsgBox RwCnt - ActiveSheet.UsedRange.Rows.Count & " rows deleted."
    End Sub
    Last edited by natefarm; 01-08-2014 at 05:15 PM. Reason: Forgot to explain about adding DelList

  4. #4
    Forum Contributor
    Join Date
    08-23-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    404

    Re: Use other workbook to act as a 'stop-list'

    Hi natefarm,

    Sorry for the late reply. No the workbook would not be open but I guess I could use a Workbook Open event to open it and then hide it? I shall have a play with your code. Many thanks.

  5. #5
    Forum Contributor
    Join Date
    08-23-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    404

    Re: Use other workbook to act as a 'stop-list'

    Hi nate,

    Hmm I couldn't get it to work. I did as you said - named the range in other workbook 'DelList' and saved the workbook. I then reopened it and ran my code from another file (which contained some criteria for deletion) but upon running the code a dialog box opened up asking me to select a file to 'update values'.

    But I already had Other Workbook.xlsx open?

    After I cancelled it the code then deleted everything in Col A?

  6. #6
    Forum Contributor
    Join Date
    08-23-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    404

    Re: Use other workbook to act as a 'stop-list'

    Hi nate,

    Got it working. I just needed to change this:

     Cells(1, ColNum).Formula = "=If(ISNA(Vlookup(A1," & Wkbk & "!DelList,1,False)),"""",True)"
    To this:

        Cells(1, ColNum).Formula = "=If(ISNA(Vlookup(A1," & "'" & Wkbk & "'" & "!DelList,1,False)),"""",True)"

  7. #7
    Valued Forum Contributor natefarm's Avatar
    Join Date
    04-22-2010
    Location
    Wichita, Kansas
    MS-Off Ver
    2016
    Posts
    1,020

    Re: Use other workbook to act as a 'stop-list'

    Very good! If you need some ideas on how to optionally open the other workbook, this is one way:

    Sub DeleteRows()
    Dim Pth as String, Wkbk As String, ColNum As Long, RwCnt As Long
    
        Application.ScreenUpdating = False
        
        Wkbk = "Other Workbook.xlsx"
        Pth = "C:\yourpath\"
    
        On Error GoTo OpenWorkbook
        Workbooks(Wkbk).Activate
    Continue:
        On Error GoTo 0
    
        ColNum = ActiveSheet.UsedRange.Columns.Count + 1
        RwCnt = ActiveSheet.UsedRange.Rows.Count
    
        Cells(1, ColNum).Formula = "=If(ISNA(Vlookup(A1," & "'" & Wkbk & "'" & "!DelList,1,False)),"""",True)"
        Cells(1, ColNum).AutoFill Range(Cells(1, ColNum), Cells(RwCnt, ColNum))
        
        On Error Resume Next
        With Columns(ColNum)
            .Copy
            .PasteSpecial (xlPasteValues)
            .SpecialCells(xlCellTypeConstants, xlLogical).EntireRow.Delete
            .Delete
        End With
        Range("A1").Select
        Application.ScreenUpdating = True
        MsgBox RwCnt - ActiveSheet.UsedRange.Rows.Count & " rows deleted."
    
    OpenWorkbook:
        On Error GoTo FileNotFound
        Workbooks.Open Pth & Wkbk
        GoTo Continue
    
    FileNotFound:
        MsgBox Err.Description
     End Sub

+ 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. [SOLVED] Macro to stop workbook being saved
    By bouncingbudha in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-01-2013, 02:35 PM
  2. [SOLVED] Stop an excel workbook or worksheet from opening if workbook isn't in a certain folder
    By Raulus in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-05-2013, 05:30 AM
  3. Macro for Stop to delete workbook
    By Tufail in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-29-2006, 04:00 PM
  4. STOP WORKBOOK CLOSING
    By KandK in forum Excel General
    Replies: 2
    Last Post: 05-05-2006, 04:45 AM
  5. Stop Workbook from closing.
    By beauty_bobaloo in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-23-2006, 08:50 AM

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