Page 1 of 1
fill code auto
Posted: Tue Mar 03, 2015 11:45 pm
by yukioh
สวัสดีครับ อาจารย์
ขออนุญาตรบกวนสอบถาม VBA ครับ
ผมมีข้อมูลอยู่ในตาราง ผมต้องการให้ ใส่ code ตามเงื่องไขดือว่า
1 ถ้าหาก ช่อง Code มีละหัด 123, ช่อง Conv ให้ใส่คำว่า A
ถ้าหาก ช่อง Code มีละหัด 150, ช่อง Conv ให้ใส่คำว่า B
ถ้าหาก ช่อง Code มีละหัด 456, ช่อง Conv ให้ใส่คำว่า C
2 ถ้าหาก ช่อง Code ไม่มีรหัสตามเงือนไขทีกำหนดไว้, เป็นรหัสอื่น, ช่อง Conv ให้ใส่ตาม เซลล์ B8
ดั่งตัวอย่างไฟล์ที่แนบมา
รบกวนอาจารย์ด้วยครับ
Re: fill code auto
Posted: Wed Mar 04, 2015 7:35 am
by snasui

จากคำอธิบายด้านบน สามารถใช้สูตรในการ Lookup ข้อมูลได้ ไม่จำเป็นต้องใช้ Code ครับ
เช่นที่ M4 คีย์สูตร
=If(Isna(Vlookup(e4,$a$19:$b$21,2,0)),b8,Vlookup(e4,$a$19:$b$21,2,0))
Enter > Copy ลงด้านล่าง
Re: fill code auto
Posted: Wed Mar 04, 2015 8:17 pm
by yukioh
เนื้องจากว่า ถ้าหากใช้สูตร จะมีคนอื่นคีย์ข้อมูลผิด และ ลบสูตรออกไป
เพื้อไม่ให้เกีดข้อความผิดพลาด ผมจิ่งต้องใช้ Code ครับ
รบกวนอาจารย์ดูให้ด้วยครับ
Code: Select all
rivate Sub CommandButton1_Click()
With Application
.EnableEvents = False
.DisplayAlerts = False
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
With Sheets("MAIN")
[N2] = "X"
For i = 4 To 100
If IsNumeric(Left(Trim(Cells(i, 5).Value), 1)) = True Then
If Cells(i, 13).Value = "" And Cells(i, 5).Value <> "" Then
Cells(i, 13).Value = [B7]
End If
If Trim(Cells(i, 5).Value) Like "123*" Then
If Right(Left(Trim(Cells(i, 5).Value), 3), 2) >= 10 And Right(Left(Trim(Cells(i, 5).Value), 3), 2) <= 13 Then
Cells(i, 13).Value = "A"
End If
If Trim(Cells(i, 5).Value) Like "150****" Then
Cells(i, 13).Value = "B"
End If
End If
End If
Next
[N2] = ""
End With
With Application
.EnableEvents = True
.DisplayAlerts = True
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
End With
End Sub
Re: fill code auto
Posted: Wed Mar 04, 2015 8:23 pm
by snasui

ถ้าใช้ Code วางสูตรตามด้านบน ไม่ทราบว่าติดอะไรหรือไม่ ถ้าไม่ติด ให้ใช้ Code วางสูตรได้เลย ติดตรงไหนค่อยมาถามกันต่อครับ
Code ที่เขียนมามีเงื่อนไขมากกว่าที่อธิบายมาในฟอรัม ควรจะอธิบายเงื่อนไขมาทั้งหมด และควรที่จะแจ้งมาด้วยเสมอว่าติดขัดที่บรรทัดใดครับ