473,396 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Create Drop-down List in Excel with Python

34
Hello All,

I wrote Python code to write data to an Excel worksheet1, and also add 5 status types to worksheet2 to be used to make a dropdown list for one column in worksheet1. Could someone help me with the code to do this in Python? I recorded the macro >>

Expand|Select|Wrap|Line Numbers
  1. Sub Macro2()
  2. '
  3. ' Macro2 Macro
  4. '
  5.  
  6. '
  7.     Sheets("Sheet2").Select
  8.     Range("A1:A5").Select
  9.     Range("A5").Activate
  10.     ActiveWorkbook.Names.Add Name:="ValidStatus", RefersToR1C1:= _
  11.         "=Sheet2!R1C1:R5C1"
  12.     Sheets("Sheet1").Select
  13.     Columns("I:I").Select
  14.     With Selection.Validation
  15.         .Delete
  16.         .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
  17.         xlBetween, Formula1:="=ValidStatus"
  18.         .IgnoreBlank = True
  19.         .InCellDropdown = True
  20.         .InputTitle = ""
  21.         .ErrorTitle = ""
  22.         .InputMessage = ""
  23.         .ErrorMessage = ""
  24.         .ShowInput = True
  25.         .ShowError = True
  26.     End With
  27. End Sub
  28.  

I have tried a few things (like below), but am stumped and can't find much documentation out there. I need to declare the name of the 5 cells on worksheet2 as 'ValidStatus' and get column I on worksheet1 to be a list with ValidStatus as the choices.

Expand|Select|Wrap|Line Numbers
  1. ws1 = wb.Worksheets(1)
  2. ws2 = wb.Worksheets(2)
  3.  
  4. ws2.Cells(1,1).Value = "GREEN"
  5. ws2.Cells(2,1).Value = "YELLOW"
  6. ws2.Cells(3,1).Value = "RED"
  7. ws2.Cells(4,1).Value = "WHITE"
  8. ws2.Cells(5,1).Value = "NOT SURE"
  9.  
  10. ws1.Columns("I").Validation.Add(Type = "xlValidateList", xlValidAlertStop, xlBetween, "=ValidStatus")
  11.  
Any help would be greatly appreciated. And, if anyone could refer me to some good documentation on Python and Excel, well that would be great!

Thanks,

jenn
Mar 3 '08 #1
2 14502
jld730
34
Add-on -->

I finally (been looking/asking for months) ran across a method to get at the value of Excel constants for use in Pyhton scripting.

From http://aspn.activestate.com/ASPN/Coo.../Recipe/528870 ...
To get the value of Excel Constants such as xlEdgeLeft (7) or xlThin (2)
type e.g. Debug.Print xlEdgeLeft in the Immediate window of the VBA editor and press enter.
This helps for general knowledge, but still have not gotten my script to work...
Mar 3 '08 #2
jld730
34
I figured it out myself after many hours!!! Solution is:

Expand|Select|Wrap|Line Numbers
  1. ws1 = wb.Worksheets(1)
  2. ws2 = wb.Worksheets(2)
  3.  
  4. ws2.Cells(1,1).Value = "GREEN"
  5. ws2.Cells(2,1).Value = "YELLOW"
  6. ws2.Cells(3,1).Value = "RED"
  7. ws2.Cells(4,1).Value = "WHITE"
  8. ws2.Cells(5,1).Value = "NOT SURE"
  9.  
  10. wb.Names.Add("ValidStatus", "=vl!R1C1:R5C1")
  11. ws1.Columns("I").Validation.Add(3, 1, 1, "=ValidStatus") ##Type = xlValidateList/3, AlertStyle = xlvalidAlertStop/1, Operator=xlBetween/1, Formula1="=ValidStatus"
  12.  
  13.  
Mar 3 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Markus | last post by:
hi, is there a way to set up an user, how only have restricted admin rights on mysql server. this user only should be able to create DBs (and drop them) and set grants on this new DB. i think...
2
by: Karen Sullivan | last post by:
Hi, all. I'm fairly new to SQL, and I have been trying to create a table from a text file. I have been looking at this for days, and can't find the problem. I get a syntax error " Line 55:...
0
by: Manzoorul Hassan | last post by:
I just Installed MySQL v4.1.14 and had created a Database (from a script). But I would like to redo the Database portion of it but am not able to drop the Database. If I do a "show database;" I...
2
by: tom horner | last post by:
Any ideas on why drop and create alias statements would take a long time? We recently went through an upgrade of our production database, which included alter statements to table structures,...
9
by: Peter | last post by:
Hello£¬everyone, My program will collect a testing machine's data ,save the data and deal with the data everyday. I want to use vb.net to create database, add and delete tables or modify the...
1
by: Darren | last post by:
I'm trying to create a file using drag and drop. I want to be able to select a listview item drag it to the shell and create a file. Each icon in the listview represents a blob in a database. When...
2
by: masri999 | last post by:
I have a requirement in SQL 2005 in Development database 1. Schema dbo owns all objects (tables,views,SPs,UDFs etc) . 2. Only DBA's ( who are database owners ) can create, alter tables ....
3
by: Rahul B | last post by:
Hi, I have a user UCLDEV1 which is a part of staff and a group(db2schemagrp1) to which i have not given any permissions. The authorizations of that user are shown as db2 =get authorizations...
2
by: semaj.remle 'at' gmail | last post by:
For files saved in source control, is it better to use code to DROP/ CREATE a procedure like this: ------------------------------------------------------------------ IF OBJECT_ID ('procName') IS...
0
by: Eric Isaacs | last post by:
Optional Create and Alter is nice because it fails, the original is still in place as are the original permissions. Drop and create is also valid in some cases if you want to clear/reset the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.