Adding character in front of changed cell value on change

Prevost

Board Regular
Joined
Jan 23, 2014
Messages
198
Hi There. I want the character "#" to be added in front of a cell value when a value is entered. I have this code but it is a worksheet_sheet change event and I am not sure if that is the correct way to go about it.
Thanks!

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim OriginalValue As Variant, NewValue As Variant
        If Target.Address = ("$E$5") Then
                OriginalValue = Range("E5").Value
                NewValue = "#" & OriginalValue
                Range("E5").Value = NewValue
        ElseIf Target.Address = ("$E$6") Then
                OriginalValue = Range("E6").Value
                NewValue = "#" & OriginalValue
                Range("E6").Value = NewValue
        End If
End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
You should disable/re-enable the events because making a change while in the change event causes the event to call itself recursively. That can cause huge problems. You could also use "Target" without any other supporting variables to simplify things a bit.

Gary

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Application.Intersect(ActiveSheet.Range("E5,E6"), Target) Is Nothing Then Exit Sub

Application.EnableEvents = False
    Target.Value = "#" & Target.Value
Application.EnableEvents = True


End Sub
 
Upvote 0
Hi There. I want the character "#" to be added in front of a cell value when a value is entered.
Does the # sign have to physically be in the cell, or can it only look like it is there (the value entered by the user would remain the value in the cell)? If the latter, consider Custom Formatting the cell(s) using this Type pattern...

\#@
 
Upvote 0

Forum statistics

Threads
1,216,101
Messages
6,128,837
Members
449,471
Latest member
lachbee

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