[VBA] If Value is found in range, replace with specific text

MarkRCC

New Member
Joined
May 22, 2017
Messages
46
Hi All,

I want to have a button that will run a macro to change any cell in a range that has the value of "-" to "md" (for 'Missing Data', as this is what the program will detect).

How can I write the code for this? For reference, the column that I would like the changes to be made in is column I and the range is around 800 rows.

Thanks in advance!
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Sorry, in addition to this, I would also like the cell in Column K to change from 'A' to 'F' when the value is changed from '-' to 'md'

Under normal circumstances I would use an IF statement to change this value automatically based on the value in Column I, but as I am using Paste as Values, I am not able to use formula in these cells on the sheet.
 
Upvote 0
Try this macro:

Code:
Public Sub ReplaceDashWithMD()

With Range("I1", Cells(Rows.Count, "I").End(xlUp))
    .Replace "-", "md", LookAt:=xlWhole
End With

End Sub

WBD
 
Upvote 0
Ah. Didn't see your extra requirement. Try this instead then:

Code:
Public Sub ReplaceDashWithMD()

Dim allValues As Variant
Dim thisRow As Long

With Range("I1", Cells(Rows.Count, "K").End(xlUp))
    allValues = .Value
    For thisRow = 1 To UBound(allValues)
        If allValues(thisRow, 1) = "-" Then
            allValues(thisRow, 1) = "md"
            allValues(thisRow, 3) = "F"
        End If
    Next thisRow
    .Value = allValues
End With

End Sub

WBD
 
Upvote 0

Forum statistics

Threads
1,223,099
Messages
6,170,107
Members
452,302
Latest member
TaMere

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