Find Text & Replace Adjacent Cell's Formula

Trikageon

Board Regular
Joined
Apr 29, 2010
Messages
51
Hey all! I'm looking for a code I can run in VBA that can look through a worksheet for a specific word and, if found, replace the formulas for the 3 cells immediately to the left of it. For Example:

I want it to look through column I:I and everytime it sees the word "Total" I want it to replace the contents of the cells in the same row for columns F,G, & H to these formulas respectively:

F:
Code:
 =IF(E18="Cost Per Stop",ROUND(SUMIF(P:P,"sum1",F:F)/SUMIF(S:S,"stop",F:F),2),IF(E18="Cost Per Directory",ROUND(SUMIF(P:P,"sum1",F:F)/SUMIF(S:S,"Book",F:F),2),IF(E18="Margin Percent (directs)",ROUND(F16/SUMIF(P:P,"sum1",F:F),2),IF(E18="Margin Percent (Sales)",-ROUND(F17/F14,2),IF(E18="Gross Margin",-((SUMIF(P:P,"sum1",F:F))+G15),IF(E18="total directs",SUMIF(P:P,"Sum"&N17,F:F),SUMIF(R:R,"Total"&Q16,F:F)))))))

G:
Code:
 =IF(E18="Cost Per Stop",ROUND(SUMIF(P:P,"sum1",G:G)/SUMIF(S:S,"stop",G:G),2),IF(E18="Cost Per Directory",ROUND(SUMIF(P:P,"sum1",G:G)/SUMIF(S:S,"Book",G:G),2),IF(E18="Margin Percent (directs)",ROUND(G16/SUMIF(P:P,"sum1",G:G),2),IF(E18="Margin Percent (Sales)",-ROUND(G17/G14,2),IF(E18="Gross Margin",-((SUMIF(P:P,"sum1",G:G))+G15),IF(E18="total directs",SUMIF(P:P,"Sum"&N17,G:G),SUMIF(R:R,"Total"&Q16,G:G)))))))

H:
Code:
 =IF(E18="Cost Per Stop",ROUND(SUMIF(P:P,"sum1",H:H)/SUMIF(S:S,"stop",H:H),2),IF(E18="Cost Per Directory",ROUND(SUMIF(P:P,"sum1",H:H)/SUMIF(S:S,"Book",H:H),2),IF(E18="Margin Percent (directs)",ROUND(H16/SUMIF(P:P,"sum1",H:H),2),IF(E18="Margin Percent (Sales)",-ROUND(H17/H14,2),IF(E18="Gross Margin",-((SUMIF(P:P,"sum1",H:H))+H15),IF(E18="total directs",SUMIF(P:P,"Sum"&N17,H:H),SUMIF(R:R,"Total"&Q16,H:H)))))))

Any ideas? Thanks in advance!
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Try

Code:
Sub test()
Dim LR As Long, i As Long
LR = Range("I" & Rows.Count).End(xlUp).Row
For i = 1 To LR
    If Range("I" & i).Value = "Total" Then
        Range("F" & i).Formula = "=IF(E18=""Cost Per Stop"",ROUND(SUMIF(P:P,""sum1"",F:F)/SUMIF(S:S,""stop"",F:F),2),IF(E18=""Cost Per Directory"",ROUND(SUMIF(P:P,""sum1"",F:F)/SUMIF(S:S,""Book"",F:F),2),IF(E18=""Margin Percent (directs)"",ROUND(F16/SUMIF(P:P,""sum1"",F:F),2),IF(E18=""Margin Percent (Sales)"",-ROUND(F17/F14,2),IF(E18=""Gross Margin"",-((SUMIF(P:P,""sum1"",F:F))+G15),IF(E18=""total directs"",SUMIF(P:P,""Sum""&N17,F:F),SUMIF(R:R,""Total""&Q16,F:F)))))))"
        Range("G" & i).Formula = "=IF(E18=""Cost Per Stop"",ROUND(SUMIF(P:P,""sum1"",G:G)/SUMIF(S:S,""stop"",G:G),2),IF(E18=""Cost Per Directory"",ROUND(SUMIF(P:P,""sum1"",G:G)/SUMIF(S:S,""Book"",G:G),2),IF(E18=""Margin Percent (directs)"",ROUND(G16/SUMIF(P:P,""sum1"",G:G),2),IF(E18=""Margin Percent (Sales)"",-ROUND(G17/G14,2),IF(E18=""Gross Margin"",-((SUMIF(P:P,""sum1"",G:G))+G15),IF(E18=""total directs"",SUMIF(P:P,""Sum""&N17,G:G),SUMIF(R:R,""Total""&Q16,G:G)))))))"
        Range("H" & i).Formula = "=IF(E18=""Cost Per Stop"",ROUND(SUMIF(P:P,""sum1"",H:H)/SUMIF(S:S,""stop"",H:H),2),IF(E18=""Cost Per Directory"",ROUND(SUMIF(P:P,""sum1"",H:H)/SUMIF(S:S,""Book"",H:H),2),IF(E18=""Margin Percent (directs)"",ROUND(H16/SUMIF(P:P,""sum1"",H:H),2),IF(E18=""Margin Percent (Sales)"",-ROUND(H17/H14,2),IF(E18=""Gross Margin"",-((SUMIF(P:P,""sum1"",H:H))+H15),IF(E18=""total directs"",SUMIF(P:P,""Sum""&N17,H:H),SUMIF(R:R,""Total""&Q16,H:H)))))))"
    End If
