How do I repeat key strokes in Excel?

G

Guest

I tried recording the following macro.
F2 - Select current cell
Shift arrow left
backspace - (to delete one character)
shirt arrow left
backspace - (to delect one character)
enter - which advance to next cell down.

instead the macro records the value that I was working with after the 2
backspaces, instead I want it to perform the key strokes as entered.

Please help.
 
B

Bob Phillips

With ActiveCell
.Value = Left(.Value, Len(.Value) - 1)
.Offset(0, -1).Value = Left(.Offset(0, -1).Value,
Len(.Offset(0, -1).Value) - 1)
.Offset(1, -1).Select
End With

--

HTH

RP
(remove nothere from the email address if mailing direct)
 
D

Doug Glancy

Ross,

You can't record or code keystrokes to edit a cell. This truncates the two
cells to the left of the active cell, stripping the rightmost character:

Sub test()

With ActiveCell.Offset(0, -1)
.Value = Left$(.Value, Len(.Value) - 1)
End With
With ActiveCell.Offset(0, -2)
.Value = Left$(.Value, Len(.Value) - 1)
End With

End Sub

hth,

Doug
 
G

Guest

Here is some code to do what you have ascked...

Public Sub TrimLast2()
With ActiveCell
If Len(.Value) >= 2 Then
.Value = Left(.Value, Len(.Value) - 2)
.Offset(1, 0).Select
End If
End With
End Sub

HTH
 
G

Guest

ross_76102 said:
I tried recording the following macro.
F2 - Select current cell
Shift arrow left
backspace - (to delete one character)
shirt arrow left
backspace - (to delect one character)
enter - which advance to next cell down.

instead the macro records the value that I was working with after the 2
backspaces, instead I want it to perform the key strokes as entered.

Please help.

You'll need a macro for this. If I understand you correctly, you may be
able to use something like this ...


sub takeoffmyval()
dim c as range, s as range
set s = selection
for each c in s
c.value = trim(left(c.value, len(c.value) - 2))
next
end sub


Note this takes some assumptions into hand. Save your work before running.
To run:

From Excel press Alt + F11.
Press Ctrl + R
Select (Bold) file on left pane.
Select Insert (menu) | Module
Copy/Paste code above on right pane.
Alt + Q will return to Excel.
Ctrl + S will Save your workbook.
Alt + F8 will bring up the Macro dialog box.
Select 'takeoffmyval'.
Click Run.

HTH
 
G

Guest

If you are just wanting to remove the last two characters from a field you
could use a formula like this: =LEFT(A2,LEN(A2)-2)

Or if you want to do it via a macro this will do the same thing:

activecell.value = left(activecell.Value,len(activecell.Value)-2)

Hope this helps
Rowan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top