Apply Find and Replace Formula to cells in specific row in selected column

HelvetiiOnExcel

New Member
Joined
Aug 23, 2022
Messages
4
Office Version
  1. 365
Platform
  1. MacOS
Hi Everyone,

Long time lurker and first time poster. I am looking for advice on creating a macro that will apply the find and replace function to cells in specific rows of a selected column. I.e. user will select column 'E', from there cells in rows 5,9,10,23,45, would have all text after a '-' in their formula removed (-* with find and replace) and then cells in row 7, 16, 34, would have all text before a '+' removed (*+) with find and replace. Would appreciate any and all support. I've gotten the part of identifying the column value and such, but applying that to specific cells has been tough.
Thanks.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
You meant user will select only column(s) or just specific group of rows in column(s)?
If just column, then the Find will look for any row in that column for '-' or '+' and perform task according to sign found?
 
Upvote 0
Hi! Thanks for responding! Allow me to clarify. The user will select the column to apply the macro. Since there are other rows that use the - or + functions, the macro would have to work in only specific rows 5,9,10,23,45 (for -) and 7, 16, 34 (for +). The other rows remain untouched.
 
Upvote 0
Hi! Thanks for responding! Allow me to clarify. The user will select the column to apply the macro. Since there are other rows that use the - or + functions, the macro would have to work in only specific rows 5,9,10,23,45 (for -) and 7, 16, 34 (for +). The other rows remain untouched.
Those rows specified are always fixed?
 
Upvote 0
Try this. Select the column and run the code

VBA Code:
Sub Test()

Dim strFind As String
Dim nCol As Long, nLoc As Long
Dim Element, ArryPlus(), ArryMinus()
Dim rngData As Range
Dim ws As Worksheet

Set ws = ActiveWorkbook.ActiveSheet
nCol = ActiveCell.Column

ArryPlus = Array(7, 16, 34)
ArryMinus = Array(5, 9, 10, 23, 45)

For Each Element In ArryPlus
    nLoc = InStr(ws.Cells(Element, nCol), "+")
    If Not nLoc = 0 Then
        strFind = Left(ws.Cells(Element, nCol), nLoc - 1)
        ws.Cells(Element, nCol) = Replace(ws.Cells(Element, nCol), strFind, "")
    End If
Next
For Each Element In ArryMinus
    nLoc = InStr(ws.Cells(Element, nCol), "-")
    If Not nLoc = 0 Then
        strFind = Right(ws.Cells(Element, nCol), Len(ws.Cells(Element, nCol)) - nLoc)
        ws.Cells(Element, nCol) = Replace(ws.Cells(Element, nCol), strFind, "")
    End If
Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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