Copying data from one column and moving that data in front of data in another column. (Only if specific criteria is met).

Livin404

Well-known Member
Joined
Jan 7, 2019
Messages
743
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Greetings, in Column G I will have a list of airport codes (3 letters), and in Column U I will have anther a list of three letter codes to indicate the routing. I would like only when the Codes "BGR" or "YQX" appear in Column G to immediately search the same row in Column U and select the the letter code before the "BGR" or "YQX" at the end of Column U. For example with CHS-HUA-OZP-SDA-OZP-BGR in Column U, I would have the macro go to the end and select the three letter code before the BGR and place it after "BGR" in Column G preceded by a dash. The end result in Column G would be BGR-OZP.
I have a Macro that I think is getting me close but it is erroring out at the c.Value=.... line. Thank you,

VBA Code:
Public Sub downlinE_In()
    Dim oWs As Worksheet, rng As Range, c As Range
    Set oWs = ThisWorkbook.Sheets("72 hr")
    Set rng = Application.Intersect(oWs.[G:G], oWs.UsedRange)
    For Each c In rng
       If CBool(InStr("|BGR|YQX|", "|" & c.Value & "|")) Then
             c.Value = c.Value & "_" & Mid(c.Offset(0, 14).Value, -4, 3)
                End If
         Next c
End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Maybe this way
VBA Code:
ublic Sub downlinE_In()
    Dim oWs As Worksheet, rng As Range, c As Range
    Set oWs = ThisWorkbook.Sheets("72 hr")
    Set rng = Application.Intersect(oWs.[G:G], oWs.UsedRange)
    For Each c In rng
       If CBool(InStr("|BGR|YQX|", "|" & c.Value & "|")) Then
            c = c & " - " & Mid(c.Offset(0, 14).Value, 17, 3)
                End If
         Next c
End Sub
 
Upvote 0
Maybe this way
VBA Code:
ublic Sub downlinE_In()
    Dim oWs As Worksheet, rng As Range, c As Range
    Set oWs = ThisWorkbook.Sheets("72 hr")
    Set rng = Application.Intersect(oWs.[G:G], oWs.UsedRange)
    For Each c I
         Next c
End Sub
In the example I displayed( CHS-HUA-OZP-SDA-OZP-BGR is just an example. The flight routing will not always have all those amount of three letter codes it can be as little as HUA-OZP-BGR. If “BGR” or “YQX” show up in Column G then the same three letter code will be at the end of Column U whether it is just Iwo codes like MHZ-BGR or eight codes. Thank you.
Maybe this way
VBA Code:
ublic Sub downlinE_In()
    Dim oWs As Worksheet, rng As Range, c As Range
    Set oWs = ThisWorkbook.Sheets("72 hr")
    Set rng = Application.Intersect(oWs.[G:G], oWs.UsedRange)
    For Each c In rng
       If CBool(InStr("|BGR|YQX|", "|" & c.Value & "|")) Then
            c = c & " - " & Mid(c.Offset(0, 14).Value, 17, 3)
                End If
         Next c
End Sub
I see the number 17 does that
 
Upvote 0
In that case can't you simply use the last 7 characters in Column "U" if the criteria is met
VBA Code:
Public Sub downlinE_In()
    Dim oWs As Worksheet, rng As Range, c As Range
    Set oWs = ThisWorkbook.Sheets("72 hr")
    Set rng = Application.Intersect(oWs.[G:G], oWs.UsedRange)
    For Each c In rng
       If CBool(InStr("|BGR|YQX|", "|" & c.Value & "|")) Then
            c = Right(c.Offset(0, 14).Value, 7)
                End If
         Next c
End Sub
 
Upvote 0
Solution
In that case can't you simply use the last 7 characters in Column "U" if the criteria is met
VBA Code:
Public Sub downlinE_In()
    Dim oWs As Worksheet, rng As Range, c As Range
    Set oWs = ThisWorkbook.Sheets("72 hr")
    Set rng = Application.Intersect(oWs.[G:G], oWs.UsedRange)
    For Each c In rng
       If CBool(InStr("|BGR|YQX|", "|" & c.Value & "|")) Then
            c = Right(c.Offset(0, 14).Value, 7)
                End If
         Next c
End Sub
That totally makes since I’ll try in the morning. Thank you.
 
Upvote 0
In that case can't you simply use the last 7 characters in Column "U" if the criteria is met
VBA Code:
Public Sub downlinE_In()
    Dim oWs As Worksheet, rng As Range, c As Range
    Set oWs = ThisWorkbook.Sheets("72 hr")
    Set rng = Application.Intersect(oWs.[G:G], oWs.UsedRange)
    For Each c In rng
       If CBool(InStr("|BGR|YQX|", "|" & c.Value & "|")) Then
            c = Right(c.Offset(0, 14).Value, 7)
                End If
         Next c
End Sub
Hello and thank you for the input, and sorry for the delayed response. I sometimes have a demanding work cycle. I did try the latest suggestion and unfortunately it didn't quite meet my desired outcome. What I'm looking for is a bit more tricky than I thought. I included a photo to illustrate the results I'm looking for. The same result I would also like for the code YQX. Thank you so much.

BGR.JPG
 
Upvote 0
In that case can't you simply use the last 7 characters in Column "U" if the criteria is met
VBA Code:
Public Sub downlinE_In()
    Dim oWs As Worksheet, rng As Range, c As Range
    Set oWs = ThisWorkbook.Sheets("72 hr")
    Set rng = Application.Intersect(oWs.[G:G], oWs.UsedRange)
    For Each c In rng
       If CBool(InStr("|BGR|YQX|", "|" & c.Value & "|")) Then
            c = Right(c.Offset(0, 14).Value, 7)
                End If
         Next c
End Sub
It is working properly with this code, than you for your help
VBA Code:
Sub downLineE_In()
Dim oWs As Worksheet
Dim Rng As Range, c As Range
Set oWs = ThisWorkbook.Sheets("72 hr")
With oWs
        Set Rng = Application.Intersect(.[G:G], .UsedRange)
        
  For Each c In Rng
        If Trim(UCase(c.Value)) = "BGR" Or Trim(UCase(c.Value)) = "YQX" Then
                If InStr(.Range("U" & c.Row).Value, "BGR") > 0 Or InStr(.Range("U" & c.Row).Value, "YQX") > 0 Then
                        .Range("G" & c.Row).Value = c.Value & "-" & Left(Right(.Range("U" & c.Row).Value, 7), 3)
                Else
                        .Range("G" & c.Row).Value = c.Value & "-" & Right(.Range("U" & c.Row).Value, 3)
                End If
        End If
 Next c
 End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,844
Members
449,051
Latest member
excelquestion515

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