+ Reply to Thread
Results 1 to 9 of 9

how to create a worksheet OPEN event to scan a sheet based on dates (expired)

Hybrid View

  1. #1
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    how to create a worksheet OPEN event to scan a sheet based on dates (expired)

    this is a continuation of this POST:

    so what i am looking for is the following:

    Use the worksheet open event to scan the sheet "Closed POs" and look for expired dates (anything more than a month) in column D.

    i already have the code for the change event and that works. what is needed now is the above.

    can anyone help me out with this?

  2. #2
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: how to create a worksheet OPEN event to scan a sheet based on dates (expired)

    Hi again.

    I would recommend that you use a separate routine to process the cells like this
    Sub processDates(ByVal Target As Range)
    
    Dim cell As Range
    
    for each cell in Target.Cells
    Select Case cell.Value
    Case Is < Now - 30
    MsgBox "Move to Closed PO archive file"
    Case Is >= Now - 30
    MsgBox "Keep for now"
    Case Else
    MsgBox "Case Else"
    End Select
    next cell
    End Sub
    then call that as required
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim KeyCells As Range
    ' The variable KeyCells contains the cells that will
    ' cause an alert when they are changed.
    Set KeyCells = Range("d:d")
    
    If Not Application.Intersect(KeyCells, Target) Is Nothing Then
    processCells Application.Intersect(KeyCells, Target)
    End If
    End Sub
    and
    Private Sub Workbook_Open()
    with Sheets("")
    processCells Application.Intersect(.Range("d:d"), .UsedRange)
    end with
    end sub
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  3. #3
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: how to create a worksheet OPEN event to scan a sheet based on dates (expired)

    @xlnitwit,

    your sub is named ProcessDates but the following two macros used ProcessCells. should i change ProcessCells to ProcessDates?

  4. #4
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: how to create a worksheet OPEN event to scan a sheet based on dates (expired)

    Yes, sorry. It was typed freehand here so I confused myself!

  5. #5
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: how to create a worksheet OPEN event to scan a sheet based on dates (expired)

    so i got the stub to work, thanks for that.

    now i have to create the macro that moves the data from the workbook "Status of POs" on the "Closed POs" sheet to the "ClosedPOsOverAMonth" workbook on the "ClosedPOs" sheet. in fact there will only be one sheet in this workbook so best to refer to this as Sheets(1) i think.

    the first thing is to see if the "ClosedPOsOverAMonth.xlsm" is Open, if it's not then open it (my code dies at this part), note that i removed the if statement to get the worksheet to open but it still fails.

    then when the date changes in col D on the "Closed POs" sheet, it moves the row (A:D) to the next available row in "ClosedPOsOverAMonth". there is only one table on this sheet.

    so here is the code that is failing:

    Sub processCells(ByVal Target As Range)
    Dim ws As Worksheet
    Dim wb As Workbook
    Dim wbws As String
    Dim my_Filename As Variant
    
    Set wb = Workbooks.Open("Z:\OPERATIONS\Purchase Orders\Dan\ClosedPOsOverAMonth.xlsm")
    'if wb = False then Workbooks.Open("Z:\OPERATIONS\Purchase Orders\Dan\ClosedPOsOverAMonth.xlsm")
    Set ws = wb.Sheets("ClosedPOs")
    
    Dim cell As Range
    
        For Each cell In Target.Cells
            Select Case cell.Value
                Case Is < Now - 30
                    'MsgBox "Move to Closed PO archive file"
                    'Range("A" & Target.Row & ":D" & Target.Row).Copy
                    Set ws = Sheets("ClosedPOs")
                    ''Dim tbl As ListObject
                    Set tbl = ws.ListObjects(1)
                    Dim newrow As ListRow
                    Set newrow = tbl.ListRows.Add
    
                    With newrow
                        .Range(1).Value = Range("A" & Target.Row)
                        .Range(1).Font.StrikeThrough = True
                        .Range(2).Value = Range("B" & Target.Row)
                        .Range(2).Font.StrikeThrough = True
                        .Range(3).Value = Range("C" & Target.Row)
                        .Range(3).Font.StrikeThrough = True
                        .Range(4).Value = Range("D" & Target.Row)
                        .Range(4).Font.StrikeThrough = True
    
                        
                    End With
                    'Set ws = Sheets("Outstanding POs")
                    'Set tbl = ws.ListObjects(1)
                    'delete a row at the end of the table
                    'tbl.ListRows(Target.Row - 1).Delete
                    'move line to "ClosedPOs" table
                    Target.ClearContents
                    
                    'Target.Value = "Move to closed po file"
                Case Is >= Now - 30
                    'MsgBox "Keep for now"
                    Exit Sub
                Case Else
                    MsgBox "Case Else"
            End Select
        Next cell
    End Sub

  6. #6
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: how to create a worksheet OPEN event to scan a sheet based on dates (expired)

    im stuck on the wb and ws statements. it errors on the set tbl=ws.listobjects(1) line.

    any ideas on how to fix this error?

  7. #7
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: how to create a worksheet OPEN event to scan a sheet based on dates (expired)

    so i had to start over from the stub. i got this working (sortof), my question is how to move the target.row and add that to the table on ClosedPOs sheet?

                Case Is < Now - 30
                    Windows("Status of PO's.xlsm").Activate
                    Range("A" & Target.Row & ":D" & Target.Row).Select
                    Selection.Copy
                    Windows("ClosedPOsOverAMonth.xlsm").Activate
                    Range("Table1[Closed PO''s]").Select
                    ActiveSheet.Paste
                    'MsgBox "Archive"

  8. #8
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: how to create a worksheet OPEN event to scan a sheet based on dates (expired)

    this is what i have so far but its not copying the range, can someone take a look and see what the issue is?

    see the with newrow section for what is not working.

    With Workbooks("ClosedPOsOverAMonth.xlsm").Sheets("ClosedPOs")
    
        For Each cell In Target.Cells
            Select Case cell.Value
                Case Is < Now - 30
                    'ws = Sheets("ClosedPOs")
                    Windows("Status of PO's.xlsm").Activate
                    Range("A" & Target.Row & ":D" & Target.Row).Copy
                    MsgBox Range("A" & Target.Row).Value
                    Set tbl = .ListObjects(1)
                    Dim newrow As ListRow
                    Set newrow = tbl.ListRows.Add
                    Windows("ClosedPOsOverAMonth.xlsm").Activate
                    'Range("Table1[Closed PO''s]").Select
                    With newrow
                        .Range(1).Value = Range("A" & Target.Row).Value
                        'the messagebox below does not display anything, what am i doing wrong?
                        MsgBox Range("A" & Target.Row).Value
                        .Range(2).Value = Range("B" & Target.Row).Value
                        .Range(3).Value = Range("C" & Target.Row).Value
                        .Range(4).Value = Range("D" & Target.Row).Value
                    End With
                    'ActiveSheet.Paste
                    'MsgBox "Archive"
                Case Is >= Now - 30
                    'do nothing
                    MsgBox "Keep for now"
                    Exit Sub
                Case Else
                    MsgBox "Case Else"
            End Select
        Next cell
    End With

  9. #9
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: how to create a worksheet OPEN event to scan a sheet based on dates (expired)

    so i got this to work, like so:

    Sub processCells(ByVal Target As Range)
    Dim ws As Worksheet
    Dim wb As Workbook
    Dim wbws As String
    Dim my_Filename As Variant
    Dim cell As Range
    Dim newrow As ListRow
    
    If CheckFileIsOpen("Z:\OPERATIONS\Purchase Orders\Dan\ClosedPOsOverAMonth.xlsm") = False Then
    Workbooks.Open "Z:\OPERATIONS\Purchase Orders\Dan\ClosedPOsOverAMonth.xlsm"
    End If
    
    With Workbooks("ClosedPOsOverAMonth.xlsm").Sheets("ClosedPOs")
    
        For Each cell In Target.Cells
            Select Case cell.Value
                Case Is < Now - 30
                    Windows("Status of PO's.xlsm").Activate
                    Set tbl = Workbooks("ClosedPOsOverAMonth.xlsm").Sheets("ClosedPOs").ListObjects(1)
                    
                    Set newrow = tbl.ListRows.Add
                    MsgBox Workbooks("Status of PO's.xlsm").Sheets("Closed POs").Range("A" & Target.Row).Value
                    With newrow
                        .Range(1).Value = Workbooks("Status of PO's.xlsm").Sheets("Closed POs").Range("A" & Target.Row).Value
                        .Range(2).Value = Workbooks("Status of PO's.xlsm").Sheets("Closed POs").Range("B" & Target.Row).Value
                        .Range(3).Value = Workbooks("Status of PO's.xlsm").Sheets("Closed POs").Range("C" & Target.Row).Value
                        .Range(4).Value = Workbooks("Status of PO's.xlsm").Sheets("Closed POs").Range("D" & Target.Row).Value
                    End With
                    With Workbooks("Status of PO's.xlsm").Sheets("Closed POs")
                        Target.Delete
                    End With
                Case Is >= Now - 30
                    'do nothing
                    Exit Sub
                Case Else
                    MsgBox "Case Else"
            End Select
        Next cell
    End With
    End Sub
    thanks xlnitwit for pointing me in the right direction.

+ 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] vba to name command buttons on a userform based on cell value on worksheet open event
    By Sintek in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-30-2016, 02:53 PM
  2. Need expired dates to be moved to a different work sheet
    By tward in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-07-2014, 04:40 PM
  3. Replies: 2
    Last Post: 03-03-2013, 12:46 PM
  4. Replies: 1
    Last Post: 03-02-2013, 01:48 PM
  5. Replies: 4
    Last Post: 10-05-2012, 07:52 AM
  6. open sheet based on cell value with event
    By Zheno in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-28-2011, 01:12 AM
  7. Replies: 2
    Last Post: 08-04-2010, 07:36 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