Need VB code to edit cell values

S

Stephen Ford

I need some VB to do the following please (My VB is not good enough yet)

Starting from the active cell (call it the StartCell)
In the cell to the left..
ThreeChars = Value.right(3) (the last three chars)
Value = Value - last three characters (assume value is text & at least 3
chars present)
In StartCell..
value = ThreeChars (as text, overwrite any contents of StartCell)
move active cell one down from StartCell
 
R

Rick Rothstein

I'm not sure if I completely understand your question... does this code do
what you want?

With ActiveCell
.Offset(0, -1).Value = Left(.Value, Len(.Value) - 3)
.Value = Right(.Value, 3)
End With
 
H

Helmut Meukel

Seems my answer didn't make it to the group, this is the second try.
(just changed the befoer value of the StartCell to XXX,
seems M$ gets offended by x_y_z_ without the underscores)

I agree with Rick, I don't quite understand what you want to do.
As Don wrote, you should provide examples of how your data
looks before and how it should be after.
That's how I understand it:
LeftCell StartCell
before: ABCDE XXX
after: AB CDE

Is that what you want?

Helmut.
 
S

Stephen_Ford

Yes, I think that's it. I'll check... back soon.

Rick Rothstein said:
I'm not sure if I completely understand your question... does this code do
what you want?

With ActiveCell
.Offset(0, -1).Value = Left(.Value, Len(.Value) - 3)
.Value = Right(.Value, 3)
End With
 
S

Stephen Ford

This works
With ActiveCell
.Value = Right(.Offset(0, -1).Value, 3)
.Offset(0, -1).Value = Left(.Offset(0, -1).Value,
Len(.Offset(0, -1).Value) - 3)
End With
ActiveCell.Offset(1, 0).Activate

It changes
CellX CellY
A03001 XXX (before)
A03 001 (after)

The code ".Offset(0, -1).Value" is repeated a lot. Can a variable / object
or whatever be assigned and used instead?

CellY is text. If is gets a value like "001" the cells shows an error. I can
use the mouse to Ignore Error. Can this be programmed or can the error be
avoided another way?
 
S

Stephen Ford

The latest version uses a variable for the text in the cell to left of the
active cell-:
Dim LocTxt As String
With ActiveCell
LocTxt = .Offset(0, -1).Value
.Value = Right(LocTxt, 3)
.Offset(0, -1).Value = Left(LocTxt, Len(LocTxt) - 3)
End With
ActiveCell.Offset(1, 0).Activate

But I would like to know how to stop the error checking for Number as Text
in the active cell.
 

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