vba getting data from a cell to use in find and replace

overbet

Board Regular
Joined
Jul 9, 2010
Messages
63
Office Version
  1. 2010
Hi, I am trying to tweak this code so that instead of manually typing in the code the new month in MM format every new month I can just reference the value in cell K1 that updates on its own.
Is there a way that I can replace the month replacement value "/01/" with the data that is in cell K1? In cell K1 I have this formula "=TEXT(TODAY(),"MM")"


Thanks in advance for any help.

Code:
Sub current_YYMM()
    Columns("I:I").Select
    
    'YEAR
            
    Selection.Replace What:="/YYYY/", Replacement:="/2019/", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
        ReplaceFormat:=False           


    ' MONTH   


    Selection.Replace What:="/MM/", Replacement:="/01/", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
        ReplaceFormat:=False       
              
    Range("J1").Select
    ActiveCell.FormulaR1C1 = "Current Date"
 
End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
I always like it when posters tell me their ultimate Goal. And not show me a script that does not work and want me to modify the script they have so it will do what they want
 
Upvote 0
MAybe this

Code:
With Columns("I:I")
    'YEAR
    .Replace What:="/YYYY/", Replacement:="/2019/", LookAt:=xlPart, SearchOrder:=xlByRows
    ' MONTH
    .Replace What:="/" & Range("K1").Value & "/", Replacement:="/01/", LookAt:=xlPart, SearchOrder:=xlByRows
End With
Range("J1").Value = "Current Date"
 
Upvote 0
I always like it when posters tell me their ultimate Goal. And not show me a script that does not work and want me to modify the script they have so it will do what they want


I didnt ask for your directly and I certainly didnt ask you to pollute my thread with drivel while adding zero value because you are in a bad place. Pause and think about how pathetic that is for a moment. You took the time not just to read my thread but actually spent your time reply to my post with no intention to help in a positive way and instead insult a person lacking a particular skill set. Is this forum not a place for those that need help and others that are willing to help? Why are you here if you are neither?

You may benefit from a break or some kind of therapeutic activity. Try exercise or coloring. All the best
 
Last edited:
Upvote 0
Thank you Michael. This got me to where I can get it to do what I was trying to from here. I typically record macros and tweak them the est I can so Im not very skilled with vba. I really appreciate you taking the time to help. Sending good karma your way. Have a great day.
 
Upvote 0
Glad to help...:beerchug:
But what happens when 01 becomes 02....might be worth considering allowing for that to happen, so the code will do whatever month you require !!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top