Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Spreadsheets (https://www.askmehelpdesk.com/forumdisplay.php?f=395)
-   -   Macro Query (https://www.askmehelpdesk.com/showthread.php?t=406584)

  • Oct 16, 2009, 08:15 AM
    cool143
    Macro Query
    Hi

    I wanted to create an application. I have a sheet "sheet1".there are 3 columns namely name, author and date.
    A B C
    1 Name Author Date
    2 harry Harry 7/2/2009
    3 Tom Moody 8/2/2009

    Condition is as below:
    If the system time is between 9:00 a.m to 5:00 p.m and name is "harry", I have to delete that rows. Else I wanted to delete all rows..

    Please advise.
  • Oct 16, 2009, 01:52 PM
    JBeaucaire

    Try this:

    Code:

    Sub DeleteByTimeName()
    'JBeaucaire  (10/16/2009)
    Dim LR As Long

    If Time > TimeValue("9:00 AM") And Time < TimeValue("5:00 PM") Then
        Range("A1").AutoFilter
        Range("A1").AutoFilter Field:=1, Criteria1:="harry"
        LR = Range("A" & Rows.Count).End(xlUp).Row
        If LR > 1 Then Range("A2:A" & LR).SpecialCells(xlCellTypeVisible).EntireRow.Delete xlShiftUp
        Range("A1").AutoFilter
    Else
        LR = Range("A" & Rows.Count).End(xlUp).Row
        Range("A2:A" & LR).SpecialCells(xlCellTypeVisible).EntireRow.Delete xlShiftUp
    End If

    End Sub

  • Oct 17, 2009, 04:45 AM
    cool143

    Excellent asnswer JB.

    Suppose if there are more than two condition like "harry","moody" and so on...

    Please advice in this situation.
  • Oct 17, 2009, 09:59 AM
    JBeaucaire

    Your question #1 was clearly stated and your conditions set forth so I was able to easily answer.

    Your follow-up question is not clear as to what you're after.
  • Oct 17, 2009, 10:07 PM
    cool143

    Thanks JB for your response.


    Condition is as below:
    If the system time is between 9:00 a.m to 5:00 p.m and name is "harry" and "Tom" and so on..

    In question#1, I have mentioned only one condition, i.e if the name "harry" exist I wanted to delete the row. Suppose same case, if there are more than one name. How we can put the condition.
  • Oct 18, 2009, 12:50 AM
    JBeaucaire

    Try this version:
    Code:

    Sub DeleteByTimeName()
    'JBeaucaire  (10/16/2009)
    Dim LR As Long, MyStr As Long, MyStrings
    MyStrings = Array("harry", "tom")

    If Time > TimeValue("9:00 AM") And Time < TimeValue("5:00 PM") Then
        Range("A1").AutoFilter
        For MyStr = 0 To UBound(MyStrings)
            Range("A1").AutoFilter Field:=1, Criteria1:=MyStrings(MyStr)
            LR = Range("A" & Rows.Count).End(xlUp).Row
            If LR > 1 Then Range("A2:A" & LR).SpecialCells(xlCellTypeVisible).EntireRow.Delete xlShiftUp
        Next MyStr
        Range("A1").AutoFilter
    Else
        LR = Range("A" & Rows.Count).End(xlUp).Row
        Range("A2:A" & LR).SpecialCells(xlCellTypeVisible).EntireRow.Delete xlShiftUp
    End If

    End Sub

  • Oct 18, 2009, 04:03 AM
    cool143

    Excellent JB. I will run that code and let u know.

  • All times are GMT -7. The time now is 02:08 PM.