ไปยังเนื้อหาหลัก

วิธีแทรกรายการแบบเลื่อนลงรหัสสีในตาราง Word?

สมมติว่าฉันมีตารางในเอกสาร Word ของฉันและตอนนี้ฉันต้องการแทรกรายการแบบหล่นลงที่มีรหัสสีในคอลัมน์ของตาราง หมายความว่าเมื่อฉันเลือกหนึ่งตัวเลือกจากเมนูแบบเลื่อนลงสีของเซลล์จะกลายเป็นสีแดงและเมื่อฉันเลือกตัวเลือกอื่นในเมนูแบบเลื่อนลงสีของเซลล์จะกลายเป็นสีเขียวดังภาพหน้าจอต่อไปนี้ คุณจะแก้งานนี้ในเอกสาร Word ได้อย่างไร

เลื่อนลงรหัสสี doc

แทรกรายการแบบเลื่อนลงรหัสสีในเอกสาร Word ด้วยรหัส VBA


แทรกรายการแบบเลื่อนลงรหัสสีในเอกสาร Word ด้วยรหัส VBA

ขั้นตอนต่อไปนี้สามารถช่วยให้คุณทำงานนี้ให้เสร็จสิ้นได้ตามที่คุณต้องการขั้นแรกแทรกรายการดรอปดาวน์จากนั้นจึงใช้สีสำหรับรายการแบบเลื่อนลง กรุณาทำตามนี้:

1. เลือกเซลล์ในตารางที่คุณต้องการแทรกรายการแบบเลื่อนลงจากนั้นคลิก ผู้พัฒนา > การควบคุมเนื้อหารายการแบบหล่นลง ไอคอนดูภาพหน้าจอ:

ดรอปดาวน์รหัสสี doc 1

2. รายการแบบหล่นลงจะถูกแทรกลงในเซลล์เฉพาะจากนั้นคลิก ผู้พัฒนา > อสังหาริมทรัพย์ ดูภาพหน้าจอ:

ดรอปดาวน์รหัสสี doc 2

3. ใน คุณสมบัติการควบคุมเนื้อหา โปรดดำเนินการดังต่อไปนี้:

(1. ) ป้อนชื่อเรื่องลงในไฟล์ ชื่อหนังสือ กล่องข้อความ;

(2. ) คลิก เพิ่ม ไปที่ปุ่ม เพิ่มทางเลือก ไดอะล็อก;

(3. ) ใน เพิ่มทางเลือก ให้พิมพ์รายการแบบหล่นลงในไฟล์ ชื่อที่แสดง กล่องข้อความ.

ดรอปดาวน์รหัสสี doc 3

4. ทำซ้ำขั้นตอนที่ 3 เพื่อแทรกรายการดรอปดาวน์อื่น ๆ ตามที่คุณต้องการ

5. หลังจากสร้างรายการแบบเลื่อนลงรายการแรกคุณสามารถคัดลอกและวางลงในเซลล์อื่น ๆ ได้ตามต้องการ ดูภาพหน้าจอ:

ดรอปดาวน์รหัสสี doc 4

6. จากนั้นคุณควรใช้รหัส VBA โปรดกดไฟล์ ALT + F11 คีย์เพื่อเปิด Microsoft Visual Basic สำหรับแอปพลิเคชัน หน้าต่าง

7. ใน Microsoft Visual Basic สำหรับแอปพลิเคชัน ดับเบิลคลิก เอกสารนี้ จาก โครงการ - โครงการ บานหน้าต่างเพื่อเปิดโหมดจากนั้นคัดลอกและวางรหัสต่อไปนี้ลงในโมดูลเปล่า

รหัส VBA: แทรกรายการแบบเลื่อนลงรหัสสีลงในตารางของเอกสาร Word:

Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
With ContentControl.Range
    If ContentControl.Title = "Status" Then
        Select Case .Text
            Case "Complete"
                .Cells(1).Shading.BackgroundPatternColor = wdColorRed
            Case "In Progress"
                .Cells(1).Shading.BackgroundPatternColor = wdColorGreen
            Case "Not Start"
                .Cells(1).Shading.BackgroundPatternColor = wdColorBlue
            Case Else
                .Cells(1).Shading.BackgroundPatternColor = wdColorAutomatic
        End Select
    End If
