Macro to select columns and replace data
I'm not sure where you're starting or where you're finishing -- or if you're
doing contiguous columns...
And it looks like you may be fixing formulas...
So maybe...
Option Explicit
Sub testme02()
Dim myWhat As String
Dim myWith As String
Dim FirstCol As Long
Dim LastCol As Long
Dim iCol As Long
Dim CalcMode As Long
CalcMode = Application.Calculate
Application.Calculation = xlCalculationManual
With ActiveSheet
FirstCol = .Range("b1").Column
LastCol = .Range("iv1").Column
For iCol = FirstCol To LastCol Step 1 'right?
'the previous column
myWhat = .Cells(1, iCol - 1).Address(0, 0) 'like B1
'remove the 1 and add $
myWhat = Left(myWhat, Len(myWhat) - 1) & "$"
'the current column
myWith = .Cells(1, iCol).Address(0, 0) 'like B1
'remove the 1 and add $
myWith = Left(myWith, Len(myWhat) - 1) & "$"
'just for testing
'MsgBox "what:=" & myWhat & vbLf & "with:=" & myWith
.Columns(iCol).Replace what:=myWhat, _
Replacement:=myWith, LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
Next iCol
End With
Application.Calculation = CalcMode
End Sub
graemeh wrote:
Ron, I need it to step from column to column and do a replacement for each
coulmn as it goes. Currently I have 37 columns that I need to step do a
replacement command.
"Ron" wrote:
On Jul 8, 4:14 pm, graemeh wrote:
I want to loop through a range of colums and do a replacement like below. Is
there an easy way to do this via a marco?
Columns("AQ:AQ").Select
Selection.Replace What:="ap$", Replacement:="AQ$", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Columns("AR:AR").Select
Selection.Replace What:="ap$", Replacement:="Ar$", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
This should work for you.
Columns("AQ").Replace What:=" ap$", Replacement:="AQ$",
LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False
--
Dave Peterson
|