Results 1 to 8 of 8

Thread: Easier way to write this

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Long Island, NY.
    Posts
    353

    Easier way to write this

    Is there any easier way so next time I don't have to write this??

    VB Code:
    1. Public Function Category(Pool)
    2.  
    3. 'Custom Function to determine ANG functional categories
    4.  
    5. If Pool = "510" Then
    6. Category = "Suffolk Environmental Payroll"
    7.  
    8. ElseIf Pool = "520" Then
    9. Category = "Suffolk Security Police"
    10.  
    11. ElseIf Pool = "530" Then
    12. Category = "Suffolk Fire Department"
    13.  
    14. ElseIf Pool = "550" Then
    15. Category = "Suffolk State Regulars"
    16.  
    17. ElseIf Pool = "556" Then
    18. Category = "Suffolk Real Property Manager"
    19.  
    20. ElseIf Pool = "555" Or Pool = "556" Then
    21. Category = "Suffolk Hourly/Snow Workers"
    22.  
    23. ElseIf Pool = "512" Then
    24. Category = "Stratton Environmental Payroll"
    25.  
    26. ElseIf Pool = "522" Then
    27. Category = "Stratton Security Police"
    28.  
    29. ElseIf Pool = "532" Then
    30. Category = "Stratton Fire Department"
    31.  
    32. ElseIf Pool = "560" Then
    33. Category = "Stratton State Regulars"
    34.  
    35. ElseIf Pool = "567" Then
    36. Category = "Stratton Real Property Manager"
    37.  
    38. ElseIf Pool = "564" Then
    39. Category = "Stratton Hourly/Snow Workers"
    40.  
    41. ElseIf Pool = "514" Then
    42. Category = "Niagara Environmental Payroll"
    43.  
    44. ElseIf Pool = "524" Then
    45. Category = "Niagara Security Police"
    46.  
    47. ElseIf Pool = "570" Then
    48. Category = "Niagara State Regulars"
    49.  
    50. ElseIf Pool = "576" Then
    51. Category = "Niagara Real Property Manager"
    52.  
    53. ElseIf Pool = "574" Then
    54. Category = "Niagara Hourly/Snow Workers"
    55.  
    56. ElseIf Pool = "516" Then
    57. Category = "Syracuse Environmental Payroll"
    58.  
    59. ElseIf Pool = "526" Then
    60. Category = "Syracuse Security Police"
    61.  
    62. ElseIf Pool = "534" Then
    63. Category = "Syracuse Fire Department"
    64.  
    65. ElseIf Pool = "580" Then
    66. Category = "Syracuse State Regulars"
    67.  
    68. ElseIf Pool = "586" Then
    69. Category = "Syracuse Real Property Manager"
    70.  
    71. ElseIf Pool = "584" Or Pool = "585" Then
    72. Category = "Syracuse Hourly/Snow Workers"
    73.  
    74. ElseIf Pool = "518" Then
    75. Category = "Stewart Environmental Payroll"
    76.  
    77. ElseIf Pool = "528" Then
    78. Category = "Stewart Security Police"
    79.  
    80. ElseIf Pool = "536" Then
    81. Category = "Stewart Fire Department"
    82.  
    83. ElseIf Pool = "590" Then
    84. Category = "Stewart State Regulars"
    85.  
    86. ElseIf Pool = "596" Then
    87. Category = "Syracuse Real Property Manager"
    88.  
    89. ElseIf Pool = "594" Or Pool = "595" Then
    90. Category = "Syracuse Hourly/Snow Workers"
    91.  
    92. Else
    93. Category = "Undefined"
    94.  
    95. End If
    96.  
    97. End Function

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Easier way to write this

    You can use a SELECT CASE statement which will greatly simplify this.
    Have a look at my post in the following thread.

    http://www.vbforums.com/showthread.php?t=376545
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Long Island, NY.
    Posts
    353

    Re: Easier way to write this

    But every number gets a different word returned. So how does SELECT CASE simply this??

  4. #4
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Easier way to write this

    Good point

    What app are you using?
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Long Island, NY.
    Posts
    353

    Re: Easier way to write this

    Excel

  6. #6
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Easier way to write this

    OK
    One way to do this would be to add a worksheet and creat a 2 column list with the codes in column "A" and the descriptions in column "B". You could then use the vlookup function to return the correct description.

    (You will need to change the sheet number in the following to reflect the sheet that has your list)
    VB Code:
    1. Function Category(Pool)
    2.     Category = Application.WorksheetFunction.VLookup(Pool, Worksheets(1).Range("$A:$B"), 2, 0)
    3. End Function
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  7. #7
    Addicted Member BobTheBuilder.'s Avatar
    Join Date
    Apr 2005
    Location
    Ohio
    Posts
    149

    Re: Easier way to write this

    Check out this post bye eyeRMonkey. You may be able to adapt this to what you want by creating an enum. looping through it each time, then i = the value you are searching for then just display the name.

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Easier way to write this

    Actually the Select Case does make it shorter, quicker, and easier to read, eg:
    VB Code:
    1. Public Function Category(Pool)
    2.  
    3. 'Custom Function to determine ANG functional categories
    4.  
    5. Select Case Pool
    6. Case "510":        Category = "Suffolk Environmental Payroll"
    7. Case "520":        Category = "Suffolk Security Police"
    8. Case "530":        Category = "Suffolk Fire Department"
    9. Case "550":        Category = "Suffolk State Regulars"
    10. Case "556":        Category = "Suffolk Real Property Manager"
    11. Case "555", "556": Category = "Suffolk Hourly/Snow Workers"
    12. Case "512":        Category = "Stratton Environmental Payroll"
    13. Case "522":        Category = "Stratton Security Police"
    14. ....
    15. Case Else:         Category = "Undefined"
    16. End Select
    17.  
    18. End Function
    By the way, "556" will never be seen as "Suffolk Hourly/Snow Workers", as "556" is the value for "Suffolk Real Property Manager".

    I think the VLookup option would be a good one, but it needs to take these double-values into account too. Would it just mean two lines for "Suffolk Hourly/Snow Workers"?

Posting Permissions

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



Click Here to Expand Forum to Full Width