473,545 Members | 2,042 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Create Drop-down List in Excel with Python

34 New Member
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 14584
jld730
34 New Member
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 New Member
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
1186
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 with the standard mysql-privileges this can't be done. - when a user has the drop-priv he can drop ALL DBs in the server. - when a user has grant...
2
5135
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: Incorrect syntax near 'DateUpdated'." Here is the query. Any suggestions would be appreciated, as I am trying to learn and improve. Use ACH go ...
0
1568
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 still see the database. But when I look in the data directory, it is not there. Below is my attempt the create / drop the DB: mysql> create database...
2
2620
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, create tables, drop tables, and data inserts, deletes, and updates. Also, we dropped and recreated all the alias's we had on the tables and views to...
9
8266
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 records in the database. Is it possible to create a SQL Server database using vb.net? I know I can use vb.net and ADOX to create a Access database....
1
4693
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 I drop onto the shell I want a file created with a specified name containing the data from the database. I've looked at the FileDrop format but it...
2
22564
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 . Developer's should not create or alter tables . 3. Developers can create/alter Stored Procedure/User Defined functions in dbo schema and can execute...
3
6663
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 Administrative Authorizations for Current User
2
8214
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 NOT NULL DROP PROC procName GO CREATE PROCEDURE procName AS BEGIN --proc code
0
2219
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 permissions. I use an approach which lets me choose which one I want to implement on the fly. If I remove the first two dashes, the drop is disabled...
0
7410
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7668
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7923
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7773
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5984
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
1901
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1025
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
722
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.