Replacing text

liampog

Active Member
Joined
Aug 3, 2010
Messages
308
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi

I have a range which has empty cells and either numbers or a piece of text.

Currently, I use a For/Next loop to go through each cell one-by-one but it takes a while to do this as the "Rota" range it's looking at is 7 columns by 165 rows.


Code:
                            For Each ShiftCell In Range("Rota")
                                
                                If ShiftCell <> "" Then
    
                                    If IsNumeric(ShiftCell) Then
                                        
                                        ShiftCell.Value = "HOL-" & ShiftCell
                                    
                                    Else
                                        
                                        If ShiftCell = "O" Then ShiftCell.Value = "OFF"
                                    
                                    End If
                                
                                End If

                            Next ShiftCell

Is there a faster and more efficient way of doing this?

Thanks
Liam
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
How about
VBA Code:
Sub liampog()
   With Range("Rota")
      .Value = Evaluate(Replace("If(isnumber(@),""HOL-""&@,if(@=""O"",""Off"",If(@="""","""",@)))", "@", .Address))
   End With
End Sub
 
Upvote 0
Hi Fluff

That resulted in erroneous data.

I am running this code from a separate workbook and I believe I've referenced it correctly but it's replacing data from the original workbook (where the code is run from).

Code:
With Workbooks(Rota_Filename).Sheets("Rota")
   .Range("Rota").Value = Evaluate(Replace("If(isnumber(@),""HOL-""&@,if(@=""O"",""OFF"",If(@="""","""",@)))", "@", .Range("Rota").Address))
End With

Any ideas?
 
Upvote 0
In that case try
VBA Code:
With Workbooks(Rota_Filename).Sheets("Rota")
  .Value = .Evaluate(Replace("If(isnumber(@),""HOL-""&@,if(@=""O"",""OFF"",If(@="""","""",@)))", "@", .Address))
End With
 
Upvote 0
That works perfectly!

Thanks so much and a MUCH quicker way of doing it!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,957
Messages
6,122,472
Members
449,087
Latest member
RExcelSearch

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