+ Reply to Thread
Results 1 to 11 of 11

SOLVED Enter number and lookup matched subject

Hybrid View

  1. #1
    Registered User
    Join Date
    06-23-2012
    Location
    chicago, IL
    MS-Off Ver
    Excel 2010
    Posts
    12

    SOLVED Enter number and lookup matched subject

    I have the following scenario and am stuck on how I need to complete my task at hand.

    I have two sheets. The first sheet has three columns. A: is the name of a student B: is the subject C: is the grade
    The Second sheet has two columns A: is a number B: is a subject that corresponds to each Column A number

    So what I want is to be able to type in a number in the first sheet under Column B and have the input be the subject (math, english, ect)

    Not quite sure where to start with this.

    any help will be appreciated!
    Last edited by svayl; 06-26-2012 at 11:43 PM.

  2. #2
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: Don't know where to start!

    See if you can adapt this basic code (no error handling)

    The code *MUST* go into the worksheet module - *NOT* a standard module.
    The code assumes a look up table is kept on another sheet ( in this example sheet2) in the range of A1:Bnnn per your post.

    Sheet references in the code are to sheet *CODE NAMES*, not worksheet (tab) names. Adjust if needed.

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
        Dim lastrow As Long
        Dim rngList As Range
        
        lastrow = Cells(Rows.Count, "A").End(xlUp).Row
        
        Set rngList = Sheet2.Range("A1").CurrentRegion
        
        If Target.Cells.Count > 1 Then Exit Sub
        
        If Not Intersect(Target, Range("B2:B" & lastrow)) Is Nothing Then ' user is in column-B
            If IsNumeric(Target) Then
                Target.Value = Application.WorksheetFunction.VLookup(Target.Value, rngList, 2, False)
            End If
        End If
        
        Set rngList = Nothing
    
    End Sub
    This is not the only approach that can be used, but it is simple and does what you asked for.
    Palmetto

    Do you know . . . ?

    You can leave feedback and add to the reputation of all who contributed a helpful response to your solution by clicking the star icon located at the left in one of their post in this thread.

  3. #3
    Registered User
    Join Date
    06-23-2012
    Location
    chicago, IL
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Enter number and lookup matched subject

    Thank you so much for the code.

    I am very...VERY...new at this so please forgive me for my elementary questions.

    I copied the code and pasted it in under 'this workbook'
    What are the steps that lead to actually testing the code out?

  4. #4
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: Enter number and lookup matched subject

    The code *DOES NOT* go into the the module named "ThisWorkbook" delete the code from that module.

    Assuming you have the VB editor open (Alt + F11 keys)

    Make sure the Project Explorer pane is in view. If not, press Ctrl + R or go to View in the menu and select it.
    Afterwards, look for the sheet name in the project explorer and double click on it to make the code pane active for that worksheet.

    Copy and paste the code the code into the code pane.
    Adjust any sheet or range references as needed.

    As I mentioned, sheet references in my code are to sheet code names

    Close the VB Editor (Alt + Q keys)

    Re-read my previous post in its entirety, especially regarding the look up table.

  5. #5
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Enter number and lookup matched subject

    @svayl

    You get better help if you post an Excel-example of your workbook, without confidential information.
    Notice my main language is not English.

    I appreciate it, if you reply on my solution.

    If you are satisfied with the solution, please mark the question solved.

    You can add reputation by clicking on the star * add reputation.

  6. #6
    Registered User
    Join Date
    06-23-2012
    Location
    chicago, IL
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Enter number and lookup matched subject

    Practice.xlsx

    Ok I attached the file. Basically want to type in a code for a student and have the output be the subject. I tried doing what you told me to do, but it keeps telling me there is an error and also won't let me save the file.

    Ah!

  7. #7
    Forum Contributor
    Join Date
    05-16-2012
    Location
    Latina, ITALY
    MS-Off Ver
    Excel 2010--2016
    Posts
    947

    Re: Enter number and lookup matched subject

    Hello, column B on sheet Jan 2012 I added code menu selection, and column C shows the subject automatically.
    I do not know if that was what you asked
    greetings
    Practice(1).zip

  8. #8
    Registered User
    Join Date
    06-23-2012
    Location
    chicago, IL
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Enter number and lookup matched subject

    Yea that is what I want. How exactly did you do that? Also, do I need to do this manually for each month(sheet)?

  9. #9
    Forum Contributor
    Join Date
    05-16-2012
    Location
    Latina, ITALY
    MS-Off Ver
    Excel 2010--2016
    Posts
    947

    Re: Enter number and lookup matched subject

    Then, it is not difficult, located on B2, and from the menu Data -> validation data, then choose to allow: list then from Origin: =codes!A2:A20. Ok.
    For the other months, copy cell B2 and paste from the Jan2012 sheet in the sheet Feb2012, then drag down.

  10. #10
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: Enter number and lookup matched subject

    Attached is a copy of your workbook with the code added and the sheet code name adjusted to reference the correct sheet containing the subject codes.

    I think you would be much better served by using a *SINGLE* worksheet to contain all of the data and include an extra column for the date (month year). Assuming you will be entering grades or other data you may want to report on, a single sheet will make this task much easier. In addition, the code only needs to go into the one sheet module, otherwise you will have to add it to all sheet modules as you add new sheets.

    I've add the code to the two worksheets in your sample workbook.

    In Excel 2007+, when you have macros you *MUST* save the file as a .xlsm file type, otherwise Excel will delete the code from the workbook.

    Excel Help_840513.xlsm

  11. #11
    Registered User
    Join Date
    06-23-2012
    Location
    chicago, IL
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Enter number and lookup matched subject

    Ok awesome...I got it to work. Thank you Thank you Thank you!
    I am going to have more data and that is why i wanted to have separate sheets. If all it takes is adding it to each new sheet, then I don't really mind.

    thank you again!

+ 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