snasui.com ยินดีต้อนรับ
ยินดีต้อนรับสู่กระดานถามตอบ Excel and VBA และอื่น ๆ ที่เป็นมิตรกับทุกท่าน มีไฟล์แนบมหาศาล ช่วยให้ท่านค้นหาและติดตามศึกษาได้โดยง่าย สมาชิกท่านใดที่ยังไม่ได้ระบุ Version ของ Excel ที่ใช้งานจริง สามารถทำตาม Link นี้เพื่อจะได้รับคำตอบที่ตรงกับ Version ของท่านครับ ระบุ Version ของ Excel
ฟอรัมถาม-ตอบปัญหาการใช้งานสูตรและฟังก์ชัน Excel
Forum rules
ไม่อนุญาตให้ใช้ภาษาแชทในการถามและตอบปัญหา ไม่ใช้คำว่า "คับ" หรือ "อ่ะครับ" แทนคำว่า "ครับ" ไม่ใช้คำว่า "เด๋ว" แทนคำว่า "เดี๋ยว" เป็นต้น เนื่องจากเมื่อแปลเป็นภาษาต่างประเทศแล้วจะให้ความหมายผิดไปจากที่ควรจะเป็น
ห้ามถามโดยระบุชื่อผู้ตอบและต้องตั้งชื่อกระทู้ให้สื่อถึงปัญหาที่จะถาม ไม่ตั้งชื่อว่า ช่วยด้วยครับ, มีปัญหามาปรึกษาครับ เป็นต้น
กรุณาอธิบายปัญหาและระบุคำตอบที่ต้องการมาในกระทู้ด้วยเสมอถึงแม้จะอธิบายไว้ในไฟล์แนบแล้วก็ตาม ทั้งนี้เพื่ออำนวยความสะดวกแก่เพื่อนสมาชิกในการค้นหาข้อมูล
กรุณาแนบไฟล์ตัวอย่างพร้อมแสดงคำตอบที่ถูกต้องมาในไฟล์ด้วยเพื่อให้ง่ายต่อการทำความเข้าใจและสะดวกต่อการตอบคำถาม (ขนาดไฟล์ไม่เกิน 500Kb ขนาดภาพไม่เกิน 800*600 Pixel) ไม่แนบเป็น Link มาจากแหล่งอื่นที่อาจจะถูกลบทิ้งไปโดยต้นทางในภายหลัง นอกจากนี้ไม่ควรแนบไฟล์ที่มีข้อมูลสำคัญอันก่อให้เกิดความเสียหายกับตนเองและผู้อื่น
กรณีเป็นคำถามเกี่ยวกับ Programming เช่น VBA, VB.Net, C#, SQL ฯลฯ ต้องลองเขียนมาเองก่อนเสมอ ถามเฉพาะที่ติดปัญหา ระบุ Module, Procedure ที่ติดปัญหาให้ชัดเจน กรุณาโพสต์ Code ให้แสดงเป็น Code คือเปิดด้วย [code] และปิดด้วย [/code] ตัวอย่างเช่น [code]dim r as range[/code] เพื่อให้แตกต่างจากข้อความทั่วไป สะดวกในการอ่านและทดสอบ (คลิกเพื่อดูตัวอย่าง)
กรุณาแจ้งผลการใช้งานเมื่อได้รับคำตอบว่าตรงกับความต้องการหรือไม่
sna
Silver
Posts: 721 Joined: Tue May 05, 2020 8:18 am
Excel Ver: Excel 365
#1
Post
by sna » Mon Oct 12, 2020 9:29 pm
Hi there!
I would need your help regarding code to select only rows have yellow color.
I try to code but not working
Also attached template
Best wishes
You do not have the required permissions to view the files attached to this post.
puriwutpokin
Guru
Posts: 3801 Joined: Fri Jan 04, 2013 9:49 pm
Location: Bangkok
Excel Ver: MS.365
#2
Post
by puriwutpokin » Mon Oct 12, 2020 10:55 pm
Try it
Code: Select all
Sub DeleteYello()
Dim rg As Range, i As Long
Set rg = Range(Range("a2"), Range("A" & Rows.Count).End(xlUp))
For i = rg.Rows.Count To 1 Step -1
If rg(i).Interior.ColorIndex = 6 Then
rg(i).EntireRow.Delete Shift:=xlUp
End If
Next i
End Sub
sna
Silver
Posts: 721 Joined: Tue May 05, 2020 8:18 am
Excel Ver: Excel 365
#3
Post
by sna » Tue Oct 13, 2020 2:57 am
Thanks .I need select only yellow rows no delete
snasui
Site Admin
Posts: 31253 Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:
#4
Post
by snasui » Tue Oct 13, 2020 8:33 am
The example code as below:
Code: Select all
'Other code
Dim strRg As String
Set rg = Range(Range("a2"), Range("A" & Rows.Count).End(xlUp))
For i = 2 To rg.Rows.Count
If rg(i).Interior.ColorIndex = 6 Then
strRg = strRg & "," & rg(i).Address(0, 0)
End If
Next
Range(Mid(strRg, 2)).EntireRow.Select
'Other code
sna
Silver
Posts: 721 Joined: Tue May 05, 2020 8:18 am
Excel Ver: Excel 365
#5
Post
by sna » Tue Oct 13, 2020 9:22 am
Thank you will update soon
sna
Silver
Posts: 721 Joined: Tue May 05, 2020 8:18 am
Excel Ver: Excel 365
#6
Post
by sna » Tue Oct 13, 2020 10:11 am
It occurs run-time-error '1004'
It highlight this part
Range(Mid(strRg, 2)).EntireRow.Select
sna
Silver
Posts: 721 Joined: Tue May 05, 2020 8:18 am
Excel Ver: Excel 365
#7
Post
by sna » Tue Oct 13, 2020 10:28 am
Here's an update
You do not have the required permissions to view the files attached to this post.
snasui
Site Admin
Posts: 31253 Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:
#8
Post
by snasui » Tue Oct 13, 2020 4:42 pm
The error occurred because not met condition. Adjust your code with below:
Code: Select all
'Other code
Dim strRg As String
Set rg = Sheets("Bakong").Range(Range("a2"), Range("a" & Rows.Count).End(xlUp))
For i = rg.Rows.Count To 1 Step -1
If rg(i).Interior.ColorIndex = 6 Then
strRg = strRg & "," & rg(i).Address(0, 0)
End If
Next i
If strRg <> "" Then
Range(Mid(strRg, 2)).EntireRow.Select
End If
'Other code
sna
Silver
Posts: 721 Joined: Tue May 05, 2020 8:18 am
Excel Ver: Excel 365
#9
Post
by sna » Tue Oct 13, 2020 7:44 pm
Hi I test it but it still work nothing
snasui
Site Admin
Posts: 31253 Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:
#10
Post
by snasui » Tue Oct 13, 2020 7:59 pm
Please read my post #8 with carefully.
sna
Silver
Posts: 721 Joined: Tue May 05, 2020 8:18 am
Excel Ver: Excel 365
#11
Post
by sna » Tue Oct 13, 2020 8:35 pm
I add this line if strRg <> "" Then
To check strRg value like in post #8 but still no luck
snasui
Site Admin
Posts: 31253 Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:
#12
Post
by snasui » Tue Oct 13, 2020 8:42 pm
snasui wrote: Tue Oct 13, 2020 4:42 pm
The error occurred because not met condition.
The above is important information
sna
Silver
Posts: 721 Joined: Tue May 05, 2020 8:18 am
Excel Ver: Excel 365
#13
Post
by sna » Tue Oct 13, 2020 8:51 pm
Hi what is the condition behind?
I just need code to select row with yellow color
from column A to I only.
snasui
Site Admin
Posts: 31253 Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:
#14
Post
by snasui » Tue Oct 13, 2020 8:58 pm
Please check this statement.
rg(i).Interior.ColorIndex = 6
sna
Silver
Posts: 721 Joined: Tue May 05, 2020 8:18 am
Excel Ver: Excel 365
#15
Post
by sna » Tue Oct 13, 2020 9:41 pm
I check the code work but yellow here comes from conditional formatting.this file came from my friend so the code won't trigger.how can we handle this?I just realized this because he doesn't say color it is from conditional formatting
snasui
Site Admin
Posts: 31253 Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:
#16
Post
by snasui » Tue Oct 13, 2020 10:17 pm
Adjust with the code below:
Code: Select all
'Other code
For i = rg.Rows.Count To 1 Step -1
If rg(i).DisplayFormat.Interior.ColorIndex = 6 Then
strRg = strRg & "," & rg(i).Address(0, 0)
End If
Next i
'Other code
sna
Silver
Posts: 721 Joined: Tue May 05, 2020 8:18 am
Excel Ver: Excel 365
#17
Post
by sna » Tue Oct 13, 2020 10:58 pm
Thanks
sna
Silver
Posts: 721 Joined: Tue May 05, 2020 8:18 am
Excel Ver: Excel 365
#18
Post
by sna » Tue Oct 13, 2020 11:15 pm
It is error this line
run time error '438'
if rg(i).DisplayFormat.Interior.ColorIndex = 6
snasui
Site Admin
Posts: 31253 Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:
#19
Post
by snasui » Wed Oct 14, 2020 7:28 am
It not show this problem in my machine.