Looking for an Excel VBA solution to swap cell values between columns when a specific value is present

StackOverflow https://stackoverflow.com//questions/25082177

  •  02-01-2020
  •  | 
  •  

Question

SO won't let me post an image of my problem, please see here: http://i.imgur.com/PaZ6Dpt.png

The picture shows what I'm trying to do. I need a VBA script that looks in column H for "Service Desk" and swaps it for the value in Column L.

So in the pic, "Service Desk" in Column H would be swapped for "GDC - US Oracle DBA" from column L.

This is the last piece of a huge (for me) scripting project and I just cant figure it out :(.

Thanks.

My current code:

Sub sla_breach_formatter()

' Reformat the priority values to SLA Tracker Format.

Columns("F").Replace What:="1", Replacement:="1 - Critical", SearchOrder:=xlByColumns
Columns("F").Replace What:="2", Replacement:="2 - Business Impact", SearchOrder:=xlByColumns
Columns("F").Replace What:="3", Replacement:="3 - Standard", SearchOrder:=xlByColumns
Columns("F").Replace What:="4", Replacement:="4 - Non-Urgent", SearchOrder:=xlByColumns

' Reformat the task types to SLA Tracker Format.

Columns("K").Replace What:="Incident", Replacement:="INC", SearchOrder:=xlByColumns
Columns("K").Replace What:="Problem", Replacement:="PRB", SearchOrder:=xlByColumns
Columns("K").Replace What:="Service Request", Replacement:="SREQ", SearchOrder:=xlByColumns

' Reformat the Breach Type
    Columns("I").Replace What:="*RESO*", Replacement:="Resolution", LookAt:= _
        xlPart, SearchOrder:=xlByColumns, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Columns("I").Replace What:="*-RESP-FR-*", Replacement:="First Response", LookAt:=xlPart _
        , SearchOrder:=xlByColumns, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Columns("I").Replace What:="*-RESP-*", Replacement:="Response", LookAt:=xlPart _
        , SearchOrder:=xlByColumns, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Columns("I").Replace What:="*PROB*", Replacement:="Problem", LookAt:=xlPart _
        , SearchOrder:=xlByColumns, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False

'Swap out "Service Desk" Assignment Group with Last Assignment Group

    Dim cl As Range
    For Each cl In Range("$H$2:$H" & Range("$H65536").End(xlUp).Row)
    If UCase(cl) = "Service Desk" Then cl = Cells(cl.Row, 4)
    Next cl

' Format the dates to Euro standard

    Columns("A:B").Select
    Selection.NumberFormat = "dd/mm/yyyy;@"

' expand all the columns

    Cells.Columns.AutoFit

' Reset focus to A1

    Cells(1, 1).Select

End Sub
Was it helpful?

Solution

replace your For Each with the below. You are referencing the cell in Cells(row, 4) which is column D. You need to use the Offset method. Also UCase will upper case the value so you need to check against SERVICE DESK.

Dim cl As Range

For Each cl In Range("$H$2:$H" & Range("$H65536").End(xlUp).Row)
    If UCase(cl) = "SERVICE DESK" Then cl = cl.Offset(, 4)
Next cl
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top