:D snasui.com ยินดีต้อนรับ :D
ยินดีต้อนรับสู่กระดานถามตอบ Excel and VBA และอื่น ๆ ที่เป็นมิตรกับทุกท่าน มีไฟล์แนบมหาศาล ช่วยให้ท่านค้นหาและติดตามศึกษาได้โดยง่าย :thup: สมาชิกท่านใดที่ยังไม่ได้ระบุ Version ของ Excel ที่ใช้งานจริง สามารถทำตาม Link นี้เพื่อจะได้รับคำตอบที่ตรงกับ Version ของท่านครับ :arrow: ระบุ Version ของ Excel
:!: โปรดทราบ :!:
  1. กรุณาอ่านกฎการใช้บอร์ด (Forum rules) ในตำแหน่งด้านบนของแต่ละบอร์ด
  2. การสมัครสมาชิก การ Login การกู้คืนรหัสผ่าน
    1. สมัครสมาชิกดูขั้นตอนได้ที่ :arrow: สมัครสมาชิก
    2. Login เข้าระบบโดยคลิกปุ่ม Login ตรงมุมขวาบนของหน้านี้ :roll:
    3. การ Login ผ่าน Facebook ดูวิธีที่ :arrow: Login ผ่าน Facebook
    4. ลืมรหัสผ่านสามารถรับรหัสใหม่ได้ที่ :arrow: Reset รหัสผ่าน
  3. มีปัญหาการใช้งาน แจ้งผู้ดูแลระบบได้ที่ :arrow: ติดต่อผู้ดูแลระบบ
  4. กำหนดการตั้งค่าส่วนตัว เช่นตั้งค่าภาษาเป็นไทยหรืออังกฤษดูได้ที่ :arrow: ตั้งค่าส่วนตัว
  5. การตั้งและตอบกระทู้ดูได้ที่ :arrow: วิธีการตั้งและตอบกระทู้
  6. การจัดรูปแบบตัวอักษรด้วย bbcode ในช่องแสดงความคิดเห็นดูได้ที่ :arrow: จัดรูปแบบตัวอักษร
  7. กำหนดขนาดตัวอักษรใน Browser ดูได้ที่ :arrow: กำหนดขนาดตัวอักษรใน Browser

VBA สั่ง Highlight ช่องสีที่มีตัวเลขเหมือนกันและค้ากัน เช่น 123 ,231 ,213 ให้เป็นสีแดง แต่ละชุดตัวเลขแตกต่างกันไป

ฟอรัมถาม-ตอบปัญหาการใช้งาน MS Excel and VBA
Forum rules
  1. ไม่อนุญาตให้ใช้ภาษาแชทในการถามและตอบปัญหา ไม่ใช้คำว่า "คับ" หรือ "อ่ะครับ" แทนคำว่า "ครับ" ไม่ใช้คำว่า "เด๋ว" แทนคำว่า "เดี๋ยว" เป็นต้น เนื่องจากเมื่อแปลเป็นภาษาต่างประเทศแล้วจะให้ความหมายผิดไปจากที่ควรจะเป็น
  2. ห้ามถามโดยระบุชื่อผู้ตอบและต้องตั้งชื่อกระทู้ให้สื่อถึงปัญหาที่จะถาม ไม่ตั้งชื่อว่า ช่วยด้วยครับ, มีปัญหามาปรึกษาครับ เป็นต้น
  3. กรุณาอธิบายปัญหาและระบุคำตอบที่ต้องการมาในกระทู้ด้วยเสมอถึงแม้จะอธิบายไว้ในไฟล์แนบแล้วก็ตาม ทั้งนี้เพื่ออำนวยความสะดวกแก่เพื่อนสมาชิกในการค้นหาข้อมูล
  4. กรุณาแนบไฟล์ตัวอย่างพร้อมแสดงคำตอบที่ถูกต้องมาในไฟล์ด้วยเพื่อให้ง่ายต่อการทำความเข้าใจและสะดวกต่อการตอบคำถาม (ขนาดไฟล์ไม่เกิน 500Kb ขนาดภาพไม่เกิน 800*600 Pixel) ไม่แนบเป็น Link มาจากแหล่งอื่นที่อาจจะถูกลบทิ้งไปโดยต้นทางในภายหลัง นอกจากนี้ไม่ควรแนบไฟล์ที่มีข้อมูลสำคัญอันก่อให้เกิดความเสียหายกับตนเองและผู้อื่น
  5. กรณีเป็นคำถามเกี่ยวกับ Programming เช่น VBA, VB.Net, C#, SQL ฯลฯ ต้องลองเขียนมาเองก่อนเสมอ ถามเฉพาะที่ติดปัญหา ระบุ Module, Procedure ที่ติดปัญหาให้ชัดเจน กรุณาโพสต์ Code ให้แสดงเป็น Code คือเปิดด้วย [code] และปิดด้วย [/code] ตัวอย่างเช่น [code]dim r as range[/code] เพื่อให้แตกต่างจากข้อความทั่วไป สะดวกในการอ่านและทดสอบ (คลิกเพื่อดูตัวอย่าง)
  6. กรุณาแจ้งผลการใช้งานเมื่อได้รับคำตอบว่าตรงกับความต้องการหรือไม่
