If a cell contains certain keyword, then move values from 1 cell to another (same row)

zack8576

Active Member
Joined
Dec 27, 2021
Messages
271
Office Version
  1. 365
Platform
  1. Windows
I need to modify a bunch of excel files, for any row that contains the word "Skimmer", I need to move the value from column B (same row) to in front of Skimmer (always in column K)
like this: (1)Skimmer, (2)Skimmer, (3)Skimmer. (The value in column B does not need to be removed)

see below for example file, code is also below, this is not working for me....
any help is appreciated, dropbox link for the example file is also below

1672869342967.png


VBA Code:
Sub AssignStructureIDToSkimmer()
   With Range("K2:K" & Range("K" & Rows.Count).End(xlUp).Row)
      .Value = Evaluate(Replace(Replace("If(" & .Offset(, 10).Address & "=""Skimmer"","" ""&@,if(@="""","""",@))", "@", .Address), "#", .Offset(, 9).Address))
   End With
End Sub

 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Try this:

VBA Code:
Sub AssignStructureIDToSkimmer()
  With Range("K2:K" & Range("K" & Rows.Count).End(xlUp).Row)
    .Value = Evaluate(Replace("=IF({1},IF(@=""Skimmer"",""(""&" & .Offset(, -9).Address & "&"")""&@,@))", "@", .Address))
  End With
End Sub
 
Last edited:
Upvote 0
@DanteAmor
I think that will fill the blank cells in column K with 0 (at least it does for me)
Modified suggestion

VBA Code:
Sub AssignStructureIDToSkimmer()
  With Range("K2:K" & Range("K" & Rows.Count).End(xlUp).Row)
    .Value = Evaluate(Replace("=if(@=""Skimmer"",""(""&" & .Offset(, -9).Address & "&"")""&@,if(@="""","""",@))", "@", .Address))
  End With
End Sub
 
Upvote 0
I think that will fill the blank cells in column K with 0 (at least it does for me)
It doesn't happen for me.

Before:
Dante Amor
ABCJK
1
21Skimmer
32a
43b
54Skimmer
65c
76d
87Skimmer
98e
10
11
12
13
Hoja3


After:
Dante Amor
ABCJK
1
21(1)Skimmer
32a
43b
54(4)Skimmer
65c
76d
87(7)Skimmer
98e
10
11
12
13
Hoja3
 
Upvote 0
@DanteAmor
I think that will fill the blank cells in column K with 0 (at least it does for me)
Modified suggestion

VBA Code:
Sub AssignStructureIDToSkimmer()
  With Range("K2:K" & Range("K" & Rows.Count).End(xlUp).Row)
    .Value = Evaluate(Replace("=if(@=""Skimmer"",""(""&" & .Offset(, -9).Address & "&"")""&@,if(@="""","""",@))", "@", .Address))
  End With
End Sub
Thank Peter for the quick response, your code works perfectly for me.
Try this:

VBA Code:
Sub AssignStructureIDToSkimmer()
  With Range("K2:K" & Range("K" & Rows.Count).End(xlUp).Row)
    .Value = Evaluate(Replace("=IF({1},IF(@=""Skimmer"",""(""&" & .Offset(, -9).Address & "&"")""&@,@))", "@", .Address))
  End With
End Sub
Your solution works flawlessly, on the 4 files I've tested...Peter's code works perfectly as well...
I wonder if there is a way to select both of your answers as solution.....
 
Upvote 0
@DanteAmor
I think that will fill the blank cells in column K with 0 (at least it does for me)
Modified suggestion

VBA Code:
Sub AssignStructureIDToSkimmer()
  With Range("K2:K" & Range("K" & Rows.Count).End(xlUp).Row)
    .Value = Evaluate(Replace("=if(@=""Skimmer"",""(""&" & .Offset(, -9).Address & "&"")""&@,if(@="""","""",@))", "@", .Address))
  End With
End Sub
both yours and Dante's work perfectly, any way I can select 2 solutions for 1 question ? :rolleyes:
 
Upvote 0
It doesn't happen for me.
You don't have any blank cells in column K in your sample.

Before:

23 01 05.xlsm
ABCJK
1
21Skimmer
32a
43
54Skimmer
65
76
87Skimmer
98e
10
Skimmer


After:

23 01 05.xlsm
ABCJK
1
21(1)Skimmer
32a
430
54(4)Skimmer
650
760
87(7)Skimmer
98e
10
Skimmer


Having said that, since the OP says both codes work perfectly, perhaps those blank cells in the OP's sheet are not actually blank.

I wonder if there is a way to select both of your answers as solution.....
No, you can only mark one. So, if you are not getting any 0 values showing up in what appear to be blank cells in column A in your first image from either code, then mark Dante's post since he was first in. :)
 
Upvote 0
You don't have any blank cells in column K in your sample.
You're right, my macro puts zeros.
Here my code with the correction pointed out by Peter:

VBA Code:
Sub AssignStructureIDToSkimmer()
  With Range("K2:K" & Range("K" & Rows.Count).End(xlUp).Row)
    .Value = Evaluate(Replace("=IF({1},IF(@="""","""",IF(@=""Skimmer"",""(""&" & .Offset(, -9).Address & "&"")""&@,@)))", "@", .Address))
  End With
End Sub
 
Upvote 0
Solution
Here my code with the correction pointed out by Peter:
Does your version actually require the IF({1}...) part of the formula? Mine doesn't, and therefore the OP's shouldn't (also using 365), require that extra calculation.
Without that, our codes are identical apart from the order.
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,175
Members
452,615
Latest member
bogeys2birdies

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