Next i
End Sub
 
Upvote 0
Something like this!!
Code:
Sub ReplaceFormulas()
Dim LR As Long
Dim myRng As Range
Dim C As Range
Dim stAdd As String
LR = Cells(Rows.Count, "I").End(xlUp).Row
Set myRng = Range("$I$2:$I" & LR)
With myRng
  Set C = .Find("Total", LookIn:=xlValues)
  If Not C Is Nothing Then
     stAdd = C.Address
     Do
       Cells(C.Row, "F").Formula = "=IF(E18=""Cost Per Stop"",ROUND(SUMIF(P:P,""sum1"",F:F)/SUMIF(S:S,""stop"",F:F),2),IF(E18=""Cost Per Directory"",ROUND(SUMIF(P:P,""sum1"",F:F)/SUMIF(S:S,""Book"",F:F),2),IF(E18=""Margin Percent (directs)"",ROUND(F16/SUMIF(P:P,""sum1"",F:F),2),IF(E18=""Margin Percent (Sales)"",-ROUND(F17/F14,2),IF(E18=""Gross Margin"",-((SUMIF(P:P,""sum1"",F:F))+G15),IF(E18=""total directs"",SUMIF(P:P,""Sum""&N17,F:F),SUMIF(R:R,""Total""&Q16,F:F)))))))"
       'similar for "G" and "H"
       Set C = .FindNext(C)
       Do While Not C Is Nothing And stAdd <> C.Address
   End If
End With
End Sub
Note the ""'s added to your formula!
Also, what does each of these formulas do?? My guess is there is another way (better??),

lenze
 
Upvote 0
Try

Code:
Sub test()
Dim LR As Long, i As Long
LR = Range("I" & Rows.Count).End(xlUp).Row
For i = 1 To LR
    If Range("I" & i).Value = "Total" Then
        Range("F" & i).Formula = "=IF(E18=""Cost Per Stop"",ROUND(SUMIF(P:P,""sum1"",F:F)/SUMIF(S:S,""stop"",F:F),2),IF(E18=""Cost Per Directory"",ROUND(SUMIF(P:P,""sum1"",F:F)/SUMIF(S:S,""Book"",F:F),2),IF(E18=""Margin Percent (directs)"",ROUND(F16/SUMIF(P:P,""sum1"",F:F),2),IF(E18=""Margin Percent (Sales)"",-ROUND(F17/F14,2),IF(E18=""Gross Margin"",-((SUMIF(P:P,""sum1"",F:F))+G15),IF(E18=""total directs"",SUMIF(P:P,""Sum""&N17,F:F),SUMIF(R:R,""Total""&Q16,F:F)))))))"
        Range("G" & i).Formula = "=IF(E18=""Cost Per Stop"",ROUND(SUMIF(P:P,""sum1"",G:G)/SUMIF(S:S,""stop"",G:G),2),IF(E18=""Cost Per Directory"",ROUND(SUMIF(P:P,""sum1"",G:G)/SUMIF(S:S,""Book"",G:G),2),IF(E18=""Margin Percent (directs)"",ROUND(G16/SUMIF(P:P,""sum1"",G:G),2),IF(E18=""Margin Percent (Sales)"",-ROUND(G17/G14,2),IF(E18=""Gross Margin"",-((SUMIF(P:P,""sum1"",G:G))+G15),IF(E18=""total directs"",SUMIF(P:P,""Sum""&N17,G:G),SUMIF(R:R,""Total""&Q16,G:G)))))))"
        Range("H" & i).Formula = "=IF(E18=""Cost Per Stop"",ROUND(SUMIF(P:P,""sum1"",H:H)/SUMIF(S:S,""stop"",H:H),2),IF(E18=""Cost Per Directory"",ROUND(SUMIF(P:P,""sum1"",H:H)/SUMIF(S:S,""Book"",H:H),2),IF(E18=""Margin Percent (directs)"",ROUND(H16/SUMIF(P:P,""sum1"",H:H),2),IF(E18=""Margin Percent (Sales)"",-ROUND(H17/H14,2),IF(E18=""Gross Margin"",-((SUMIF(P:P,""sum1"",H:H))+H15),IF(E18=""total directs"",SUMIF(P:P,""Sum""&N17,H:H),SUMIF(R:R,""Total""&Q16,H:H)))))))"
    End If
