+ Reply to Thread
Results 1 to 6 of 6

Case statement code please

Hybrid View

  1. #1
    Registered User
    Join Date
    01-02-2013
    Location
    Philippines
    MS-Off Ver
    Excel 2003
    Posts
    22

    Case statement code please

    Hi! I need help on this. Column A cells has 7 default entries AA, BB,CC,DD,EE,FF,GG which I made it as a list. By default these 7 entries represents corresponding numbers AA=5, BB=7, CC=15, DD=15, EE=15, FF=30, GG=30. Is it possible to have it on a case statement? Thank you!

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Case statement code please

    attach please a sample file ad explain better your goal
    If solved remember to mark Thread as solved

  3. #3
    Registered User
    Join Date
    01-02-2013
    Location
    Philippines
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: Case statement code please

    Oh sorry, accidentally press the post button.

    Column A cells has 7 default entries AA, BB,CC,DD,EE,FF,GG which I made it as a list. By default these 7 entries represents corresponding numbers AA=5, BB=7, CC=15, DD=15, EE=15, FF=30, GG=30. What I want is once I select "EE" in column A2 there's should be a return value of 15 in B2 and so on..

    Thanks!

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Case statement code please

    Hi, jayce_sos,

    you could add the line to each Case-statement or use
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim lngNumber As Long
    If Target.Count > 1 Then Exit Sub
    If Target.Column = 1 Then
      Select Case UCase(Target.Value)
        Case "AA"
          lngNumber = 5
        Case "BB"
          lngNumber = 7
        Case "CC", "DD", "EE"
          lngNumber = 15
        Case "FF", "GG"
          lngNumber = 30
        Case Else
          lngNumber = 0
      End Select
    End If
    
    If lngNumber > 0 Then
      Application.EnableEvents = False
      Target.Offset(0, 1).Value = lngNumber
      Application.EnableEvents = True
    End If
    
    End Sub
    This will not check for any present value in the next column but write the number into that cell.

    BTW: you should give this thread a more meaningful title according to Forum Rule #1.

    CIao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  5. #5
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Case statement code please

    Hi, jayce_sos,

    you would need to tell us what should be done with the value you get back by checking.

    Code goes behind Sheet, right-click on the worksheet tab and paste code into code-window:
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim lngNumber As Long
    If Target.Count > 1 Then Exit Sub
    If Target.Column = 1 Then
      Select Case UCase(Target.Value)
        Case "AA"
          lngNumber = 5
        Case "BB"
          lngNumber = 7
        Case "CC", "DD", "EE"
          lngNumber = 15
        Case "FF", "GG"
          lngNumber = 30
        Case Else
          lngNumber = 0
      End Select
    End If
    If lngNumber > 0 Then MsgBox "Corresponding Number is: " & lngNumber
    End Sub
    Ciao,
    Holger

  6. #6
    Registered User
    Join Date
    01-02-2013
    Location
    Philippines
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: Case statement code please

    OMG you got it Holger! THANK YOU!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Disregard case in VBA code. (UCase, LCase, Select Case)
    By Orestees in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 09-07-2012, 12:12 PM
  2. Select Case code not returning correct statement
    By BCITgirl in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-19-2010, 01:15 AM
  3. Color code cell with case statement and datestamp cell(s) to the left
    By garricko in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-26-2010, 08:37 PM
  4. [SOLVED] Utilize a Select Case Statement in Target Intersect Statement
    By max57 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-29-2009, 08:55 PM
  5. Switch(case) statement(enhanced if statement)
    By zapopaul in forum Excel General
    Replies: 6
    Last Post: 10-24-2008, 06:53 PM

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