Macro for CTRL+RIGHT then Backspace

ChingChoke

New Member
Joined
Feb 24, 2015
Messages
2
I have a need to frequently edit text in a cell. The text is always in the sequence "string1 spaces string2" where string can be any length and any type of character. I want to delete the leading string1 and spaces, ending up with just string2 (which may contain many spaces). Any length of spaces separate the strings. After F2, the keystrokes "Home, Ctrl+Right and then Backspace" do this perfectly. Examples:

123 abc
1234 abcde
AbCdEf 123AbcDE

I cannot figure out how to generate a simple macro that will do this (and cannot find past postings for same).

Any advice greatly appreciated.
 
Last edited:

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
hey.

Give this a go:

Code:
        tx = Split(ActiveCell.Value, " ")
        new_text = tx(UBound(tx))


You could use that in a macro like this. I've added comments so you can better understand what each part does.

Code:
Sub second_part_after_spaces()

    Range("A1").Activate ' Or which ever cell is at the top of your column
    Do Until ActiveCell.Value = ""
        ' Creates an array called tx. Array takes activecell.value and seperates where there are spaces
        tx = Split(ActiveCell.Value, " ")
        
        ' UBound(tx) finds the last element in the tx array.
        new_text = tx(UBound(tx))

        ActiveCell.Offset(1).Activate
    Loop

End Sub
 
Upvote 0
try either
Activecell = mid (activecell, instr (1, activecell, " ")+ 1, 99)

Or
sendkeys "{F2}+^{RIGHT} {DEL}{ENTER}
 
Upvote 0
Thank you eddvrs for your advice. I couldn't get your code to work for my problem - kept generating a blank cell. But, it did get me digging into string manipulation (I wasn't familiar with Split) so thank you for that. (See my other comments below for what worked.)

Jonmo1, your solution works to remove String1 plus 1 space (or other amount predefined), but not when there is an unknown number of spaces.

kennedy_clan, your approach is closest I think. I'm tweaking it for my data and seems to be getting me results.

Here's what I ended up using and it's working but it's clumsy (and relies on 2 or more characters at the start of String1, so not ideal):

ActiveCell.Select
A = Trim(Right(ActiveCell.Value, Len(ActiveCell.Value) - 2))
ActiveCell.Value = A

Note I am using two lines of code to reassign "A" value to ActiveCell. Not sure that's necessary but couldn't get it to work otherwise.

Thanks everyone for your input - very helpful to getting my brain engaged.

CC
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,875
Members
452,363
Latest member
merico17

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