End With
End Sub

ดรอปดาวน์รหัสสี doc 5

หมายเหตุ: ในโค้ดด้านบน Status เป็นชื่อเรื่องเมื่อคุณสร้างรายการแบบเลื่อนลงและ สมบูรณ์, กำลังดำเนินการ, ไม่เริ่ม เป็นรายการแบบเลื่อนลงคุณสามารถเปลี่ยนเป็นของคุณเองได้ และคุณยังสามารถเปลี่ยนสีได้ตามต้องการ

8. จากนั้นบันทึกและปิดหน้าต่างรหัสตอนนี้เมื่อคุณเลือกหนึ่งรายการจากรายการแบบเลื่อนลงสีสัมพัทธ์จะเต็มไปด้วยเซลล์ดูภาพหน้าจอ:

เลื่อนลงรหัสสี doc


 

สุดยอดเครื่องมือเพิ่มผลผลิตในสำนักงาน

Kutools สำหรับ Word - ยกระดับประสบการณ์คำศัพท์ของคุณด้วย Over 100 คุณสมบัติเด่น!

🤖 Kutools ผู้ช่วย AI: แปลงโฉมงานเขียนของคุณด้วย AI - สร้างเนื้อหา  /  เขียนข้อความใหม่  /  สรุปเอกสาร  /  สอบถามข้อมูล ขึ้นอยู่กับเอกสารทั้งหมดนี้ภายใน Word

📘 ความเชี่ยวชาญด้านเอกสาร: แยกหน้า  /  ผสานเอกสาร  /  ส่งออกตัวเลือกในรูปแบบต่างๆ (PDF/TXT/DOC/HTML...)  /  แบทช์แปลงเป็น PDF  /  ส่งออกเพจเป็นรูปภาพ  /  พิมพ์หลายไฟล์พร้อมกัน...

การแก้ไขเนื้อหา: ค้นหาแบทช์และแทนที่ ข้ามหลายไฟล์  /  ปรับขนาดรูปภาพทั้งหมด  /  ย้ายแถวและคอลัมน์ของตาราง  /  แปลงตารางเป็นข้อความ...

🧹 ทำความสะอาดได้อย่างง่ายดาย: หลบไป พื้นที่พิเศษ  /  แบ่งส่วน  /  ส่วนหัวทั้งหมด  /  กล่องข้อความ  /  เชื่อมโยงหลายมิติ  / หากต้องการดูเครื่องมือถอดเพิ่มเติม โปรดไปที่ของเรา ลบกลุ่ม...

ส่วนแทรกโฆษณา: แทรก ตัวคั่นหลักพัน  /  กล่องกาเครื่องหมาย  /  ปุ่มวิทยุ  /  คิวอาร์โค้ด  /  บาร์โค้ด  /  ตารางเส้นทแยงมุม  /  คำอธิบายสมการ  /  คำบรรยายภาพ  /  คำบรรยายตาราง  /  รูปภาพหลายภาพ  / ค้นพบเพิ่มเติมใน แทรกกลุ่ม...

🔍 การเลือกที่แม่นยำ: ระบุ หน้าที่เฉพาะเจาะจง  /  ตาราง  /  รูปร่าง  /  หัวเรื่องย่อหน้า  / เสริมการนำทางด้วย ข้อมูลเพิ่มเติม เลือกคุณสมบัติ...

การปรับปรุงดาว: นำทางอย่างรวดเร็วไปยังสถานที่ใด ๆ  /  แทรกข้อความซ้ำอัตโนมัติ  /  สลับระหว่างหน้าต่างเอกสารได้อย่างราบรื่น  /  11 เครื่องมือการแปลง...

???? ต้องการลองใช้คุณสมบัติเหล่านี้หรือไม่? Kutools for Word มีไฟล์ ทดลองใช้ฟรี 60 วันโดยไม่มีข้อจำกัด! 🚀
 

 

Comments (54)
Rated 5 out of 5 · 1 ratings
This comment was minimized by the moderator on the site
hello . i tried but it not go.
This comment was minimized by the moderator on the site
bonjour, je ne parviens pas à enregistrer
This comment was minimized by the moderator on the site
I have a word document that i would like some help with, in short, i have a large inspection table, items either comply, don't, further information etc. I currently use a simple drop-down option which has 5-6 options, ill like a selection option that when different options are selected, that changes the cells/row to a red or green etc, and then also (If possible) updates a Summary Table? is this possible, I'll pay someone for a general templet i can the work on....