Next i
End Sub

Thanks for your reply/time! This code ran without any errors, but they didn't change anything (or at least not the cells I was hoping they would). I was thinking that possibly it might help if you knew that the presence of the word total (what we're looking for) is the result of a formula...I'm not sure if that changes anything, but just fyi.
 
Upvote 0
Something like this!!
Code:
Sub ReplaceFormulas()
Dim LR As Long
Dim myRng As Range
Dim C As Range
Dim stAdd As String
LR = Cells(Rows.Count, "I").End(xlUp).Row
Set myRng = Range("$I$2:$I" & LR)
With myRng
  Set C = .Find("Total", LookIn:=xlValues)
  If Not C Is Nothing Then
     stAdd = C.Address
     Do
       Cells(C.Row, "F").Formula = "=IF(E18=""Cost Per Stop"",ROUND(SUMIF(P:P,""sum1"",F:F)/SUMIF(S:S,""stop"",F:F),2),IF(E18=""Cost Per Directory"",ROUND(SUMIF(P:P,""sum1"",F:F)/SUMIF(S:S,""Book"",F:F),2),IF(E18=""Margin Percent (directs)"",ROUND(F16/SUMIF(P:P,""sum1"",F:F),2),IF(E18=""Margin Percent (Sales)"",-ROUND(F17/F14,2),IF(E18=""Gross Margin"",-((SUMIF(P:P,""sum1"",F:F))+G15),IF(E18=""total directs"",SUMIF(P:P,""Sum""&N17,F:F),SUMIF(R:R,""Total""&Q16,F:F)))))))"
       'similar for "G" and "H"
       Set C = .FindNext(C)
       Do While Not C Is Nothing And stAdd <> C.Address
   End If
End With
End Sub
Note the ""'s added to your formula!
Also, what does each of these formulas do?? My guess is there is another way (better??),

lenze

Thanks for your reply/time! I'm getting a compile error "End if without Block if." To answer your question about the formulas, basically what it's doing is replacing the hard coded totals of a report I imported with the equivilant of an autosums, based on certain criteria. I'm SURE there's a better/more efficient way of doing this, but I'm more-less a novice and this is the best I've come up with. All I know is it works when I do it manually, so if I can impliment this in my macro, I'll be set!!
 
Upvote 0
Is it just "Total" or could it be "Total widgets"?

Presently here's the code that's resulting in the display of "Total," if it helps...

Rich (BB code):
=IF(AND($E18="Gross Margin", ISBLANK(F18)),"",IF(OR($E18="Sub-total",$E18="Total Directs",$E18="Cost Per Directory",$E18="Cost Per Stop",$E18="Gross Margin",LEFT($E18,6)="Margin"),"Total",IF($F18="----------------","----------------",IF($F18="****************","****************",IF($C18>1,"Box","")))))
 
Upvote 0
Sorry, had a typo!!
Rich (BB code):
       Set C = .FindNext(C)
       Loop While Not C Is Nothing And stAdd <> C.Address
End If

lenze
 
Upvote 0
Sorry, had a typo!!
Rich (BB code):
       Set C = .FindNext(C)
       Loop While Not C Is Nothing And stAdd <> C.Address
End If

lenze

It ran through without an error, but it didn't change anything, that I can tell. I'm not sure if you caught it in my reply to the other gentleman, but the word "Total" that it's looking for is the result of a formula

Rich (BB code):
=IF(AND($E18="Gross Margin", ISBLANK(F18)),"",IF(OR($E18="Sub-total",$E18="Total Directs",$E18="Cost Per Directory",$E18="Cost Per Stop",$E18="Gross Margin",LEFT($E18,6)="Margin"),"Total",IF($F18="----------------","----------------",IF($F18="****************","****************",IF($C18>1,"Box","")))))

Not sure if that matters or not...
 
Upvote 0

Forum statistics

Threads
1,215,237
Messages
6,123,807
Members
449,127
Latest member
Cyko

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