+ Reply to Thread
Results 1 to 4 of 4

Excel Macro If with text

Hybrid View

  1. #1
    Registered User
    Join Date
    02-26-2009
    Location
    Maastricht, Netherlands
    MS-Off Ver
    Excel 2007
    Posts
    1

    Excel Macro If with text

    I have a 100 different catagories in one collumn and I want an Excel macro to generate a abbreviation for each different catagorie in another collumn in order to have a better overview.

    I am new to the whole macro writing thing but found a way to do it using this code:

    Sub catagories()
    '
    'catagories Macro
    ' Macro recorded
    '
    
    Function CalcValue(pVal As String) As Long
    
    If pVal = "CATAGORY1" Then
    CalcValue = 1
    
    ElseIf pVal = "CATAGORY2" Then
    CalcValue = 2
    
    ElseIf pVal = "CATAGORY3" Then
    CalcValue = 3
    
    ElseIf pVal = "CATAGORY4" Then
    CalcValue = 4
    
    ElseIf pVal = "CATAGORY5" Then
    CalcValue = 5
    
    ElseIf pVal = "CATAGORY6" Then
    CalcValue = 6
    
    ElseIf pVal = "CATAGORY7" Then
    CalcValue = 7
    
    ElseIf pVal = "CATAGORY8" Then
    CalcValue = 8
    
    ElseIf pVal = "CATAGORY9" Then
    CalcValue = 9
    
    ElseIf pVal = "CATAGORY10" Then
    CalcValue = 10
    
    Else
    CalcValue = 0
    End If
    
    End Function
    This works, but instead of numbers I want tekst (exemple: when it's Catagory 1 the "Callcvalue should be "HS" instead of "1")

    I am probably making a very basic mistake here but I have tried everything I could think off and cannot find a way to fix it.

    Can you please help me???
    Last edited by royUK; 02-26-2009 at 07:57 AM. Reason: add code tags

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Excel Macro If with text

    please take a few minutes to read the Forum Rules, you will see then that your Thread Title & lack of Code tags is breaking the rules.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

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

    Re: Excel Macro If with text

    Please place your VBA within CODE Tags... edit your last post, highlight the VBA and press the # icon -- at which point we will be permitted to assist you.

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Excel Macro If with text

    Change the code like this
    If pVal = "CATAGORY1" Then
    CalcValue = "HS"

+ 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