SuminO
Member
Member
Posts: 81
Joined: Thu Jul 25, 2019 4:57 pm
Excel Ver: 365 2021

VBA สั่ง Highlight ช่องสีที่มีตัวเลขเหมือนกันและค้ากัน เช่น 123 ,231 ,213 ให้เป็นสีแดง แต่ละชุดตัวเลขแตกต่างกันไป

#1

Post by SuminO »

VBA สั่ง Highlight เน้นเซลล์ ที่มีตัวเลขเหมือนกันและซ้ำกันใน Excel
ตัวอย่างเช่น 123,213,321,231 และ 301,103,310.130
แต่ละชุดตัวเลข เปลี่ยนสีที่แตกต่างกันไป


ผมแนบตัวอย่างโค๊ดและไฟล์ตัวอย่างมาให้
ขอบคุณอาจารย์มาก ๆ

Code: Select all

Sub Highlight_Duplicate_Entry()
    Dim ws As Worksheet
    Dim cell As Range
    Dim myrng As Range
    Dim clr As Long
    Dim dict As Object
    Dim colorIndex As Long
    Dim cellRange As Variant
    Dim rng As Variant
    Dim fullRange As Range
    
    Set ws = ThisWorkbook.Sheets("Sheet1")
    
    ' Define the ranges
    cellRange = Array("A2:A29", "D2:D29", "G2:G29", "J2:J29", "M2:M29", "P2:P29", _
                      "A30:A59", "D30:D59", "G30:G59", "J30:J59", "M30:M59", "P30:P59")
    
    ' Combine all ranges into one
    For Each rng In cellRange
        If fullRange Is Nothing Then
            Set fullRange = ws.Range(rng)
        Else
            Set fullRange = Union(fullRange, ws.Range(rng))
        End If
    Next rng
    
    Set dict = CreateObject("Scripting.Dictionary")
    colorIndex = 3  ' Start coloring from color index 3
    
    ' Clear existing colors
    fullRange.Interior.ColorIndex = xlNone
    
    ' Loop through each cell in the combined range
    For Each cell In fullRange
        If cell.Value > 0 Then ' Only consider positive values
            If Application.WorksheetFunction.CountIf(fullRange, cell.Value) > 1 Then
                If Not dict.exists(cell.Value) Then
                    dict.Add cell.Value, colorIndex
                    colorIndex = colorIndex + 1
                End If
                cell.Interior.ColorIndex = dict(cell.Value)
            End If
        End If
    Next cell
End Sub
You do not have the required permissions to view the files attached to this post.
User avatar
snasui
Site Admin
Site Admin
Posts: 30801
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 365
Contact:

Re: VBA สั่ง Highlight ช่องสีที่มีตัวเลขเหมือนกันและค้ากัน เช่น 123 ,231 ,213 ให้เป็นสีแดง แต่ละชุดตัวเลขแตกต่างกันไป

#2

Post by snasui »

:D กรุณาแนบไฟล์ที่มี Code มาด้วยครับ

