+ Reply to Thread
Results 1 to 15 of 15

Adding drop down List to Cell

  1. #1
    Registered User
    Join Date
    02-20-2012
    Location
    Austin
    MS-Off Ver
    Excel 2010
    Posts
    28

    Adding drop down List to Cell

    I have the following code below but I can not seem to get the syntax correct with it. I am wanting the program to check when cells A1:A5 is empty. If the range is not empty then take the one cells to the right using the "offset(0,1)" and put a dropdown list titled "Categories" in it. Below is what I have this far.

    Code:

    Sub Add_Drop_Down_Menu_Cell()
    Dim Cell As Range
    Dim Rng As Range
    Set Rng = Sheets("Sheet1").Range("A1:A5").Validation
    For Each Cell In Rng
    If Cell.Value <> Empty Then
    Cell.Offset(0, 1).Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, _
    *Formula1:="=Categories"*
    .IgnoreBlank = True
    .InCellDropdown = True
    If Answer = vbCancel Then Exit Sub
    End If
    Next Cell
    End Sub

  2. #2
    Forum Expert Fotis1991's Avatar
    Join Date
    10-11-2011
    Location
    Athens(The homeland of the Democracy!). Greece
    MS-Off Ver
    Excel 1997!&2003 & 2007&2010
    Posts
    13,744

    Re: Adding drop down List to Cell

    Rule #3, of the forum.

    3. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Select your code and click the [#] button at the top of the post window (if you are editing an existing post, press Go Advanced to see the [#] button). The result will appear like this in the post window:
    Regards

    Fotis.

    -This is my Greek whisper to Europe.

    --Remember, saying thanks only takes a second or two. Click the little star * below, to give some Rep if you think an answer deserves it.

    Advanced Excel Techniques: http://excelxor.com/

    --KISS(Keep it simple Stupid)

    --Bring them back.

    ---See about Acropolis of Athens.

    --Visit Greece.

  3. #3
    Registered User
    Join Date
    02-20-2012
    Location
    Austin
    MS-Off Ver
    Excel 2010
    Posts
    28

    Re: Adding drop down List to Cell

    Sorry about that.

    I have the following code below but I can not seem to get the syntax correct with it. I am wanting the program to check when cells A1:A5 is empty. If the range is not empty then take the one cells to the right using the "offset(0,1)" and put a dropdown list titled "Categories" in it. Below is what I have this far.


    Please Login or Register  to view this content.

  4. #4
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Adding drop down List to Cell

    Hello nalgene5622,

    See new attached WorkBook.

    Hope it helps!
    Attached Files Attached Files
    Last edited by Winon; 03-19-2012 at 02:43 AM. Reason: Presented wrong WorkBook
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  5. #5
    Registered User
    Join Date
    02-20-2012
    Location
    Austin
    MS-Off Ver
    Excel 2010
    Posts
    28

    Re: Adding drop down List to Cell

    I am using the function "Private Sub Worksheet_Change(ByVal Target As Range)" to call a macro that simply adds a dropdown list to right of the "Cell". Below is the first part of the macro using the Private Sub Work_Sheet and then the call of the macro "Add_Drop Down_Menu_Cell

    Please Login or Register  to view this content.
    Now lets say cell A3 has a value in it then using the below macro to put a dropdown list in cell B3. There might be a more efficient way to do this but I am new to using macros. Right now I get an error message Compile Error: Method or data member not found. I also wonder if there is a way to delete the cell contents in Range (B1:B5) if the range in A1:A5 is empty.

    Please Login or Register  to view this content.

  6. #6
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Adding drop down List to Cell

    Hello nalgene5622,

    You gave me absolutely no feedback on the first WorkBook I had sent you as in Post No.4.

    You now say that you want to use the Worksheet_Change_Event:

    I have changed The WorkBook, and it will now do exactly what you need, and more
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    02-20-2012
    Location
    Austin
    MS-Off Ver
    Excel 2010
    Posts
    28

    Re: Adding drop down List to Cell

    Winon,

    Thanks for the help on this I greatly appreciate it. Just a quick question how do you get the message box you post to the show up? The code is attached below. Have a great day.

    Please Login or Register  to view this content.

  8. #8
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Adding drop down List to Cell

    Hello nalgene5622,

    You are welcome!,

    Thank you for your response,

    Re:
    how do you get the message box you post to the show up?
    It is merely elaborating on the capabilities on what can be done with VBA. Nothing to do with your "problem".

    If you select another sheet and then go back to sheet1, the Sheet1, Sub Worksheet_Activate, "triggers" the message box to appear.

    Hope that helps.

    Also see the notes "Please consider" at the bottom of this Post

  9. #9
    Registered User
    Join Date
    02-20-2012
    Location
    Austin
    MS-Off Ver
    Excel 2010
    Posts
    28

    Re: Adding drop down List to Cell

    Dear Winon,

    I am eventually going to have that code look over the Range C1:C500. I tested the code from C1:C500 and it seems to take about 30 seconds. Is there a way either in the Worksheet_Change or in the Macro itself to get the code to only look at the value that has recently changed. Meaning lets C5 changes, which triggers the Macro to run but the Macro looks over the entire range from C1:C500. Is there a way to just get the Worksheet_Change or Macro to run only over C5 (i.e. the cell that changes). I tried in the Macro changing it to ActiveCell but it does not work. If I change C5 and hit ENTER the activecell goes to C6 and the macro adds a dropdown list to C6. Similarly if I change C5 and hit the right arrow buttom it adds a dropdown list to E5. Any thoughts on this? Below is what I got from you and I added some edits to it.

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

  10. #10
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: Adding drop down List to Cell

    Pl try this code.It gives drop down list (=Accounts) after making some entry in the cells C1:C500 in the changed cell.
    If value is entered in C5 drop down appears in C5.
    Any clarifications welcome.

    Please Login or Register  to view this content.

  11. #11
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Adding drop down List to Cell

    Hello nalgene5622,

    To speed up the process add these two lines in Red:

    Please Login or Register  to view this content.
    I have tested it on 700 Rows, and it takes about 1 or 2 seconds!
    Last edited by Winon; 03-21-2012 at 12:46 PM. Reason: spelling mistake

  12. #12
    Registered User
    Join Date
    02-20-2012
    Location
    Austin
    MS-Off Ver
    Excel 2010
    Posts
    28

    Re: Adding drop down List to Cell

    kvsrinivasamurthy,

    That code worked great and fast. Just as a side note is there a way to write the code where it calls a macro under the Private Sub then actually runs the code instead of a long Private Sub code. I am eventually going to have several codes that look like that the one you posted and for organization it would be easier to breakout the codes into different macros. Something along the lines of the codes below. Thanks for the help.

    Please Login or Register  to view this content.
    Then

    Please Login or Register  to view this content.

  13. #13
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Adding drop down List to Cell

    Hello nalgene5622,

    Try this:

    Please Login or Register  to view this content.
    and

    Please Login or Register  to view this content.

  14. #14
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: Adding drop down List to Cell

    Pl try these two codes, to have separate macro.A spare cell DA1 is used.
    Clarifications welcome.

    Code1 for worksheet


    Please Login or Register  to view this content.
    Code2 for Macro

    Please Login or Register  to view this content.

  15. #15
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Adding drop down List to Cell

    It would be simpler to adjust your second macro to accept an incoming parameter so you could tell it what "cell" to run the macro against. Then when you CALL that macro, you send it that parameter at the same time:

    Please Login or Register  to view this content.



    Further, this code so far only works if the user never edits more than one cell at a time. You could make the macro work on ALL the cells in the target range even if you changed a bunch at once by adding a loop:

    Please Login or Register  to view this content.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

+ 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