Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Computing > Computer Information > Macro To Replace things?

Reply
Thread Tools

Macro To Replace things?

 
 
John
Guest
Posts: n/a
 
      01-19-2004
Hello.

I have a macro which replaces certain numbers entered in column 1 of a
spreadsheet with different numbers.

I have a slight error in my macro though. This is an example of the
macro. It changes 123 to 1271, 234 to 5501 etc.

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 10/12/2003 by John
'
' Keyboard Shortcut: Ctrl+k
'
Cells.Replace What:="123", Replacement:="1271", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False
Cells.Replace What:="234", Replacement:="5501", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False
Cells.Replace What:="23", Replacement:="3006", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False
Cells.Replace What:="12", Replacement:="4000", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False
End Sub

Unfortunately the problem is that it doesn't look for whole numbers.
Instead of only just replacing the number 12 with 4000, what it will
do is replace any number containing 12 to that as well. For example
if I have 701249, it will replace the 12 in that number so I'll get
70400049.

Is there a way I can avoid this and make it so that it will only
replace the whole number if found and not parts of numbers?

Thanks for your help

John


 
Reply With Quote
 
 
 
 
Ken Wright
Guest
Posts: n/a
 
      01-19-2004
The answer is staring you in the face in each line of your code. Hint:-

LookAt:=xlPart (Take a guess at what it should be, and I mean a 'whole'
guess, not just 'part' of a guess)

--
Regards
Ken....................... Microsoft MVP - Excel
Sys Spec - Win XP Pro / XL 00/02/03

----------------------------------------------------------------------------
It's easier to beg forgiveness than ask permission
----------------------------------------------------------------------------



"John" <> wrote in message
news:...
> Hello.
>
> I have a macro which replaces certain numbers entered in column 1 of a
> spreadsheet with different numbers.
>
> I have a slight error in my macro though. This is an example of the
> macro. It changes 123 to 1271, 234 to 5501 etc.
>
> Sub Macro1()
> '
> ' Macro1 Macro
> ' Macro recorded 10/12/2003 by John
> '
> ' Keyboard Shortcut: Ctrl+k
> '
> Cells.Replace What:="123", Replacement:="1271", LookAt:=xlPart, _
> SearchOrder:=xlByRows, MatchCase:=False
> Cells.Replace What:="234", Replacement:="5501", LookAt:=xlPart, _
> SearchOrder:=xlByRows, MatchCase:=False
> Cells.Replace What:="23", Replacement:="3006", LookAt:=xlPart, _
> SearchOrder:=xlByRows, MatchCase:=False
> Cells.Replace What:="12", Replacement:="4000", LookAt:=xlPart, _
> SearchOrder:=xlByRows, MatchCase:=False
> End Sub
>
> Unfortunately the problem is that it doesn't look for whole numbers.
> Instead of only just replacing the number 12 with 4000, what it will
> do is replace any number containing 12 to that as well. For example
> if I have 701249, it will replace the 12 in that number so I'll get
> 70400049.
>
> Is there a way I can avoid this and make it so that it will only
> replace the whole number if found and not parts of numbers?
>
> Thanks for your help
>
> John
>
>



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.562 / Virus Database: 354 - Release Date: 16/01/2004


 
Reply With Quote
 
 
 
 
John
Guest
Posts: n/a
 
      01-19-2004
On Mon, 19 Jan 2004 21:44:59 -0000, "Ken Wright"
<> wrote:

>The answer is staring you in the face in each line of your code. Hint:-
>
>LookAt:=xlPart (Take a guess at what it should be, and I mean a 'whole'
>guess, not just 'part' of a guess)


Thanks for your help.

I've not done too much work with macros before but I'm an idiot for
not spotting that

John


 
Reply With Quote
 
Ken Wright
Guest
Posts: n/a
 
      01-20-2004
LOL - Shouldn't worry - We've all been there.

--
Regards
Ken....................... Microsoft MVP - Excel
Sys Spec - Win XP Pro / XL 00/02/03

----------------------------------------------------------------------------
It's easier to beg forgiveness than ask permission
----------------------------------------------------------------------------



"John" <> wrote in message
news:...
> On Mon, 19 Jan 2004 21:44:59 -0000, "Ken Wright"
> <> wrote:
>
> >The answer is staring you in the face in each line of your code. Hint:-
> >
> >LookAt:=xlPart (Take a guess at what it should be, and I mean a 'whole'
> >guess, not just 'part' of a guess)

>
> Thanks for your help.
>
> I've not done too much work with macros before but I'm an idiot for
> not spotting that
>
> John
>
>



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.562 / Virus Database: 354 - Release Date: 16/01/2004


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dedicated Macro or Normal Macro? John Ortt Digital Photography 5 11-22-2005 12:43 PM
Macro lens on a camera with a macro setting??? mitchell.chris@gmail.com Digital Photography 2 09-28-2005 07:55 AM
in S.E. Asia : Canon EOS 300d with 100 macro ED vs. Nikon D70 with Nikon 105 macro ? J. Cod Digital Photography 0 09-29-2004 05:46 AM
#define macro to enclose an older macro with strings Dead RAM C++ 20 07-14-2004 10:58 AM
macro name from macro? D Senthil Kumar C Programming 1 09-21-2003 07:02 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57