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 <<<
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?
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: