Consulting

Results 1 to 7 of 7

Thread: Replace a text with a future data

  1. #1

    Replace a text with a future data

    HI!
    Trying to replace this:

    "Current + 4 Shortages" with "January Shortages"

    Code: Cells.Replace what:="Cur+4 Shortage", Replacement:=MonthName(4 + Month(Now())) & " Shortage

    The syntax was working last month, but It's not working on this month. Weird..

  2. #2
    VBAX Expert
    Joined
    May 2016
    Posts
    604
    Location
    it is because 9+4 =13 which isn't a month number, while 8+4 =12 which is december

  3. #3
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    I don't see how "Current + 4" could ever be found using search for "Cur+4"

    Cells.Replace What:="Current + 4 Shortages", Replacement:= _
            MonthName(Month(DateAdd("M", 4, Now))) & " Shortages"
    Greg

    Visit my website: http://gregmaxey.com

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Sub Main()  
      If 4 + Month(Date) > 12 Then
        Cells.Replace what:="Current + 4 Shortage", Replacement:=MonthName((4 + Month(Date) - 12)) & " Shortage"
        Else
        Cells.Replace what:="Current + 4 Shortage", Replacement:=MonthName(4 + Month(Date)) & " Shortage"
      End If
    End Sub

  5. #5
    Hi Greg,

    It was typo on my end.

    By the way, it worked... Thank you so much.

    Enjoy your day.

  6. #6
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    You're welcome. I didn't even pick up the "13" issue because I just don't ever code that way.
    Greg

    Visit my website: http://gregmaxey.com

  7. #7
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Sub M_snb()
        Cells.Replace "Current + 4", MonthName((Month(Date) + 3) Mod 12 + 1)
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •