+ Reply to Thread
Results 1 to 5 of 5

Finding a Date in Forumla via VBA

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-16-2010
    Location
    Australia
    MS-Off Ver
    Excel 2007; Excel 2010
    Posts
    178

    Finding a Date in Forumla via VBA

    Hi,

    I am having difficulty trying to find a date using VBA.

    I have a date range like so.

    Cell C5 =01/01/2012
    Cell D5 =C5+1
    Cell E5 = D5+1
    Cell F5 = E5+1

    Formatted as dd (day date only)

    Which gives me the dates ok

    When try to use find function in vba it just doesn't find any dates
    This is what I have
    Dim iRes As Integer, dFind As Date, strMonth As String, iM As Long
    Dim rngDate As Range
    
        iRes = MsgBox(Prompt:="Print range", Buttons:=vbYesNo)
        
        If iRes = vbNo Then
            Exit Sub
        ElseIf iRes = vbYes Then
            strMonth = Me.cboMonth.Text
            iM = Month(DateValue("01-" & strMonth & "-1900"))
            dFind = DateSerial(Me.txtYear.Value, iM, 1)
            Set rngDate = ActiveSheet.Range("C4:ND4").Find(what:=dFind, LookIn:=xlFormulas)
        End If
    rngDate always end up with nothing

    How do I go about the correct way?

    Thanks
    Last edited by Foreverlearning; 07-23-2012 at 05:28 PM.

  2. #2
    Forum Contributor
    Join Date
    03-21-2012
    Location
    Ho Chi Minh city
    MS-Off Ver
    Excel 2003
    Posts
    180

    Re:Refer to the following macro

    Option Explicit
    Sub gpeDateFind()
     Dim jJ As Byte, Format_ As String
     Dim rDat As Range, fRng As Range
     
     Randomize:             jJ = 9 + Rnd() * 240 \ 1
     Set rDat = Range([c5], [c5].End(xlToRight))
     Format_ = rDat.NumberFormat
     rDat.NumberFormat = "MM/DD/yyyy"
     Set fRng = rDat.Find(Format([c5].Value + jJ, "MM/dd/yyyy"), , xlValues, xlWhole)
     If fRng Is Nothing Then
        MsgBox "Nothing"
     Else
        MsgBox fRng.Address, , DateAdd("d", jJ, [c5].Value)
     End If
     rDat.NumberFormat = Format_
    End Sub

  3. #3
    Forum Contributor
    Join Date
    06-16-2010
    Location
    Australia
    MS-Off Ver
    Excel 2007; Excel 2010
    Posts
    178

    Re: Finding a Date in Forumla via VBA

    Hi,

    Sorry, but I am a liitle confused as to what really is happening here.

    But I can see that you have temporary formatted the dates to do a find then refromatted to original

    Can I ask what is the (Randomize: jJ = 9 + Rnd() * 240 \ 1) for?

    That is a bit confusing

    Thanks
    Lionel

  4. #4
    Forum Contributor
    Join Date
    03-21-2012
    Location
    Ho Chi Minh city
    MS-Off Ver
    Excel 2003
    Posts
    180

    Re: Finding a Date in Forumla via VBA

    [C5].value => #1/1/2012#

    In [D2] Add formula =9+INT(240*RAND())

    And {ENTER}

  5. #5
    Forum Contributor
    Join Date
    06-16-2010
    Location
    Australia
    MS-Off Ver
    Excel 2007; Excel 2010
    Posts
    178

    Re: Finding a Date in Forumla via VBA

    Thanks Sa DQ

    You have given me ideas for an alternative which works well.

    Appreciate your help

+ 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