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 » Sun May 30, 2021 1:13 pm
Hi Dear!
I have written a macro in form of custom function to average cells base on red color font.
I checked code right but why not calculate right?
Code: Select all
Function AverageRed(rng As Range)
Dim C As Range
Dim Count As Long
Dim Result As Double
For Each C In rng
If C.Font.ColorIndex=3 And IsNumeric(C.Value) Then
Result = Result + C.Value
Count = Count + 1
End If
Next
If Count = 0 Then
AverageRed = ""
Else
AverageRed = Result / Count
End If
End Function
What's wrong ,any matter will share file
Best Wishes,
snasui
Site Admin
Posts: 31253 Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:
#2
Post
by snasui » Sun May 30, 2021 1:30 pm
Please attach the example Excel file with code above inside.
sna
Silver
Posts: 721 Joined: Tue May 05, 2020 8:18 am
Excel Ver: Excel 365
#3
Post
by sna » Wed Jun 02, 2021 6:17 pm
Here's an attached
the first calculation is right but when change font to black it is not working well
Best Wishes,
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:
#4
Post
by snasui » Wed Jun 02, 2021 7:30 pm
Your attached file is incorrect file type. The extension should be .xlsm instead .xlsx.
Changing the font color does not trigger your formula. Please see this link for more information.
https://stackoverflow.com/questions/563 ... th-changes
sna
Silver
Posts: 721 Joined: Tue May 05, 2020 8:18 am
Excel Ver: Excel 365
#5
Post
by sna » Wed Jun 02, 2021 7:49 pm
Ok thanks for the link
I will take a look
Best Wishes,
sna
Silver
Posts: 721 Joined: Tue May 05, 2020 8:18 am
Excel Ver: Excel 365
#6
Post
by sna » Sat Jun 05, 2021 1:34 pm
File attached