+ Reply to Thread
Results 1 to 3 of 3

if value exists

Hybrid View

  1. #1
    Registered User
    Join Date
    03-14-2007
    Posts
    58

    if value exists

    Have a lot of data organized on worksheet "closed cases". Need the worksheet to be organized by day. Currently am calling the following macro from another macro and then using a delete duplicate rows to get the duplicate dates out but I know there's an easier way to do it code wise. Have over 6000 rows so the duplicate one is not ideal.

    I want the macro to check column B for today's date and if it exists, do nothing, if it does not exist, put today's date under the last row of data.

    Here's what I currently have. I put the paste special in there so that the date doesn't change. I'm not very good at if then statements yet

    
    Sub today()
        
    Sheets("Closed Cases").Select
    Range("B65000").End(xlUp).Offset(1).Select
    Range("B65000").End(xlUp).Offset(1).Formula = "=today()"
        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValues
    End Sub

  2. #2
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    here is one way

    I have replaced the

    Sub TodaysDate()
       Dim rFound As Range
       Dim lLR As Long
       
       lLR = Sheets("Closed Cases").Cells(Rows.Count, "b").End(xlUp).Row
       
       Set rFound = Sheets("Closed Cases").Range("b2:b" & lLR). _
          Find(what:=Date, SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
       If rFound Is Nothing Then
          Sheets("Closed Cases").Cells(lLR + 1, "b").Value = Date
       End If
       Set rFound = Nothing
    End Sub
    Please note that you should avoid using macro & variables names that match commands used by Excel & VBA. e.g. Today, Rows, Sheets etc
    Using such names can cause unexpected results

    For Your Info
    Most times you do not need to select sheets, cells etc to work with them
    Macros actually run faster & can be easier on the eyses if you do not select or activate sheets etc

    The code you posted could be replaced by a single line command

    Sheets("Closed Cases").Cells(Rows.Count, "b").End(xlUp).Offset(1).Value = Date
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  3. #3
    Registered User
    Join Date
    03-14-2007
    Posts
    58
    thank you

+ 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