Thank you in advance....
This comment was minimized by the moderator on the site
Hello, peter,
Did you mean to select an item from the drop down list, and a specific background color is filled for that row?
Please view the attachment to check if it is your need. If this is not your need, please give your problem more clearly.
Thank you!
This comment was minimized by the moderator on the site
Almost, by changing the drop-down tab the whole row changes color, perfect, thank you, however the magic is then coping the row (Yet only rows that don't comply or need further info etc... ) and placing it in a summary table at the top of the page in another table etc... getting them all to keep adding would be great in a summary table.

I can then alter the summary table as things change, and it updated the main parent table .... I'm not sure this is possible.

I'm happy to have a Teams Meeting to get this working, it would be apricated as it is doing my head in...

This comment was minimized by the moderator on the site
Hi, this is great, got me over a major problem with my fillable form. However, when I go to protect my fillable form, it brings up the debugger box? any idea how i can overcome this?
This comment was minimized by the moderator on the site
Thank you, i have managed to change the background colour. Is there a way i can change the text to white?
This comment was minimized by the moderator on the site
Hello, Emily,
To change the text color to white, please apply the following code:
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
With ContentControl.Range
    If ContentControl.Title = "Status" Then
        Select Case .Text
            Case "Complete"
                .Cells(1).Shading.BackgroundPatternColor = wdColorRed
                .Cells(1).Range.Font.Color = wdColorWhite
            Case "In Progress"
                .Cells(1).Shading.BackgroundPatternColor = wdColorGreen
                .Cells(1).Range.Font.Color = wdColorWhite
            Case "Not Start"
                .Cells(1).Shading.BackgroundPatternColor = wdColorBlue
                .Cells(1).Range.Font.Color = wdColorWhite
            Case Else
                .Cells(1).Shading.BackgroundPatternColor = wdColorAutomatic
        End Select
    End If
End With
End Sub

Please have a try, hope it can help you!
This comment was minimized by the moderator on the site
What code do you add so it goes back to the normal colours (both background and text) when you select "Choose an item" from dropdown list after previously selecting a case
This comment was minimized by the moderator on the site
Hello, Usama,
In this case, the code in this article can help you. When uou select an item and then do back to choosing "Choose an item", the backdround color will not be filled.
Please try again. Thank you!
This comment was minimized by the moderator on the site
Well done! Is it possible to activate different cells (independent of row and column) with an item of the ContentControl... e.g. if we put the alphabet of English letters in a table with 6 rows and 5 columns and want to activate the vowels or the consonants?
This comment was minimized by the moderator on the site
Well done! Is it possible to activate different cells (independent of row and column) with an item of the ContentControl... e.g. if we put the alphabet of English letters in a table with 6 rows and 5 columns and want to activate the vowels or the consonants?
This comment was minimized by the moderator on the site
Hi I want a drop down list of colours but i do not want any text in the cells, i just want my users to be able to choose a colour as I am creating a RAG rated form.

is this possible?

thanks.
This comment was minimized by the moderator on the site
Probably way too late, but in case anyone else is wondering, you can probably do something like this by just adapting the code a bit.

adjust the range.font.color line with the same wd color code as the code above. Its not perfect but if the font is the same color as the highlight, you wont be able to visually see the difference
This comment was minimized by the moderator on the site
I have pasted the code and can see my drop down options (the text) but when I choose them, the background colors don't apply. Do you have any way to help me figure out what I may be doing wrong? Please and thank you! This is exactly what I'm hoping to do in my document, if I can make it work. Thanks!
This comment was minimized by the moderator on the site
Hello, Susan
You should check if the corresponding text in the VBA code has been mofified to your own contents.

Note: In the above code, Status is the title name when you creating the drop down list, and Complete, In Progress, Not Start are the items of the drop down list, you can change them to your own. And you can also change the color to your need.

If there is still problem after changing the text, you can upload the attachment word file and we will help you check it.
Thank you!
This comment was minimized by the moderator on the site
I am having the same issue.
I have changed the titles names in the code and followed the rest of the instructions.
Can you please tell me what I may have done wrong?
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations