Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Junior Member
 
Posts: 2
Default Macro to find/replace cells containing a number and letter

Hi,

I'm trying to find cells that contain a number and the letter A (such as "16A"), and replace them with just the number part (such as "16"). There are other cells that contain only letters (like "CAT") or only numbers (like "16") that I don't want to affect. Any advice would be appreciated.

Thanks!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 587
Default Macro to find/replace cells containing a number and letter

hi mb6g,


Sub Macro1()
Dim x1 As Boolean, x2 As Boolean, x3 As Boolean
Dim i As Integer, w As String, c As Range

For Each c In Range("A1:A10") 'range of cells to be determined
For i = Len(c) To 1 Step -1
On Error Resume Next
x1 = Mid(c, i, 1) = "A"
If Err.Number < 0 Then Err.Clear: x1 = False
x2 = IsNumeric(Mid(c, i - 1, 1))
If Err.Number < 0 Then Err.Clear: x2 = False
x3 = IsNumeric(Mid(c, i - 2, 1))
If Err.Number < 0 Then Err.Clear: x3 = False

If x1 And x2 And x3 Then
w = Mid(c, i - 2, 1) & Mid(c, i - 1, 1) & Mid(c, i, 1)
c.Replace What:=w, Replacement:="20A", LookAt:=xlPart
End If
Next
Next
End Sub

--
isabelle



Le 2012-06-08 16:25, mb6g a écrit :
Hi,

I'm trying to find cells that contain a number and the letter A (such as
"16A"), and replace them with just the number part (such as "16").
There are other cells that contain only letters (like "CAT") or only
numbers (like "16") that I don't want to affect. Any advice would be
appreciated.

Thanks!




  #3   Report Post  
Junior Member
 
Posts: 2
Default

Hi Isabelle,

Thanks, that worked great. I took out the x3 requirement, since some of my numbers only have one digit (such as 2A), and I simplified the replace line to:

If x1 And x2 Then c.Replace What:="A", Replacement:="", LookAt:=xlPart.

Thanks again for your help!

Mary


Quote:
Originally Posted by isabelle View Post
hi mb6g,


Sub Macro1()
Dim x1 As Boolean, x2 As Boolean, x3 As Boolean
Dim i As Integer, w As String, c As Range

For Each c In Range("A1:A10") 'range of cells to be determined
For i = Len(c) To 1 Step -1
On Error Resume Next
x1 = Mid(c, i, 1) = "A"
If Err.Number < 0 Then Err.Clear: x1 = False
x2 = IsNumeric(Mid(c, i - 1, 1))
If Err.Number < 0 Then Err.Clear: x2 = False
x3 = IsNumeric(Mid(c, i - 2, 1))
If Err.Number < 0 Then Err.Clear: x3 = False

If x1 And x2 And x3 Then
w = Mid(c, i - 2, 1) & Mid(c, i - 1, 1) & Mid(c, i, 1)
c.Replace What:=w, Replacement:="20A", LookAt:=xlPart
End If
Next
Next
End Sub

--
isabelle



Le 2012-06-08 16:25, mb6g a écrit :
Hi,

I'm trying to find cells that contain a number and the letter A (such as
"16A"), and replace them with just the number part (such as "16").
There are other cells that contain only letters (like "CAT") or only
numbers (like "16") that I don't want to affect. Any advice would be
appreciated.

Thanks!



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find and replace for cells with certain number of characters. JRC[_2_] Excel Worksheet Functions 8 December 28th 09 03:02 PM
replace or substitute a letter for a specific number El Criollo Excel Discussion (Misc queries) 4 December 7th 06 03:30 PM
replace letter of alfanumeric value by a specific number El Criollo Excel Discussion (Misc queries) 1 December 7th 06 01:09 PM
Find/Replace Macro with format changes to the replacing number [email protected] Excel Programming 0 June 28th 06 06:08 PM
How to exclude all letter using find and replace function? Paul Mak Excel Programming 0 May 5th 05 07:25 PM


All times are GMT +1. The time now is 11:40 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"