• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Find and replace cell value using IF statement VBA

dububer

New Member
Can someone please help me on this? I'm new to Macro and I'm trying to create a code which will replace the value of a cell from numbers to Age bracket. If the number in a cell is 7 then it will change to "<12 hours", 26 then should be replaced to "24-36 hours" and so on. My problem is when I'm trying to run my macro, it only get the first condition which is "<12 hours" when it didn't even met the criteria. Appreciate the help.

Please see my code.

>>> use code - tags <<<

Code:
Sub Ticket_Age()

'Declare variable for Ticket Age
Dim rCell As Range
Dim age_lastrow As Long, y As Long
Dim age_sheet As Worksheet


Set age_sheet = ThisWorkbook.Worksheets("Sheet1")
age_lastrow = ThisWorkbook.Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
Set rCell = age_sheet.Range("A2:A" & age_lastrow)


    For y = 2 To age_lastrow
        On Error Resume Next
        'In Selection
        If rCell.Value < 12 And rCell.Value > -5 Then
        rCell.Value = "<12 hours"
        ElseIf rCell.Value > 12 And rCell.Value < 24 Then
        rCell.Value = "12-24 hours"
        ElseIf rCell.Value > 24 And rCell.Value < 36 Then
        rCell.Value = "24-36 hours"
        ElseIf rCell.Value > 36 And rCell.Value < 48 Then
        rCell.Value = "36-48 hours"
        ElseIf rCell.Value > 48 Then rCell.Value = "48+"
        Else
        rCell.Value = "Error"

        End If

    Next y

End Sub

Update: Found out a way to make it work but another problem is when I rerun the macro, the already converted cells will update again and gives me the Else value which is "Error" how can I fix or prevent the result being overwritten whenever I re-run the macro?

Code:
Sub Ticket_Age()

'Declare variable for Ticket Age
'Dim rCell As Range
Dim age_lastrow As Long, y As Long
Dim age_sheet As Worksheet


Set age_sheet = ThisWorkbook.Worksheets("Sheet1")
age_lastrow = ThisWorkbook.Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
'Set rCell = age_sheet.Range("A2:A" & age_lastrow)


    For y = 2 To age_lastrow
        On Error Resume Next
       
        If age_sheet.Range("A" & y).Value <= 12 And age_sheet.Range("A" & y) > -5 Then
        age_sheet.Range("A" & y) = "<12 hours"
        If age_sheet.Range("A" & y).Value = "<12 hours" Then age_sheet.Range("A" & y) = "<12 hours"
        ElseIf age_sheet.Range("A" & y).Value > 12 And age_sheet.Range("A" & y).Value <= 24 Then
        age_sheet.Range("A" & y).Value = "12-24 hours"
        ElseIf age_sheet.Range("A" & y).Value > 24 And age_sheet.Range("A" & y).Value <= 36 Then
        age_sheet.Range("A" & y).Value = "24-36 hours"
        ElseIf age_sheet.Range("A" & y).Value > 36 And age_sheet.Range("A" & y).Value <= 48 Then
        age_sheet.Range("A" & y).Value = "36-48 hours"
        ElseIf age_sheet.Range("A" & y).Value > 48 And age_sheet.Range("A" & y).Value < 100 Then
        age_sheet.Range("A" & y).Value = "48+ hours"
        Else
        age_sheet.Range("A" & y).Value = "Error"
       
        End If

    Next y

End Sub
 
Last edited:
.
The following is presently coded to change selected term on all sheets ...

Code:
Option Explicit

Sub FindReplaceAll_CountReplacements()
'PURPOSE: Find & Replace text/values throughout entire workbook, notify user of how many cells were affected
'SOURCE: www.TheSpreadsheetGuru.com

Dim sht As Worksheet
'Dim fnd As Variant
'Dim rplc As Variant
Dim ReplaceCount As Long

Dim fnd, rplc As String

fnd = InputBox("Enter OLD term. ", "Old Term ?")
rplc = InputBox("Enter NEW term. ", "New Term ?")

For Each sht In ActiveWorkbook.Worksheets

  ReplaceCount = ReplaceCount + Application.WorksheetFunction.CountIf(sht.Cells, "*" & fnd & "*")

  sht.Cells.Replace what:=fnd, Replacement:=rplc, _
    LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
    SearchFormat:=False, ReplaceFormat:=False
    
Next sht

MsgBox "I have completed my search and made replacements in " & ReplaceCount & " cell(s)."

End Sub
 
Back
Top