VBA Repalce Formula with Changing Variable

Lars_C

New Member
Joined
Aug 21, 2017
Messages
7
Hey there,

I am new on here, please bear with me. I have already knocked my head against the wall for a few hours and done lots of researching of examples, but I can't get there unfortunately.

So what I was planning is pretty straight forward:
  • Copy 2 rows from above selected cell and insert
  • Replace a value in the equation (it is a number in a text directory, i.e. date) for the newly inserted 2 rows
  • Value of replacement to change based on dates. E.g. 20th of August = 20170820, 21st of August= 20170821.
  • The equation in which the number replacement is to take place is as follows:
"....Reports\[Report 20170815e.xlsm]12hr Report'!$AN$18"

  • So it is a number that I need to add 1 to every day, but it is a text replacement?

The values are processed fine, i.e. the value to be replaced "LastDay" and replacement value "NewDay" are ok. It is just the replacement that does not work.

Any help would be great.

Thank you,
Lars
Sub NewDay()
'
' NewDay Macro
'
Dim LastDay As Long
Dim NewDay As Long
LastDay = Worksheets("Info").Cells(5, "S").Value
NewDay = LastDay + 1

'Just to check that Values are ok, can be deleted later
Range("A43").Value = LastDay
Range("A44").Value = NewDay

ActiveCell.Offset(-2, 0).Rows("1:2").EntireRow.Select
Selection.Copy
ActiveCell.Offset(2, 0).Rows("1:2").EntireRow.Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
Selection.Replace What:="LastDay", Replacement:="NewDay", LookAt:=xlPart, _
SearchOrder:=xlByColumns, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Apologies, only just realized my bad formatting:

Rich (BB code):
 Sub NewDay()
 '
 ' NewDay Macro
 '
    Dim LastDay As Long
    Dim NewDay As Long
    LastDay = Worksheets("Info").Cells(5, "S").Value
    NewDay = LastDay + 1

 'Just to check that Values are ok, can be deleted later
    Range("A43").Value = LastDay
    Range("A44").Value = NewDay

    ActiveCell.Offset(-2, 0).Rows("1:2").EntireRow.Select
    Selection.Copy
    ActiveCell.Offset(2, 0).Rows("1:2").EntireRow.Select
    Selection.Insert Shift:=xlDown
    Application.CutCopyMode = False
    Selection.Replace What:="LastDay", Replacement:="NewDay", LookAt:=xlPart, _
        SearchOrder:=xlByColumns, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
 End Sub
 
Upvote 0

Forum statistics

Threads
1,224,829
Messages
6,181,218
Members
453,024
Latest member
Wingit77

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