+ Reply to Thread
Results 1 to 13 of 13

Can a Message box be brought up in the middle of code asking for criteria?

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Can a Message box be brought up in the middle of code asking for criteria?

    Hello all,

    I have a worksheet with several columns (30+). One of these columns is "Date Opened". This "date opened" column can have dates as early as 01/01/89 and as recent as today.

    Can a macro be made that does this

    1. Data/Sorts the whole worksheet by the "Date opened" column
    2. Brings up a message box in xx/xx/xxxx where format saying "Please enter the earliest date opened you would like to use, all other rows will be deleted"
    3. Do just that, delete all rows that have a date earlier than the date entered in the message box.

    So if the date of 01/01/2009 were entered into the message box, the macro would delete all dates in the "Date Opened" column that were older than that date.

    And then, continue on with the rest of the sub, if neccessary.


    thanks,

    duugg
    Last edited by duugg; 07-04-2009 at 11:14 AM.

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,627

    Re: Can a Message box be brought up in the middle of code asking for criteria?

    Sub Test()
         Dim D As Date, DateCol As Range, Dx As Range
         
         Set DateCol = Range("A:A")
    
         For Each Dx in DateCol
              If Dx="" Then Exit For
              If Dx < D Then Dx.EntireRow.Delete
         Next Dx
    End Sub
    Ben Van Johnson

  3. #3
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Re: Can a Message box be brought up in the middle of code asking for criteria?

    proton,

    Sorry, but Data/Sort didn't work nor did a message box come up. Also, I could be wrong but it appears that the code points to column A. the "Date Opened" column could be in any column letter, not just limited to column A.

    thanks

  4. #4
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,627

    Re: Can a Message box be brought up in the middle of code asking for criteria?

    My mistake, you didn't specify a column for the "Date Opened" so I just used column A with some random date values entered. So:

    duuggzdates.xls

  5. #5
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Re: Can a Message box be brought up in the middle of code asking for criteria?

    ProtonLeah,

    Sorry, the message box did pop up but that was it. I entered the date and nothing happened. All dates older didn't delete, data wasn't sorted either. Also, I intentionally moved the "date opened" column to another column and got the error

    "Run-Time Error 1004" Sort method of range class failed"

    Also, this macro should run until it finds the first blank cell, once it does, it can stop.


    thanks

  6. #6
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,627

    Re: Can a Message box be brought up in the middle of code asking for criteria?

    This one works but you will have to specify the rows and columns in the macro.
    Option Explicit
    Sub test()
        'this macro is built expecting dates in column A, i.e., col #1 and row #2
        
        Dim TestDate    As Date, _
            DateRange   As Range, _
            DateCol     As String, _
            LastRow     As Long, _
            ctrl        As Long, _
            FirstRow    As Long
    
        TestDate = InputBox("enter the earliest date to use: mm/dd/yyyy")
        
        DateCol = "A"       '<============== change to suit
        FirstRow = 2        '<============== change to suit
        LastRow = Cells(Rows.Count, DateCol).End(xlUp).Row
        
        Set DateRange = Range(DateCol & FirstRow & ":" & DateCol & LastRow)
    
        Range(DateCol & FirstRow).Select
        Selection.Sort _
            Key1:=Range(DateCol & FirstRow), _
            Order1:=xlAscending, _
            Header:=xlGuess, _
            OrderCustom:=1, _
            MatchCase:=False, _
            Orientation:=xlTopToBottom
            
        Application.ScreenUpdating = False
        
        'start at the bottom of the list and work upward
        For ctrl = LastRow To FirstRow Step -1
            Select Case Range(DateCol & ctrl).Value
                Case Is < TestDate
                    Range(DateCol & ctrl).EntireRow.Delete
                Case Is > TestDate
                    Range(DateCol & ctrl).EntireRow.Delete
            End Select
        Next ctrl
        
        Application.ScreenUpdating = True
    End Sub
    duuggzdates.xls

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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