ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Macro to delete characters in field (https://www.excelbanter.com/excel-programming/362310-macro-delete-characters-field.html)

LisaVH

Macro to delete characters in field
 
I want to create a simple macro that will start at the active cell and delete
the first 4 characters and then move the next cell and delete the first 4
characters and so on. Can this be done. I tried to record it using the
keyboard and it takes the cell contents and copies it to the next cell.

[email protected]

Macro to delete characters in field
 
A fixed range or a variable one - if fixed

on error resume next
'error trap in case you haven't got 4 characters or more
for each cell in range("A1:A100") ' or whatever the range is
'cell is simply a variable in this context
cell.value=right(cell.value,len(cell.value)-4)
next


DS

Macro to delete characters in field
 
Hi Lisa,

try:

Sub ShortenIt()

Range("A1").Select
Do
Do Until Selection = ""
Selection = Right$(Selection, (Len(Selection) - 4))
Selection.Offset(1, 0).Select
Exit Do
Loop
Loop Until Selection = ""

End Sub

Your method of moving through the range you want can vary, but the central
part will work for varying length entries. (note that this is set to run
through column A from A1 until it hits a blank cell, change this if you need
a different range to be checked!)

HTH
DS



"LisaVH" wrote:

I want to create a simple macro that will start at the active cell and delete
the first 4 characters and then move the next cell and delete the first 4
characters and so on. Can this be done. I tried to record it using the
keyboard and it takes the cell contents and copies it to the next cell.


LisaVH

Macro to delete characters in field
 
Thanks to you both. That was exactly what I needed.

"DS" wrote:

Hi Lisa,

try:

Sub ShortenIt()

Range("A1").Select
Do
Do Until Selection = ""
Selection = Right$(Selection, (Len(Selection) - 4))
Selection.Offset(1, 0).Select
Exit Do
Loop
Loop Until Selection = ""

End Sub

Your method of moving through the range you want can vary, but the central
part will work for varying length entries. (note that this is set to run
through column A from A1 until it hits a blank cell, change this if you need
a different range to be checked!)

HTH
DS



"LisaVH" wrote:

I want to create a simple macro that will start at the active cell and delete
the first 4 characters and then move the next cell and delete the first 4
characters and so on. Can this be done. I tried to record it using the
keyboard and it takes the cell contents and copies it to the next cell.



All times are GMT +1. The time now is 01:03 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com