+ Reply to Thread
Results 1 to 15 of 15

Adding drop down List to Cell

Hybrid View

nalgene5622 Adding drop down List to Cell 03-18-2012, 06:01 PM
Fotis1991 Re: Adding drop down List to... 03-18-2012, 06:15 PM
nalgene5622 Re: Adding drop down List to... 03-18-2012, 06:56 PM
Winon Re: Adding drop down List to... 03-19-2012, 02:08 AM
nalgene5622 Re: Adding drop down List to... 03-19-2012, 09:03 PM
Winon Re: Adding drop down List to... 03-20-2012, 02:01 AM
nalgene5622 Re: Adding drop down List to... 03-20-2012, 09:41 AM
Winon Re: Adding drop down List to... 03-20-2012, 11:08 AM
nalgene5622 Re: Adding drop down List to... 03-20-2012, 09:03 PM
Winon Re: Adding drop down List to... 03-21-2012, 02:16 AM
kvsrinivasamurthy Re: Adding drop down List to... 03-21-2012, 01:33 AM
nalgene5622 Re: Adding drop down List to... 03-22-2012, 09:30 AM
Winon Re: Adding drop down List to... 03-22-2012, 04:17 PM
kvsrinivasamurthy Re: Adding drop down List to... 03-24-2012, 03:17 AM
JBeaucaire Re: Adding drop down List to... 03-24-2012, 10:58 AM
  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.


    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

  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

    If Intersect(Target, Range("A1:A5")) Is Nothing Then Exit Sub
       Call Add_Drop_Down_Menu_Cell
    End Sub
    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.

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

  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.

    Private Sub Worksheet_Activate()
    MsgBox "Hello", vbOKOnly, ("Greetings")
    End Sub

  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.

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Intersect(Target, Range("C8:C500")) Is Nothing Then Exit Sub
       Call Add_DropDown_In_ColumnD
    End Sub
    Sub Add_DropDown_In_ColumnD()
        Dim Cell As Range
        Dim Rng As Range
              Set Rng = ActiveSheet.Range("C8:C500")
              For Each Cell In Rng
               If Cell.Value <> Empty Then
                   Cell.Offset(0, 1).Interior.ColorIndex = 6
                    With Cell.Offset(0, 1).Validation
                       .Delete
                       .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
                        xlBetween, Formula1:= _
                        "=Accounts"
                        .IgnoreBlank = True
                        .InCellDropdown = True
                        .InputTitle = ""
                        .ErrorTitle = ""
                        .InputMessage = ""
                        .ErrorMessage = ""
                        .ShowInput = True
                        .ShowError = True
      
                    End With
                Else
                    Cell.Offset(0, 1).Clear
                    Cell.Offset(0, 1).Interior.ColorIndex = 2
                    With Cell.Offset(0, 1).Validation
                       .Delete
                    End With
                End If
                Next
    End Sub

  10. #10
    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:

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Not Intersect(Target, Range("A1:A700")) Is Nothing Then 'change range to suit
    
    Application.Calculation = xlCalculationManual
    
    Add_Drop_Down_Menu_Cell
    
    Application.Calculation = xlCalculationAutomatic
    
    End If
    
    End Sub
    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

  11. #11
    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.

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Vfyrng As Range
    
    Set Vfyrng = Range("C1:C500")
    
    If Not Intersect(Vfyrng, Target) Is Nothing Then
    
    If Target.Value <> "" Then
        With Target.Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
            xlBetween, Formula1:="=Accounts"
            .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = "Cell Validated"
            .InputMessage = ""
            .ErrorMessage = "Cell validated.Select from dropdown list."
            .ShowInput = True
            .ShowError = True
        End With
    Else
     Target.Validation.Delete
    End If
    
    End If
    
    End Sub

  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.

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Vfyrng As Range
    Set Vfyrng = Range("C1:C500")
    If Not Intersect(Vfyrng, Target) Is Nothing Then
        Call Macro Here
    End If
    End Sub
    Then

    Sub Macro
    If Target.Value <> "" Then
        With Target.Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
            xlBetween, Formula1:="=Accounts"
            .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = "Cell Validated"
            .InputMessage = ""
            .ErrorMessage = "Cell validated.Select from dropdown list."
            .ShowInput = True
            .ShowError = True
        End With
    Else
     Target.Validation.Delete
    End If
    End Sub

  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:

    Option Explicit
    
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Not Intersect(Target, Range("C8:C500")) Is Nothing Then
    Application.Calculation = xlCalculationManual
    Add_Drop_Down_Menu_Cell
    Application.Calculation = xlCalculationAutomatic
    End If
    End Sub
    and

    Option Explicit
    Sub Add_Drop_Down_Menu_Cell()
    Dim Cell As Range
    Dim Rng As Range
    Set Rng = Sheets("Sheet1").Range("C8:C500")
    For Each Cell In Rng
    
    If Cell.Value <> Empty Then
       Cell.Offset(0, 1).Interior.ColorIndex = 6
    
       With Cell.Offset(0, 1).Validation
       
       .Delete
       .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
       xlBetween, Formula1:= _
       "=Accounts"
       .IgnoreBlank = True
       .InCellDropdown = True
       .InputTitle = ""
       .ErrorTitle = ""
       .InputMessage = ""
       .ErrorMessage = ""
       .ShowInput = True
       .ShowError = True
        End With
    Cell.Offset(0, 1).Select
    Else
    Cell.Offset(0, 1).Clear
    Cell.Offset(0, 1).Interior.ColorIndex = 2
    
    End If
    Next
    
    End Sub

  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


    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Vfyrng As Range
    Set Vfyrng = Range("C1:C500")
    
    If Not Intersect(Vfyrng, Target) Is Nothing Then
    [DA1] = Target.Address
    Run "Macro1"
    End If
    End Sub
    Code2 for Macro

    Private Sub Macro1()
    Dim K As String
    K = [DA1]
    If Range(K).Value <> "" Then
        With Range(K).Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
            xlBetween, Formula1:="=Accounts"
            .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = "Cell Validated"
    
            .InputMessage = ""
            .ErrorMessage = "Cell validated.Select from dropdown list."
            .ShowInput = True
            .ShowError = True
        End With
    Else
    Range(K).Validation.Delete
    End If
    
    End Sub

  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:

    Private Sub Worksheet_Change(ByVal Target As Range)
    
        If Not Intersect(Target, Range("C1:C500")) Is Nothing Then
            Call Macro1(Intersect(Target, Range("C1:C500")))
        End If
    
    End Sub
    
    
    Private Sub Macro1(Target As Range)
    
    If Target.Value <> "" Then
        With Target.Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
                Operator:=xlBetween, Formula1:="=Accounts"
            .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = "Cell Validated"
            .InputMessage = ""
            .ErrorMessage = "Cell validated.Select from dropdown list."
            .ShowInput = True
            .ShowError = True
         End With
     Else
         Target.Validation.Delete
    End If
    
    End Sub



    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:

    Private Sub Worksheet_Change(ByVal Target As Range)
    
        If Not Intersect(Target, Range("C1:C500")) Is Nothing Then
            Call AddValidation(Intersect(Target, Range("C1:C500")))
        End If
    
    End Sub
    
    
    Private Sub AddValidation(Target As Range)
    Dim Cell As Range
    
    For Each Cell In Target
        If Cell.Value <> "" Then
            With Cell.Validation
                .Delete
                .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
                    Operator:=xlBetween, Formula1:="=Accounts"
                .IgnoreBlank = True
                .InCellDropdown = True
                .InputTitle = ""
                .ErrorTitle = "Cell Validated"
                .InputMessage = ""
                .ErrorMessage = "Cell validated.Select from dropdown list."
                .ShowInput = True
                .ShowError = True
            End With
        Else
             Cell.Validation.Delete
        End If
    Next Cell
    
    End Sub
    _________________
    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