ไฟล์ที่จะแนบ Code ได้จะต้องมีนามสกุลเป็น .xlsm, .xlsb เป็นต้น ไม่ใช่ .xlsx ครับ
SuminO
Member
Member
Posts: 81
Joined: Thu Jul 25, 2019 4:57 pm
Excel Ver: 365 2021

Re: VBA สั่ง Highlight ช่องสีที่มีตัวเลขเหมือนกันและค้ากัน เช่น 123 ,231 ,213 ให้เป็นสีแดง แต่ละชุดตัวเลขแตกต่างกันไป

#3

Post by SuminO »

ขออภัยครับ อาจารย์
ผมแนบไฟล์มาให้ใหม่แล้ว
You do not have the required permissions to view the files attached to this post.
User avatar
snasui
Site Admin
Site Admin
Posts: 30801
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 365
Contact:

Re: VBA สั่ง Highlight ช่องสีที่มีตัวเลขเหมือนกันและค้ากัน เช่น 123 ,231 ,213 ให้เป็นสีแดง แต่ละชุดตัวเลขแตกต่างกันไป

#4

Post by snasui »

:D ตัวอย่าง Code ครับ

Code: Select all

Sub Test0()
    Dim rall As Range, r As Range, strVal As String
    Dim d As Object, x As Long
    
    Application.ScreenUpdating = False
    Set d = CreateObject("Scripting.Dictionary")
    x = 100000
    With Worksheets(1)
        Set rall = .Range("a2:q30,a32:q59")
        rall.Interior.Color = xlNone
        
        For Each r In rall
            If Not IsEmpty(r.Value) Then
                strVal = Sort0(r.Value)
                If Not d.Exists(strVal) Then
                    d.Add key:=strVal, Item:=x
                    r.Interior.Color = x
                    x = x + 20000
                Else
                    r.Interior.Color = d.Item(strVal)
                End If
            End If
        Next r
    End With
    Application.ScreenUpdating = True
End Sub

Function Sort0(v As Long) As String
    Dim i As Integer, j As Integer, k As Integer
    Dim n As Integer, l As Integer, m As Integer
    Dim arr() As Variant, tmp As Variant
    For i = 1 To VBA.Len(v)
        n = VBA.Mid(v, i, 1)
        ReDim Preserve arr(m)
        arr(m) = CInt(n)
        m = m + 1
    Next i
    For k = LBound(arr) To UBound(arr)
        For l = LBound(arr) To UBound(arr) - 1
            If arr(l) > arr(k) Then
                tmp = arr(k)
                arr(k) = arr(l)
                arr(l) = tmp
            End If
        Next l
    Next k
    Sort0 = VBA.Join(arr, "")
End Function
SuminO
Member
Member
Posts: 81
Joined: Thu Jul 25, 2019 4:57 pm
Excel Ver: 365 2021

Re: VBA สั่ง Highlight ช่องสีที่มีตัวเลขเหมือนกันและค้ากัน เช่น 123 ,231 ,213 ให้เป็นสีแดง แต่ละชุดตัวเลขแตกต่างกันไป

#5

Post by SuminO »

ขอบคุณมากครับอาจารย?

069 096
609 690
960 906
ทั้ง6ตัว สีเดียวกัน พอเปลี่ยนสีต่างกันครับ

แต่หากมองไม่เห็นเลข 0 ข้างหน้า
ต้องปรับให้ฟังก์ชัน Sort0
จัดการกับข้อมูลเป็นสตริงแทนใช่ไหมครับ
ไม่แน่ใจว่าผมมาถูกไหมครับ

Code: Select all

Function Sort0(v As Variant) As String
    Dim i As Integer, j As Integer
    Dim arr() As String, tmp As String
    Dim n As Integer
    
    v = CStr(v)
    
    n = Len(v)
    ReDim arr(1 To n)
    
    For i = 1 To n
        arr(i) = Mid(v, i, 1)
    Next i
    
    For i = 1 To n - 1
        For j = i + 1 To n
            If arr(i) > arr(j) Then
                tmp = arr(i)
                arr(i) = arr(j)
                arr(j) = tmp
            End If
        Next j
    Next i
    
    Sort0 = Join(arr, "")
