Page 1 of 1

เปรียบเทียบค่าภายในคอลัมน์

Posted: Wed Feb 28, 2018 3:28 pm
by aapichaya
ต้องการเปรียบเทียบค่าในคอลัมน์E ระหว่างสองRow โดยเริ่มจาก Rowที่3 ไปเรื่อยๆจะบรรทัดสุดท้าย และให้แสดงค่าในคอลัมน์ AW
โดย ถ้ามีเปรียบเทียบเเล้ว มีค่าเท่ากันให้=1 ไม่เท่ากันให้=0
ลองเขียนโค้ดเเล้วไม่ขึ้นerror แต่ก็ไม่แสดงผลค่ะ ไม่ทราบว่าผิดตรงไหน

Code: Select all

Sub test6()
Dim wsData As New Worksheet
Dim i, j, k As Integer
Dim title1, title2 As Variant
Dim result As Variant

Set wsData = Sheets("Data")

i = 0
j = 3
k = 4

Do
    title1 = wsData.Cells(j + i, 5)
    title2 = wsData.Cells(k + i, 5)
    result = wsData.Cells(j + i, 48)
    
    If title1 = title2 Then
        result = "1"
                     
          'Debug.Print "1"
        Else
            result = "0"
            'Debug.Print "0"
        End If
   
i = i + 1
Loop Until title1 = ""

End Sub

Re: เปรียบเทียบค่าภายในคอลัมน์

Posted: Wed Feb 28, 2018 4:25 pm
by logic
ลองปรับเป็นแบบนี้ครับ

Code: Select all

Dim wsData As New Worksheet
Dim i, j, k As Integer
Dim title1 As Range, title2 As Range
Dim result As Range

Set wsData = Sheets("Data")

i = 0
j = 3
k = 4

Do
    Set title1 = wsData.Cells(j + i, 5)
    Set title2 = wsData.Cells(k + i, 5)
    Set result = wsData.Cells(j + i, 48)
    
    If title1 = title2 Then
        result = "1"
        'Debug.Print "1"
    Else
        result = "0"
        'Debug.Print "0"
    End If
    i = i + 1
Loop Until title2 = ""

Re: เปรียบเทียบค่าภายในคอลัมน์

Posted: Fri Mar 02, 2018 11:06 am
by aapichaya
ขอบคุณมากค่ะ