Run Macro Only In Highlighted Column

BWMagee

Board Regular
Joined
Feb 18, 2014
Messages
112
Hi, when I run the following Macro it runs for the entire worksheet, however I only want it to run in the highlighted column (eg. If I click on column "H" to select the whole column, I do not want the macro to run outside that column).

Here is the macro:


Sub ReplacNames()
'
' ReplaceNames Macro
'
' Keyboard Shortcut: Option+Cmd+Shift+R
'
Cells.Replace What:="NZL", Replacement:="NZ", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False
Cells.Replace What:="AUS", Replacement:="AU", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False

End Sub

Any help much appreciated!
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Maybe...

Code:
Sub ReplacNames()
'
' ReplaceNames Macro
'
' Keyboard Shortcut: Option+Cmd+Shift+R
'
With Selection.Cells
    .Replace What:="NZL", Replacement:="NZ", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False
    .Replace What:="AUS", Replacement:="AU", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False
End With

End Sub

M.
 
Upvote 0
Maybe...

Code:
Sub ReplacNames()
'
' ReplaceNames Macro
'
' Keyboard Shortcut: Option+Cmd+Shift+R
'
With Selection.Cells
    .Replace What:="NZL", Replacement:="NZ", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False
    .Replace What:="AUS", Replacement:="AU", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False
End With

End Sub

M.

Thanks for the response. Unfortunately it didn't work. Still changed cells that were not selected.
 
Upvote 0
Hello BWMagee,

I tested Marcelo's code and it worked seamlessly.

You could also try by using an input box:-

Code:
Sub ReplacNames()

Dim actColumn As String
actColumn = InputBox("Please enter the column letter to be activated.")

With Sheet1.Columns(actColumn)
     .Replace What:="NZL", Replacement:="NZ", LookAt:=xlPart, _
     SearchOrder:=xlByRows, MatchCase:=False
     .Replace What:="AUS", Replacement:="AU", LookAt:=xlPart, _
     SearchOrder:=xlByRows, MatchCase:=False
End With

End Sub

Cheerio,
vcoolio.
 
Upvote 0
Upvote 0

Forum statistics

Threads
1,215,518
Messages
6,125,292
Members
449,218
Latest member
Excel Master

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top