• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Error in VBA Creating Multi-Select Drop Down List|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / Error in VBA Creating Multi-Select Drop Down List|VBA & Macros|Excel Forum|My Online Training Hub

vba course banner

Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosError in VBA Creating Multi-Select …
sp_PrintTopic sp_TopicIcon
Error in VBA Creating Multi-Select Drop Down List
Avatar
Jamie Eagle

New Member
Members
Level 0
Forum Posts: 2
Member Since:
September 9, 2020
sp_UserOfflineSmall Offline
1
September 9, 2020 - 10:53 am
sp_Permalink sp_Print

Hello Philip,

I too am trying to create a multi-select drop down using the data validation list

leveraging your example, I am unable to create the multiple selections - i have a new (enterprise) version of excel that is listed as 365

In the debugger, the "If Not Intersect..." is displaying OldVal as "" instead of the first choice in the drop down and when i try to add another it replaces not adds.

Separately, i had my drop downs on a separate tab but that was generating errors so i moved them back to the primary sheet.

Any help would be greatly appreciated

Thanks in advance,

Jamie

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1529
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
2
September 9, 2020 - 11:58 am
sp_Permalink sp_Print

Hi Jamie,

Please supply your workbook, impossible to debug without it!

Phil

Avatar
Jamie Eagle

New Member
Members
Level 0
Forum Posts: 2
Member Since:
September 9, 2020
sp_UserOfflineSmall Offline
3
September 9, 2020 - 12:11 pm
sp_Permalink sp_Print

Unfortunately, I can not upload the spreadsheet due to privacy concerns

Any other ideas?

 

Private Sub Worksheet_Change(ByVal Target As Range)

' To allow multiple selections in a Drop Down List in Excel (without repetition)

Dim OldVal As String
Dim NewVal As String

'If more than 1 cell is being changed
'If Target.Count > 1 Then Exit Sub

If Not Intersect(Target, ActiveSheet.Range("DataC4")) Is Nothing Then

'Turn off events so our changes don't Trigger this event again
Application.EnableEvents = False

NewVal = Target.Value

'If there's nothing to undo this will cause an error
'On Error Resume Next
Application.Undo
'On Error GoTo 0

OldVal = Target.Value

If OldVal = "" Then

Target.Value = NewVal

Else
'Delete cell contents
If NewVal = "" Then

Target.Value = ""

Else
'This IF prevents the same value appearing in the cell multiple times
'If you are happy to have the same value multiple time then remove this if
'If InStr(Target.Value, NewVal) = 0 Then

Target.Value = OldVal & "," & NewVal

End If

End If
Application.EnableEvents = True

Else

Debug.Print "nothing"

Exit Sub

End If

End Sub

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1529
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
4
September 9, 2020 - 1:42 pm
sp_Permalink sp_Print

Hi Jamie,

That code as you've pasted it works if there is a named range called DataC4 on the ActiveSheet - see attached file.

The code will allow the same value from the DV list to be entered more than once because you've commented out the IF statement that prevented this from happening.

You state that "If Not Intersect..." is displaying OldVal as "" instead of the first choice in the drop down but the If Not Intersect line doesn't refer to OldVal so I don't understand what you mean

If Not Intersect(Target, ActiveSheet.Range("DataC4")) Is Nothing Then

Can you please explain the steps you go through to create the error with the code above and also tell me the actual error message (if there is one?).

Regards

Phil

Avatar
Oscar Chung

New Member
Members
Level 0
Forum Posts: 1
Member Since:
September 17, 2020
sp_UserOfflineSmall Offline
5
September 17, 2020 - 1:16 am
sp_Permalink sp_Print

How would I add multiple ranges in a spreadsheet for "DV_Range" and "DV_Range2"?

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1529
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
6
September 17, 2020 - 9:37 am
sp_Permalink sp_Print

Hi Oscar,

Just change the IF statement in the Worksheet_Change event

If (Not Intersect(Target, ActiveSheet.Range("DV_Range")) Is Nothing) Or (Not Intersect(Target, ActiveSheet.Range("DV_Range2")) Is Nothing) Then

Regards

Phil

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Kumud Patel, Bright Asamoah
Guest(s) 8
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 873
Purfleet: 414
Frans Visser: 346
David_Ng: 306
lea cohen: 222
Jessica Stewart: 218
A.Maurizio: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
Gilbert Lemek
Ashleigh Farquharson
Jayz Luu
Fred Smith
Charles DeGraffenreaid
Cathi Giard
Sarah Young
Henry Delgado
Alita Nieuwoudt
KL KOH
Forum Stats:
Groups: 3
Forums: 24
Topics: 6360
Posts: 27812

 

Member Stats:
Guest Posters: 49
Members: 32340
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.