简体   繁体   中英

Replacing multiple text in excel formula using VBA

Good morning,

I am seeking some assistance in how to properly replace multiple criteria within a formula using VBA - Excel. I have a userform at the beginning of the macro where the user will select the month that they are wanting to run the report for. I am using the previous month's report as the template and need to update the formula to reflect the proper months accordingly. I am looking to have the formula be applied to an entire column of data. I am shifting each month in the formula forward one month to capture the previous four (4) months of data.

For example: I am running a report for AUG. I will be using the JUL report that was ran the previous month as the template.

The formula that is currently in the report:

  =sum('JUL18'!$E$19+'JUN18'!$E$19+'MAY18'!$E$19+'APR18'!$E$19)/B6

I would like the formula to update to:

  =sum('AUG18'!$E$19+'JUL18'!$E$19+'JUN18'!$E$19+'MAY18'!$E$19)/B6

The code I currently have is:

If FormMonth.Value = "AUG" Then
     Columns("D:D").Select
 Selection.Replace What:="JUL", Replacement:="AUG", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
 Selection.Replace What:="JUN", Replacement:="JUL", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
 Selection.Replace What:="MAY", Replacement:="JUN", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
 Selection.Replace What:="APR", Replacement:="MAY", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
 Selection.Replace What:="MAR", Replacement:="APR", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
 Selection.Replace What:="FEB", Replacement:="MAR", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
End If

-The end result is as follows:

  =sum('AUG18'!$E$19+'AUG18'!$E$19+'JUN18'!$E$19+'MAY18'!$E$19)/B6

The first and last two months in the formula appear to update properly, however the JUN (supposed to update to JUL) jumps to AUG. It's as if it continues to loop through until it reaches the chosen form month.

Any ideas as to why this may be? Still becoming acclimated with VBA so the code may not be the prettiest.

You really don't need a macro, you can use indirect function.

formulas you will need:

'=TEXT(EOMONTH(B2,-1),"MMM") '=SUM(INDIRECT(B5&"!A1"),INDIRECT(B6&"!A1"),INDIRECT(B7&"!A1"))

在此处输入图片说明

You can try something like this to reduce the amount of repetitive code. Also, removed instanced of .Select

Sub Test()

Dim Arr1: Arr1 = Array("JUL", "JUN", "MAY", "APR", "MAR", "FEB")
Dim Arr2: Arr2 = Array("AUG", "JUL", "JUN", "MAY", "APR", "MAR")

Dim i As Long

With ThisWorkbook.Sheets("Sheet1").Range("D:D")
    For i = LBound(Arr1) To UBound(Arr2)
        .Replace What:=Arr1(i), Replacement:=Arr2(i), LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Next i
End With

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM