Page 1 of 1

การดึงข้อมูลระหว่าง sheet แล้วปรับค่าเป็นหลักพันเปลี่ยนยอดจากลบเป็นบวก

Posted: Fri Dec 02, 2016 10:05 am
by lumi
ต้องการนำข้อมูลจาก Sheet: Purchase ไปวางที่ Sheet B03 แล้วปรับเปลี่ยนตัวเลขให้เป็นหลักพัน
รวมถึงแก้ไขตัวเลขจากยอดลบให้เป็นบวกด้วยโดยใช้ VBA
ข้อมูลใน Sheet: Purchase Column K จะนำไปวางที่ Sheet B03 ใน Column H
ข้อมูลใน Sheet: Purchase Column L จะนำไปวางที่ Sheet B03 ใน Column I
ซึ่งเงื่อนใขการวางคือ Code Column E ใน sheet B03 ต้องตรงกับ Code ใน Column A Sheet : Purchase
ในตำแหน่งที่ 3-6 เช่น Sheet : B03 = 5251 ใน Sheet : Purchase = 7052510000
โดยถ้ามีหลายยอดก็ให้นำมาบวกกันก่อนนำยอดมาวางเช่น Code 1000

ซึ่ง Code ที่เขียนสามารถดึงข้อมูลได้แล้วแต่อยากขอคำแนะนำว่ามีวิธีเขียนให้ง่ายและสั้นกว่านี้มั้ยคะ

Code: Select all

Private Sub CmddataB03_Click()
Application.ScreenUpdating = False
r = 23
For k = 9 To 150
    If Sheet6.Cells(r, 5) = "1000" Then
        Sheet6.Cells(r, 8) = -Round(Sheet7.Cells(13, 11) + Sheet7.Cells(18, 11) + Sheet7.Cells(21, 11) _
        + Sheet7.Cells(26, 11) + Sheet7.Cells(29, 11), 2) / 1000 'KBT Trade
        Sheet6.Cells(r, 9) = -Round(Sheet7.Cells(13, 12) + Sheet7.Cells(18, 12) + Sheet7.Cells(21, 12) _
        + Sheet7.Cells(26, 12) + Sheet7.Cells(29, 12), 2) / 1000 'KBT Non Trade
    End If
            
    If Sheet6.Cells(r, 5) = "1207" Then
        Sheet6.Cells(r, 8) = -Round(Sheet7.Cells(34, 11), 2) / 1000 '1012070000
        Sheet6.Cells(r, 9) = -Round(Sheet7.Cells(34, 12), 2) / 1000 '1012070001
    End If
           
    If Sheet6.Cells(r, 5) = "5101" Then
        Sheet6.Cells(r, 8) = -Round(Sheet7.Cells(50, 11), 2) / 1000 '1051010000
        Sheet6.Cells(r, 9) = -Round(Sheet7.Cells(50, 12), 2) / 1000 '1051010001
    End If
           
    If Sheet6.Cells(r, 5) = "5102" Then
        Sheet6.Cells(r, 8) = -Round(Sheet7.Cells(53, 11), 2) / 1000 '1051020000
    End If
    
    If Sheet6.Cells(r, 5) = "5103" Then
        Sheet6.Cells(r, 8) = -Round(Sheet7.Cells(56, 11), 2) / 1000 '1051030000
    End If
           
    If Sheet6.Cells(r, 5) = "5152" Then
        Sheet6.Cells(r, 8) = -Round(Sheet7.Cells(61, 11), 2) / 1000 '1051520000
        Sheet6.Cells(r, 9) = -Round(Sheet7.Cells(61, 12), 2) / 1000 '1051520001
    End If
           
    If Sheet6.Cells(r, 5) = "5154" Then
        Sheet6.Cells(r, 8) = -Round(Sheet7.Cells(66, 11), 2) / 1000 '1051540000
        Sheet6.Cells(r, 9) = -Round(Sheet7.Cells(66, 12), 2) / 1000 '1051540001
    End If
           
    If Sheet6.Cells(r, 5) = "5157" Then
        Sheet6.Cells(r, 8) = -Round(Sheet7.Cells(69, 11), 2) / 1000 '1051570000
    End If
           
    If Sheet6.Cells(r, 5) = "5158" Then
        Sheet6.Cells(r, 8) = -Round(Sheet7.Cells(72, 11), 2) / 1000 '1051580000
    End If
           
    If Sheet6.Cells(r, 5) = "5160" Then
        Sheet6.Cells(r, 8) = -Round(Sheet7.Cells(77, 11), 2) / 1000 '1051600000
        Sheet6.Cells(r, 9) = -Round(Sheet7.Cells(77, 12), 2) / 1000 '1051600001
    End If
           
    If Sheet6.Cells(r, 5) = "5161" Then
        Sheet6.Cells(r, 8) = -Round(Sheet7.Cells(80, 11), 2) / 1000 '1051610000
    End If
           
    If Sheet6.Cells(r, 5) = "5162" Then
        Sheet6.Cells(r, 9) = -Round(Sheet7.Cells(83, 12), 2) / 1000 '1051620001
    End If
    
    If Sheet6.Cells(r, 5) = "5164" Then
        Sheet6.Cells(r, 9) = -Round(Sheet7.Cells(86, 12), 2) / 1000 '1051640001
    End If
    
    If Sheet6.Cells(r, 5) = "5166" Then
        Sheet6.Cells(r, 9) = -Round(Sheet7.Cells(89, 12), 2) / 1000 '1051660001
    End If
    
    If Sheet6.Cells(r, 5) = "5167" Then
        Sheet6.Cells(r, 9) = -Round(Sheet7.Cells(92, 12), 2) / 1000 '1051670001
    End If
    
    If Sheet6.Cells(r, 5) = "5169" Then
        Sheet6.Cells(r, 9) = -Round(Sheet7.Cells(95, 12), 2) / 1000 '1051690001
    End If
           
    If Sheet6.Cells(r, 5) = "5251" Then
        Sheet6.Cells(r, 8) = -Round(Sheet7.Cells(100, 11), 2) / 1000 '1052510000
        Sheet6.Cells(r, 9) = -Round(Sheet7.Cells(100, 12), 2) / 1000 '1052510001
    End If
           
    If Sheet6.Cells(r, 5) = "5352" Then
        Sheet6.Cells(r, 8) = -Round(Sheet7.Cells(103, 11), 2) / 1000 '1053520000
    End If
           
    If Sheet6.Cells(r, 5) = "5353" Then
        Sheet6.Cells(r, 8) = -Round(Sheet7.Cells(106, 11), 2) / 1000 '1053530000
    End If
           
    If Sheet6.Cells(r, 5) = "8832" Then
        Sheet6.Cells(r, 9) = -Round(Sheet7.Cells(109, 12), 2) / 1000 '1088320001
    End If
           
    If Sheet6.Cells(r, 5) = "5155" Then
        Sheet6.Cells(r, 9) = -Round(Sheet7.Cells(112, 12), 2) / 1000 '9400000001
    End If
    
    If Sheet6.Cells(r, 8) = "0" Then
            Sheet6.Cells(r, 8) = ""
    End If
    If Sheet6.Cells(r, 9) = "0" Then
            Sheet6.Cells(r, 9) = ""
    End If
            r = r + 1
    Next
    Application.ScreenUpdating = True
End Sub

Re: การดึงข้อมูลระหว่าง sheet แล้วปรับค่าเป็นหลักพันเปลี่ยนยอดจากลบเป็นบวก

Posted: Fri Dec 02, 2016 3:29 pm
by DhitiBank
หากอยากให้โค้ดรวบรัดขึ้นคงต้องใช้การลูปครับ แต่เมื่อรหัส 2 ชีทไม่เหมือนกันก็คงต้องทำตารางที่บอกว่ารหัสนี้ในชีท B03 คือรหัสอะไรในชีท Purchase (หรืออาจเขียนใส่ไว้ในโค้ด แต่คิดว่าไม่สะดวกกรณีมีเพิ่มมาใหม่เรื่อยๆ) พอมีตารางเทียบแบบนี้แล้ว เราก็เขียนโค้ดตอนลูปชีท B03 เช่น เมื่อเจอ 1207 มันคือรหัส 1012070000 ก็เอารหัสนี้ไปลูปหาในชีท Purchase อีกที ก็จะได้ค่ามาวางในชีท B03 ครับ คิดว่าง่ายกว่าคำถามที่แล้วมาก ลองเขียนเองดูก่อนครับ

สำหรับโค้ดที่ใช้เปลี่ยนค่าลบเป็นบวกเสมอ ใช้ ABS ครับ
ABS(-123)
ก็จะได้ 123