+ Reply to Thread
Results 1 to 11 of 11

Data validation multiple selection VBA code - adaptation to whole column - URGENT HELP!

Hybrid View

  1. #1
    Registered User
    Join Date
    02-15-2016
    Location
    London, UK
    MS-Off Ver
    14
    Posts
    32

    Unhappy Data validation multiple selection VBA code - adaptation to whole column - URGENT HELP!

    Could anyone tell me how can I adapt below VBA code to apply to selection in a column? So far it only applies to one cell (E40) and I need the multiple selection to apply to cells from E40 to E350.

    Private Sub Worksheet_Change(ByVal Target As Range)

 ***Dim oldVal As String
 ***Dim newVal As String
 ***
 ***If Target.Address(0, 0) <> "E40" Then Exit Sub

 ***On Error GoTo ReEnable
 ***Application.EnableEvents = False
 ***newVal = Target.Value
 ***Application.Undo
 ***oldVal = Target.Value
 ***Target.Value = newVal

 ***If oldVal <> "" And newVal <> "" Then
 *******Target.Value = oldVal & ", " & newVal
 ***End If
ReEnable:
 ***Application.EnableEvents = True
 ***
End Sub

  2. #2
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Data validation multiple selection VBA code - adaptation to whole column - URGENT HELP

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here



    Also, not sure why each of you lines starts with "***", but that can go away, too.

    Once you make the changes, I'll happily post a solution.
    I'm interested in starting a career working with VBA, if anyone knows of any opportunities!

  3. #3
    Registered User
    Join Date
    02-15-2016
    Location
    London, UK
    MS-Off Ver
    14
    Posts
    32

    Re: Data validation multiple selection VBA code - adaptation to whole column - URGENT HELP

    sorry about this, I had no idea how to post the code correctly.

  4. #4
    Registered User
    Join Date
    02-15-2016
    Location
    London, UK
    MS-Off Ver
    14
    Posts
    32

    Re: Data validation multiple selection VBA code - adaptation to whole column - URGENT HELP

    I have found this one and used it instead and it works fine. But I have one more issue. I would like to apply this same code to another set of cells/column where I already have INDIRECT formula. Would it work?

    Private Sub Worksheet_Change(ByVal Target As Range)
    ' Developed by Contextures Inc.
    ' www.contextures.com
    Dim rngDV As Range
    Dim oldVal As String
    Dim newVal As String
    If Target.Count > 1 Then GoTo exitHandler
    
    On Error Resume Next
    Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
    On Error GoTo exitHandler
    
    If rngDV Is Nothing Then GoTo exitHandler
    
    If Intersect(Target, rngDV) Is Nothing Then
       'do nothing
    Else
      Application.EnableEvents = False
      newVal = Target.Value
      Application.Undo
      oldVal = Target.Value
      Target.Value = newVal
      If Target.Column = 3 Then
        If oldVal = "" Then
          'do nothing
          Else
          If newVal = "" Then
          'do nothing
          Else
          Target.Value = oldVal _
            & ", " & newVal
    '      NOTE: you can use a line break,
    '      instead of a comma
    '      Target.Value = oldVal _
    '        & Chr(10) & newVal
          End If
        End If
      End If
    End If
    
    exitHandler:
      Application.EnableEvents = True
    End Sub

  5. #5
    Registered User
    Join Date
    02-15-2016
    Location
    London, UK
    MS-Off Ver
    14
    Posts
    32

    Re: Data validation multiple selection VBA code - adaptation to whole column - URGENT HELP

    
    Private Sub Worksheet_Change(ByVal Target As Range)

 ***
        Dim oldVal As String
 ***
        Dim newVal As String
 ***
 ***
    If Target.Address(0, 0) <> "E8" Then Exit Sub

 ***
    
     ***If oldVal <> "" And newVal <> "" Then
 *******
             Target.Value = oldVal & ", " & newVal
 ***
        End If

    ReEnable:
 ***
       Application.EnableEvents = True
 ***

    
    End Sub
    **
    Last edited by JuNova; 02-15-2016 at 03:07 PM.

  6. #6
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Data validation multiple selection VBA code - adaptation to whole column - URGENT HELP

    Ok, you're trying to comply and you're new, so I'll help you out.

    Here's the original:

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim oldVal As String
    Dim newVal As String
    If Target.Address(0, 0) <> "E40" Then Exit Sub
    On Error GoTo ReEnable
    Application.EnableEvents = False
    newVal = Target.Value
    Application.Undo 
    oldVal = Target.Value
    Target.Value = newVal
    If oldVal <> "" And newVal <> "" Then
    Target.Value = oldVal & ", " & newVal
    End If
    ReEnable: Application.EnableEvents = True
    End Sub
    And here's the code modified to your request:

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim oldVal As String
    Dim newVal As String
    If Not Intersect(Range("E40:E350"), Target) Is Nothing Then
        On Error GoTo ReEnable
        Application.EnableEvents = False
        newVal = Target.Value
        Application.Undo
        oldVal = Target.Value
        Target.Value = newVal
        If oldVal <> "" And newVal <> "" Then
            Target.Value = oldVal & ", " & newVal
        End If
    End If
    ReEnable: Application.EnableEvents = True
    End Sub

  7. #7
    Registered User
    Join Date
    02-15-2016
    Location
    London, UK
    MS-Off Ver
    14
    Posts
    32

    Re: Data validation multiple selection VBA code - adaptation to whole column - URGENT HELP

    Very new! Just opened my account 1h ago.

    Fantastic! Thank you so much!!!

    Will that work also on my cells with INDIRECT formula?

  8. #8
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Data validation multiple selection VBA code - adaptation to whole column - URGENT HELP

    Not sure what you mean by "work". The code will run on any cell you list in the Intersect Range, which is currently "E40:E350". You could easily do:

    Range("E40:F350")
    or

    Range("E40:E350, H10:H100")
    and since you're applying .Values, any cell in those ranges that have formulas in them will be overwritten to contain only the values (end results) that the formulas calculated. the formula would be gone.

    If that's not what you want, please post an example workbook that represents your data set along with a specific example of what you want to happen and what the end result would be.

  9. #9
    Registered User
    Join Date
    02-15-2016
    Location
    London, UK
    MS-Off Ver
    14
    Posts
    32

    Re: Data validation multiple selection VBA code - adaptation to whole column - URGENT HELP

    I have applied your formula to column D.
    Only when REASON is selected in column A for Context the values in Column B can be selected ( INDIRECT formula)
    I would like to be abel to do the same selection separated by comma like in column D in column B without changing/overriding the INDIRECT formula in column B.
    Attached Files Attached Files
    Last edited by JuNova; 02-15-2016 at 04:11 PM.

  10. #10
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Data validation multiple selection VBA code - adaptation to whole column - URGENT HELP

    Not sure that can be done, or at least I don't know how to do it. In VBA, as soon as you manipulate the .Value of a cell (as your code is doing), the formula goes away. You can't have both a .Formula and a .Value determining the contents of a cell. Either a .Formula dictates what you see (the .Value), or the cell has a direct .Value with no formula.

    You can, however, ditch the formulas and just do everything in VBA instead. Let me know if that's a feasible path for you.

    If not, and you want to explore the formula approach further, I suggest starting a new thread with the title "can I manipulate the .Value of a cell while maintaining the .Formula?".

    Explain a bit, link to this post, and in this post, include a link to your new thread. Like I said, I'm really not sure how what you're asking is possible, but I am by no means the most experienced user here.

  11. #11
    Registered User
    Join Date
    02-15-2016
    Location
    London, UK
    MS-Off Ver
    14
    Posts
    32

    Re: Data validation multiple selection VBA code - adaptation to whole column - URGENT HELP

    I guess you can't have everything What I have is enough.

    Thank you so much for helping!

+ 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. Mac 2011 Data validation with multiple list selection
    By rickycr in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-06-2015, 08:55 PM
  2. [SOLVED] Multiple selection in Data validation list
    By anchuri_chaitanya in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-21-2013, 08:56 AM
  3. Pull Data from Multiple Columns Based On Data Validation List Selection
    By CHRISOK in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-13-2013, 11:41 PM
  4. Data validation multiple selection
    By tetiva in forum Excel General
    Replies: 3
    Last Post: 02-16-2012, 08:04 PM
  5. Multiple Selection Data Validation and Placement of Selection.
    By LCS in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 02-08-2012, 08:48 AM
  6. Add multiple named ranges in data validation for selection
    By jeffreybrown in forum Excel General
    Replies: 5
    Last Post: 01-21-2012, 11:49 AM
  7. Adaptation of code to delete data
    By Dave32 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-20-2008, 06:59 PM

Tags for this Thread

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