Find cell value and replace multiple instances

creases

New Member
Joined
Feb 8, 2020
Messages
5
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I've written this to code but the issue I'm having is that the code appears to replace only single instance of each unique text and not all of them. The columns are fixed, but the rows will be dynamic and there will be other tables (separated by a line break) above and below. The other issue I'm having is that I will have Run-time errors when the find text (12", 16" or 24") are not found. I don't think the 'On Error GoTo' I added in is working the way I need it to. Thank you in advance for any help or advice.

Current Data:
MARKTYPESERIESDEPTHQTY.LENGTH
BK1BLOCKA14"212"
BK1BLOCKB14"412"
BK1BLOCKC14"612"
BK2BLOCKD14"816"
BK2BLOCKE14"1016"
BK3BLOCKF14"1224"
BK3BLOCKG14"1424"


Desired Result:
MARKTYPESERIESDEPTHQTY.LENGTH
BK1BLOCKA14"LF2'
BK1BLOCKB14"LF4'
BK1BLOCKC14"LF6'
BK2BLOCKD14"LF11'
BK2BLOCKE14"LF13'
BK3BLOCKF14"LF24'
BK3BLOCKG14"LF28'


Current Code:
Sub Block()

'Change 12" Blocking to LF
On Error GoTo NotFound:
Cells.find(What:="12""", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Activate
ActiveCell.FormulaR1C1 = "=(1)*RC[-1]"
Selection.NumberFormat = "0""'"""
ActiveCell.Value = ActiveCell.Value
Selection.Offset(0, -1).Select
ActiveCell.Value = "LF"
NotFound:

'Change 16" Blocking to LF
On Error GoTo NotFound2:
Cells.find(What:="16""", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Activate
ActiveCell.FormulaR1C1 = "=(1.33)*RC[-1]"
Selection.NumberFormat = "0""'"""
ActiveCell.Value = ActiveCell.Value
Selection.Offset(0, -1).Select
ActiveCell.Value = "LF"
NotFound2:

'Change 24" Blocking to LF
On Error GoTo NotFound3:
Cells.find(What:="24""", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Activate
ActiveCell.FormulaR1C1 = "=(2)*RC[-1]"
Selection.NumberFormat = "0""'"""
ActiveCell.Value = ActiveCell.Value
Selection.Offset(0, -1).Select
ActiveCell.Value = "LF"
NotFound3:

End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hi,​
just following the VBA help example of Range.Find method avoids this bad coding from the Macro Recorder and On Error as well …​
 
Upvote 0
According to VBA help a demonstration as a beginner starter to paste to the worksheet module :​
VBA Code:
Sub Demo1()
    Dim V, R%, Rf As Range
        V = [{"12""",1;"16""",1.33;"24""",2}]
        Application.ScreenUpdating = False
    With UsedRange.Columns(6)
           .WrapText = False
        For R = 1 To UBound(V)
               Set Rf = .Find(V(R, 1), , xlValues, xlWhole)
            If Not Rf Is Nothing Then
                Do
                    Rf.NumberFormat = "0'_W"
                    Rf(1, 0).Resize(, 2).Value2 = Array("LF", Rf(1, 0).Value2 * V(R, 2))
                    Set Rf = .FindNext(Rf)
                Loop Until Rf Is Nothing
            End If
        Next
    End With
        Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi,​
just following the VBA help example of Range.Find method avoids this bad coding from the Macro Recorder and On Error as well …​
Hi, where can I find this VBA help example? Is there a post on this forum? I searched but couldn’t find what you were referring to.
 
Upvote 0
In VBE side whatever by the help search box or just placing the text cursor on a code statement and hitting F1 key …​
Or in MsDocs website …​
 
Upvote 0

Forum statistics

Threads
1,215,513
Messages
6,125,258
Members
449,219
Latest member
daynle

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