Offset partial values to the left

zack8576

Active Member
Joined
Dec 27, 2021
Messages
271
Office Version
  1. 365
Platform
  1. Windows
I need to offset the first 3 characters in column O to the left, add them infront of the existing text in K (with parenthesis) ONLY WHEN the value in column F is P1

so in the example below, row 2, 7, and 14 have P1 in column F, after I run the macro column F on these rows will look like:
(D B) D Base,6"w
((D J) 58x60",Base,8"w,FT
(C B)C Box,6"w

1664479830276.png


this is what I have so far, and it is not working properly

VBA Code:
public lr6 As Long
public Lr11 As Long
Sub StructureID() 
    Set rng2 = Range("A1").CurrentRegion
    lr6 = rng2.Cells(Rows.Count, "F").End(3).Row
    lr11 = rng2.Cells(Rows.Count, "O").End(3).Row
    For i = lr6 To 2 Step -1
        If rng2.Cells(i, 6) Like "*P1*" Then
           lr11.Offset(0, 4).Value = Left(lr11.Value, 3)
        End If
    Next i
End Sub
 

Attachments

  • 1664479729005.png
    1664479729005.png
    66.6 KB · Views: 23

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Why you want to go through VBA when you can do same with a simple formula and helper column without much effort?
 
Upvote 0
Why you want to go through VBA when you can do same with a simple formula and helper column without much effort?
Because there are many files that need to be modified, and each file has a different number of rows that contains "P1", and these can be any row in the file.
 
Upvote 0
How about
Excel Formula:
Sub zack()
   With Range("K2:K" & Range("F" & Rows.Count).End(xlUp).Row)
      .Value = Evaluate(Replace("If(" & .Offset(, -5).Address & "=""P1"",""(""&left(" & .Offset(, 4).Address & ",3)&"") ""&@,if(@="""","""",@))", "@", .Address))
   End With
End Sub
 
Upvote 0
How about
Excel Formula:
Sub zack()
   With Range("K2:K" & Range("F" & Rows.Count).End(xlUp).Row)
      .Value = Evaluate(Replace("If(" & .Offset(, -5).Address & "=""P1"",""(""&left(" & .Offset(, 4).Address & ",3)&"") ""&@,if(@="""","""",@))", "@", .Address))
   End With
End Sub

thanks ! this works like a charm, AND this approach is simpler and much more elegant !

one more question, there are 2 exceptions, if the value in column O is "E Box" or "E Base", no offset is needed. How do I add this exception rule in there ?
If I figure this out myself somehow, I will post it here so others can see
 
Upvote 0
How about
VBA Code:
Sub zack()
   With Range("K2:K" & Range("F" & Rows.Count).End(xlUp).Row)
      .Value = Evaluate(Replace(Replace("If(" & .Offset(, -5).Address & "=""P1"",""(""&if((#=""E Box"")+(#=""E Base""),#,left(#,3))&"") ""&@,if(@="""","""",@))", "@", .Address), "#", .Offset(, 4).Address))
   End With
End Sub
 
Upvote 0
Solution
How about
VBA Code:
Sub zack()
   With Range("K2:K" & Range("F" & Rows.Count).End(xlUp).Row)
      .Value = Evaluate(Replace(Replace("If(" & .Offset(, -5).Address & "=""P1"",""(""&if((#=""E Box"")+(#=""E Base""),#,left(#,3))&"") ""&@,if(@="""","""",@))", "@", .Address), "#", .Offset(, 4).Address))
   End With
End Sub
this works beautifully, thanks a ton !
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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