Replace all characters within a cell apart from the 1st with *

hopeless88

New Member
Joined
Mar 19, 2014
Messages
1
I relatively new to VBA so not exactly sure of the best way to achive this:

I am trying to anonymise a column of data. I need to replace all letters in the cell with an * apart from the first letter.

I have tried this:

Sub AnonymiseCAR()
Dim CounterCAR
Dim ClientName As String

'Select CAR Worksheet
Worksheets("RAW DATA - CAR").Activate
CounterCAR = 2
Do
ClientName = "E" & CounterCAR
If ClientName = "" Then
Exit Do
Else
Replace(ClientName,*,"*",2)

CounterCAR = CounterCAR + 1
Loop

However I get a Compile error for the Replace line because of a Syntax error.

Any suggestions on how to do this would be great!

Thanks
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Something like
Code:
Dim oneCell as Range

With ThisWorkbook.Sheets("Raw data CAR").Range("E:E")
    For each oneCell in Range(.Cells(2, 1), .Cells(.Rows.Count, 1).End(xlup))
        oneCell.Value = Left(Left(oneCell.Value, 1) & String(Len(oneCell.Value), "*"), Len(oneCell.Value))
    Next oneCell
End With
 
Upvote 0
Here is another way for your to consider...
Code:
Dim Addr As String

Addr = "'Raw data CAR'!E2:E" & Cells(Rows.Count, "E").End(xlUp).Row
Range(Addr) = Evaluate(Replace("IF(LEN(@),LEFT(@)&REPT(""*"",LEN(@)-1),"""")", "@", Addr))
While it should be obvious, I think it important to point out that running either mikerickson's or my code will destroy (physically change) the data in Column E and you will not be able to Undo the changes made by the code.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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