Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 43
Default replace text with a loop

If I wanted to replace text "A3" with "A4" I would do this

Cells.Replace What:="A3", _
Replacement:="A4", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False

How could I put this in a loop to also replace also "B3" with "B4", "C3"
with "C4", all the way to "Z3" with "Z4"?

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default replace text with a loop

Give this a try...

Dim Index As Long
For Index = Asc("A") To Asc("Z")
Cells.Replace What:=Chr(Index) & "3", _
Replacement:=Chr(Index) & "4", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
Next

--
Rick (MVP - Excel)


"Scooter" wrote in message
...
If I wanted to replace text "A3" with "A4" I would do this

Cells.Replace What:="A3", _
Replacement:="A4", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False

How could I put this in a loop to also replace also "B3" with "B4", "C3"
with "C4", all the way to "Z3" with "Z4"?


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default replace text with a loop

Option Explicit
Sub testme()

Dim lCtr As Long

With ActiveSheet
For lCtr = Asc("A") To Asc("Z")
.Cells.Replace What:=Chr(lCtr) & "3", _
Replacement:=Chr(lCtr) & "4", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
Next lCtr
End With

End Sub



Scooter wrote:

If I wanted to replace text "A3" with "A4" I would do this

Cells.Replace What:="A3", _
Replacement:="A4", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False

How could I put this in a loop to also replace also "B3" with "B4", "C3"
with "C4", all the way to "Z3" with "Z4"?


--

Dave Peterson
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 43
Default replace text with a loop

Thanks that worked like a charm. It brought to light another question.
How can I go past Z to say AA, AB, AC...? (these letters represent the Excel
column letters).

"Rick Rothstein" wrote:

Give this a try...

Dim Index As Long
For Index = Asc("A") To Asc("Z")
Cells.Replace What:=Chr(Index) & "3", _
Replacement:=Chr(Index) & "4", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
Next

--
Rick (MVP - Excel)


"Scooter" wrote in message
...
If I wanted to replace text "A3" with "A4" I would do this

Cells.Replace What:="A3", _
Replacement:="A4", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False

How could I put this in a loop to also replace also "B3" with "B4", "C3"
with "C4", all the way to "Z3" with "Z4"?



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 43
Default replace text with a loop

Thanks that worked like a charm. It brought to light another question.
How can I go past Z to say AA, AB, AC...? (these letters represent the Excel
column letters).


"Dave Peterson" wrote:

Option Explicit
Sub testme()

Dim lCtr As Long

With ActiveSheet
For lCtr = Asc("A") To Asc("Z")
.Cells.Replace What:=Chr(lCtr) & "3", _
Replacement:=Chr(lCtr) & "4", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
Next lCtr
End With

End Sub



Scooter wrote:

If I wanted to replace text "A3" with "A4" I would do this

Cells.Replace What:="A3", _
Replacement:="A4", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False

How could I put this in a loop to also replace also "B3" with "B4", "C3"
with "C4", all the way to "Z3" with "Z4"?


--

Dave Peterson



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default replace text with a loop

Maybe you can use something like:

Option Explicit
Sub testme()

Dim lCtr As Long
Dim myStr As String

With ActiveSheet
For lCtr = .Range("a1").Column To .Range("IV1").Column
'.address(0,0) will be like A1 or BA1 or IV1
myStr = .Cells(1, lCtr).Address(0, 0)
'remove the 1, to get A or BA or IV
myStr = Left(myStr, Len(myStr) - 1)
.Cells.Replace What:=myStr & "3", _
Replacement:=myStr & "4", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
Next lCtr
End With

End Sub



Scooter wrote:

Thanks that worked like a charm. It brought to light another question.
How can I go past Z to say AA, AB, AC...? (these letters represent the Excel
column letters).

"Dave Peterson" wrote:

Option Explicit
Sub testme()

Dim lCtr As Long

With ActiveSheet
For lCtr = Asc("A") To Asc("Z")
.Cells.Replace What:=Chr(lCtr) & "3", _
Replacement:=Chr(lCtr) & "4", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
Next lCtr
End With

End Sub



Scooter wrote:

If I wanted to replace text "A3" with "A4" I would do this

Cells.Replace What:="A3", _
Replacement:="A4", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False

How could I put this in a loop to also replace also "B3" with "B4", "C3"
with "C4", all the way to "Z3" with "Z4"?


--

Dave Peterson


--

Dave Peterson
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default replace text with a loop

Since you are doing a xlPart search, the posted code will handle those
situations automatically... if you have C3, AC3 or even HC3 in a cell, the
C3 to C4 replacement will catch all the occurrences automatically

--
Rick (MVP - Excel)


"Scooter" wrote in message
...
Thanks that worked like a charm. It brought to light another question.
How can I go past Z to say AA, AB, AC...? (these letters represent the
Excel
column letters).

"Rick Rothstein" wrote:

Give this a try...

Dim Index As Long
For Index = Asc("A") To Asc("Z")
Cells.Replace What:=Chr(Index) & "3", _
Replacement:=Chr(Index) & "4", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
Next

--
Rick (MVP - Excel)


"Scooter" wrote in message
...
If I wanted to replace text "A3" with "A4" I would do this

Cells.Replace What:="A3", _
Replacement:="A4", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False

How could I put this in a loop to also replace also "B3" with "B4",
"C3"
with "C4", all the way to "Z3" with "Z4"?




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
loop and fine & replace NewToVB Excel Programming 2 July 30th 07 05:36 PM
any function to replace loop [email protected] Excel Programming 1 December 14th 06 10:54 PM
Msg Box on each Loop to Replace text Ricky Pang Excel Programming 10 May 10th 06 11:47 PM
Replace using Do loop Dave B[_9_] Excel Programming 1 November 29th 05 07:35 PM
Replace Loop Darrell[_4_] Excel Programming 1 November 21st 03 04:49 PM


All times are GMT +1. The time now is 11:35 PM.

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

About Us

"It's about Microsoft Excel"