You are here:

Excel/VBA to create Drop-Down List on multiple sheets

Advertisement


Question
QUESTION: Hi Tom,

Using Excel 2003
I created a macro that will create a drop-down list in column J using the data I put in cells X6550 to X65503 and it works fine on any single sheet.  Now I’m trying to have it create the drop-down list on all sheets (excluding two specified sheets) but I get this Run-time error
Method ‘Add’ of object ‘Validation’ failed

Appreciate any assistance you can provide.
Thanks, Anthony

Sub CreateMyList()
      Dim ws As Worksheet
       For Each ws In ActiveWorkbook.Worksheets
         
' Exclude these two sheets
         If ws.Name <> "Actions" Or ws.Name <> "Daily_Rejections" Then
         
   Range("X65500") = "CNL"
   Range("X65501") = "NVT"
   Range("X65502") = "Card"
   Range("X65503") = "ERA"
   
    With ws.Range("J2:J65536").Validation
       .Delete
       .Add Type:=xlValidateList, _
       AlertStyle:=xlValidAlertStop, _
       Operator:=xlBetween, _
       Formula1:="=$X$65500:$X$65503"
       .IgnoreBlank = True
       .InCellDropdown = True
       .InputTitle = ""
       .ErrorTitle = ""
       .InputMessage = ""
       .ErrorMessage = ""
       .ShowInput = True
       .ShowError = True
   End With
         
         End If
       Next ws
End Sub

ANSWER: Anthony,

I modified the code and this code worked fine for me:

Sub CreateMyList()
     Dim ws As Worksheet
      For Each ws In ActiveWorkbook.Worksheets
        
' Exclude these two sheets
        If ws.Name <> "Actions" Or ws.Name <> "Daily_Rejections" Then
        
  ws.Range("X65500") = "CNL"
  ws.Range("X65501") = "NVT"
  ws.Range("X65502") = "Card"
  ws.Range("X65503") = "ERA"
  ws.Activate
  
   With ws.Range("J2:J65536").Validation
      .Delete
      .Add Type:=xlValidateList, _
      AlertStyle:=xlValidAlertStop, _
      Operator:=xlBetween, _
      Formula1:="=$X$65500:$X$65503"
      .IgnoreBlank = True
      .InCellDropdown = True
      .InputTitle = ""
      .ErrorTitle = ""
      .InputMessage = ""
      .ErrorMessage = ""
      .ShowInput = True
      .ShowError = True
  End With
        
        End If
      Next ws
End Sub


However, you will have to see if it is consistent with what you wanted to do.  


--
Regards,
Tom Ogilvy


---------- FOLLOW-UP ----------

QUESTION: Tom, it wasn’t working at first because one of the two sheets I was trying to skip was protected.  I took a shot and changed the line below.  I changed the OR Operator to an AND Operator.
It works perfectly now.  Please forgive my ignorance but honestly I’m not exactly sure why this change made a difference.  Can you tell me why?
Thanks, Anthony
' Exclude these two sheets
CHANGED  If ws.Name <> "Actions" Or ws.Name <> "Daily_Rejections" Then

TO       If ws.Name <> "Actions" AND ws.Name <> "Daily_Rejections" Then

Answer
Anthony,

Yes - I didn't look at that statement because that wasn't where your question was.

with the OR conjunctive, the statement will always be true.  If the sheet is named "Actions", then it will not be "Daily_Rejections" and vice versa.  when you put in the "AND" conjunctive, then both contidions have to be true.  The sheet isn't named "Actions" and isn't named "Daily_Rejections".  sorry I didn't pick that up for you, but since my testing workbook didn't contain a sheet with either name, it wasn't of concern to me and you didn't cite that the if statement was problematic.  

--
Regards,
Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.