Ask Experts Questions for FREE Help !
Ask
    cool143's Avatar
    cool143 Posts: 7, Reputation: 1
    New Member
     
    #1

    Oct 16, 2009, 08:15 AM
    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.
    JBeaucaire's Avatar
    JBeaucaire Posts: 5,426, Reputation: 997
    Software Expert
     
    #2

    Oct 16, 2009, 01:52 PM

    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
    cool143's Avatar
    cool143 Posts: 7, Reputation: 1
    New Member
     
    #3

    Oct 17, 2009, 04:45 AM

    Excellent asnswer JB.

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

    Please advice in this situation.
    JBeaucaire's Avatar
    JBeaucaire Posts: 5,426, Reputation: 997
    Software Expert
     
    #4

    Oct 17, 2009, 09:59 AM

    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.
    cool143's Avatar
    cool143 Posts: 7, Reputation: 1
    New Member
     
    #5

    Oct 17, 2009, 10:07 PM

    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.
    JBeaucaire's Avatar
    JBeaucaire Posts: 5,426, Reputation: 997
    Software Expert
     
    #6

    Oct 18, 2009, 12:50 AM

    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
    cool143's Avatar
    cool143 Posts: 7, Reputation: 1
    New Member
     
    #7

    Oct 18, 2009, 04:03 AM

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

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Macro application [ 3 Answers ]

Hi, I wanted to create an macro application. In 1st sheet, I have 5 columns and 1000 rows (may vary). In 2nd sheet, I have 5 columns and 1000 rows (may vary). Now I wanted to compare 1st sheet with 2nd sheet (Only one column) If cells in sheet1 is not equal to sheet2, copy that entire...

Macro help [ 3 Answers ]

Hi all, I am currently working with a dataset where the data in column A is empty until a user enters a rank (i.e. like ranking grades in ascending order). Currently the macro takes the number and moves the row entry to the top by assuming that the rank is to be the highest rank (it then takes...

DNS Query Query. A Query about DNS Queries... [ 12 Answers ]

I am a software developer, I don’t know much about networks. I have a working network but with one weird (well to me it’s weird) problem. If you can offer any insight I’ll be very grateful! <!--- Image Attachment Below (I couldn't find a way to paste it here in the editor :-( ) ---> ...


View more questions Search