+ Reply to Thread
Results 1 to 7 of 7

Add prefix depending what is entered in cell

Hybrid View

  1. #1
    Registered User
    Join Date
    02-22-2010
    Location
    croatia
    MS-Off Ver
    Excel 2007
    Posts
    15

    Question Add prefix depending what is entered in cell

    How to do this for example:

    In A1 i enter "john", and when i enter "john" the cell automatically adds prefix "long_" so final text says "long_john"

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Add prefix depending what is entered in cell

    Depending on the number of variations you have you could use a Custom Format, however, I suspect you probably have more than one prefix.

    What are the other prefixes you intend to you use ?

    Have you considered creating Defined Names, eg:

    Name: John
    RefersTo: long_john

    In native XL if you type

    =john

    the cell will display long_john

    If that's not viable for you you will need to consider using VBA.

  3. #3
    Registered User
    Join Date
    02-22-2010
    Location
    croatia
    MS-Off Ver
    Excel 2007
    Posts
    15

    Re: Add prefix depending what is entered in cell

    Evey cell in column is different but prefix is the same. Word needs to be this way:

    "R1" - prefix
    "something" - word entered in cell

    and final result "R1something"

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Add prefix depending what is entered in cell

    If you're happy to just use a "mask" then you can apply a Custom Format to the necessary range of: "R1"@

    Whenever a value (say Apple) is entered into this column (say Col A) the value will appear as R1Apple.

    However it's very important to note that though the R1 prefix appears & prints it does not actually exist in the cell ... the underlying value remains "Apple" (eg LEFT(A1;1) = "A" not "R")

  5. #5
    Registered User
    Join Date
    02-22-2010
    Location
    croatia
    MS-Off Ver
    Excel 2007
    Posts
    15

    Re: Add prefix depending what is entered in cell

    I need it existing in the cell

    So i need VBA for this? How to write a program for this and how to use it, etc. i'm beginner in this.

  6. #6
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Add prefix depending what is entered in cell

    Assuming the values are entered manually then you can use the Worksheet_Change event.

    Below is a basic example:

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim xlCalc As XlCalculation
    Dim rngColumn As Range, rngInterest As Range
    Dim strPrefix As String
    If Target(1).Value = "" Then Exit Sub
    On Error GoTo ExitPoint
    With Application
        xlCalc = .Calculation
        .Calculation = xlCalculationManual
        .EnableEvents = False
        .ScreenUpdating = False
    End With
    For Each rngColumn In Target.Columns
        Set rngInterest = Intersect(Target, rngColumn)
        Select Case rngColumn.Column
            Case 1
                strPrefix = "R1"
            Case 2
                strPrefix = "X1"
            Case 3
                strPrefix = "Y1"
            Case Else
                strPrefix = ""
        End Select
        rngInterest = strPrefix & rngInterest(1).Value
        Set rngInterest = Nothing
    Next rngColumn
    ExitPoint:
    With Application
        .Calculation = xlCalc
        .EnableEvents = True
        .ScreenUpdating = True
    End With
    End Sub
    In the above a prefix of R1 is added to cells changed in Column A, X1 for Column B & Y1 for Column C.

    To apply - from native XL - right click on tab containing values - select View Code and paste above into resulting window.
    Macros must be enabled and in 2007 and beyond file must be saved in Macro compliant type - eg .xlsm

    (obviously the above does not take into account the possibility that a changed cell already has the prefix applied)

  7. #7
    Registered User
    Join Date
    02-22-2010
    Location
    croatia
    MS-Off Ver
    Excel 2007
    Posts
    15

    Re: Add prefix depending what is entered in cell

    thanks! this will help.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1