VBA move character to next column

jaik22

Board Regular
Joined
Sep 23, 2016
Messages
102
I want to write a code that If column A contains REG then move it over to column B and remove it from A.
For example

before
a1 b1
tsf-REG

after

a1 b1
tsf REG


How can I write code for this kind of purpose?
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Give this a try

Code:
Option Explicit


Sub RegX()
    Dim i As Long, lr As Long, x As Long
    lr = Range("A" & Rows.Count).End(xlUp).Row
    For i = 1 To lr
        If InStr(1, Range("A" & i), "REG") > 0 Then
            x = InStr(1, Range("A" & i), "REG")
            Range("A" & i) = Left(Range("A" & i), Len(Range("A" & i)) - 5)
            Range("B" & i) = "REG"
        End If
    Next i


End Sub
 
Upvote 0
Give this a try
Code:
Option Explicit

Sub RegX()
    Dim i As Long, lr As Long, x As Long
    lr = Range("A" & Rows.Count).End(xlUp).Row
    For i = 1 To lr
        If InStr(1, Range("A" & i), "REG") > 0 Then
            x = InStr(1, Range("A" & i), "REG")
            Range("A" & i) = Left(Range("A" & i), Len(Range("A" & i)) - 5)
            Range("B" & i) = "REG"
        End If
    Next i
End Sub
Here is another way to write this macro...
Code:
[table="width: 500"]
[tr]
	[td]Sub RegX2()
  Dim LastRow As Long
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  Range("B1:B" & LastRow) = Evaluate(Replace("IF(ISNUMBER(FIND(""-"",A1:A#)),REPLACE(A1:A#,1,FIND(""-"",A1:A#),""""),IF(B1:B#="""","""",B1:B#))", "#", LastRow))
  Columns("A").Replace "-*", "", xlPart
End Sub[/td]
[/tr]
[/table]
 
Upvote 0

Forum statistics

Threads
1,214,629
Messages
6,120,630
Members
448,973
Latest member
ChristineC

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