End Function
You do not have the required permissions to view the files attached to this post.
SuminO
Member
Member
Posts: 81
Joined: Thu Jul 25, 2019 4:57 pm
Excel Ver: 365 2021

Re: VBA สั่ง Highlight ช่องสีที่มีตัวเลขเหมือนกันและค้ากัน เช่น 123 ,231 ,213 ให้เป็นสีแดง แต่ละชุดตัวเลขแตกต่างกันไป

#6

Post by SuminO »

ติดอีกนิดเดียว
รบกวนอาจารย์

คือถ้าหากตัวเลขชุดไหนไม่ตรงกับช่องอื่น ๆเลยให้มันเป็นสีขาวเหมือนเดิมได้ไหมครับ

ได้โค๊ดมาเป็นโค๊ดตัวนี้ครับ

Code: Select all

Sub Test0()
    Dim rall As Range, r As Range, strVal As String
    Dim d As Object, x As Long
    
    Application.ScreenUpdating = False
    Set d = CreateObject("Scripting.Dictionary")
    x = 100000
    With Worksheets(1)
        Set rall = .Range("E2:E1000,H2:H1000,K2:K1000,N2:N1000,Q2:Q1000,T2:T1000,W2:W1000,Z2:Z1000,AC2:AC1000,")
        rall.Interior.Color = xlNone
        
        For Each r In rall
            If Not IsEmpty(r.Value) Then
                strVal = Sort0(Format(r.Value, "000"))
                If Not d.Exists(strVal) Then
                    d.Add key:=strVal, Item:=x
                    r.Interior.Color = x
                    x = x + 20000
                Else
                    r.Interior.Color = d.Item(strVal)
                End If
            End If
        Next r
    End With
    Application.ScreenUpdating = True
End Sub

Function Sort0(v As String) As String
    Dim arr() As String
    Dim i As Integer, j As Integer
    Dim tmp As String
    
 
    ReDim arr(Len(v) - 1)
    For i = 1 To Len(v)
        arr(i - 1) = Mid(v, i, 1)
    Next i
    
    ' Sort the array of characters
    For i = LBound(arr) To UBound(arr) - 1
        For j = i + 1 To UBound(arr)
            If arr(i) > arr(j) Then
                tmp = arr(i)
                arr(i) = arr(j)
                arr(j) = tmp
            End If
        Next j
    Next i
    
    
    Sort0 = Join(arr, "")
End Function
User avatar
snasui
Site Admin
Site Admin
Posts: 30801
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 365
Contact:

Re: VBA สั่ง Highlight ช่องสีที่มีตัวเลขเหมือนกันและค้ากัน เช่น 123 ,231 ,213 ให้เป็นสีแดง แต่ละชุดตัวเลขแตกต่างกันไป

#7

Post by snasui »

:D ตัวอย่างการปรับ Code ครับ

Code: Select all

Sub Test0()
    Dim rall As Range, r As Range, strVal As String
    Dim d As Object, x As Long, itm As Variant, stra As String
    
    Application.ScreenUpdating = False
    Set d = CreateObject("Scripting.Dictionary")
    x = 100000
    With Worksheets(1)
        Set rall = .Range("E2:E1000,H2:H1000,K2:K1000,N2:N1000,Q2:Q1000,T2:T1000,W2:W1000,Z2:Z1000,AC2:AC1000,")
        rall.Interior.Color = xlNone
        
        For Each r In rall
            If Not IsEmpty(r.Value) Then
                strVal = Sort0(Format(r.Value, "000"))
                If Not d.Exists(strVal) Then
                    d.Add Key:=strVal, Item:=x & "|" & r.Address
                    r.Interior.Color = x
                    x = x + 20000
                Else
                    r.Interior.Color = VBA.Split(d.Item(strVal), "|")(0)
                    d.Item(strVal) = r.Interior.Color & "|" & ""
                End If
            End If
        Next r
        For Each itm In d.Keys
            stra = VBA.Split(d.Item(itm), "|")(1)
            If stra <> "" Then
                .Range(stra).Interior.Color = xlNone
            End If
        Next itm
    End With
    Application.ScreenUpdating = True
End Sub
Post Reply