snasui.com ยินดีต้อนรับ ยินดีต้อนรับสู่กระดานถามตอบ Excel and VBA และอื่น ๆ ที่เป็นมิตรกับทุกท่าน มีไฟล์แนบมหาศาล ช่วยให้ท่านค้นหาและติดตามศึกษาได้โดยง่าย สมาชิกท่านใดที่ยังไม่ได้ระบุ Version ของ Excel ที่ใช้งานจริง สามารถทำตาม Link นี้เพื่อจะได้รับคำตอบที่ตรงกับ Version ของท่านครับ ระบุ Version ของ Excel
Sub CopyPartCode()
Dim nvalue As Long
Application.ScreenUpdating = False
Range("A1").Select
nrow = 4
Do Until ActiveCell.Offset(1, 0).Value = ""
With ActiveCell
If Len(ActiveCell.Value) = 13 Then
Sheets("schedule").Cells(nrow, 1).Value = .Value
Set arange = Range(ActiveCell.Offset(1, 6), ActiveCell.Offset(1, 6).End(xlDown))
For Each a In arange
tvalue = Left(a.Value, 2)
If tvalue = "" Then Exit For
nvalue = Left(a.Value, 2)
For Each b In Sheets("schedule").Range("c2:ag2")
If nvalue = b.Value Then
Sheets("schedule").Cells(nrow, nvalue + 2).Value = "D"
Exit For
End If
Next b
Next a
ActiveCell.Offset(1, 0).Select
nrow = nrow + 1
Else
ActiveCell.Offset(1, 0).Select
End If
End With
Loop
MsgBox ("Process Completed!")
Sheets("schedule").Select
End Sub
Sub CopyPartCode()
Dim nvalue As Integer
Dim arange As Range
Dim r As Range
Dim tvalue As Integer
Application.ScreenUpdating = False
nrow = 4
With Sheets("data")
Set arange = .Range("a2", .Range("a" & .Rows.Count).End(xlUp)).Offset(0, 6)
End With
For Each r In arange
If Len(r.Offset(0, -6).Value) = 13 Then
Sheets("schedule").Cells(nrow, 1).Value = r.Offset(0, -6).Value
For Each a In arange
tvalue = Left(a.Value, 2)
If IsEmpty(tvalue) Then Exit For
nvalue = Left(a.Offset(0, -1).Value, 2)
For Each b In Sheets("schedule").Range("c2:ag2")
If tvalue = b.Value Then
Sheets("schedule").Cells(nrow, b.Column).Value = "D"
ElseIf nvalue = b.Value Then
Sheets("schedule").Cells(nrow, b.Column).Value = "R"
End If
Next b
Next a
nrow = nrow + 1
End If
Next r
Application.ScreenUpdating = True
MsgBox ("Process Completed!")
Sheets("schedule").Select
End Sub
เรียน คุณ snasui
หลังจากรันมาโครแล้วผลลัพธ์ยังไม่ถูกต้องนะคะ ทำไมมันถึงมี R และ D ซ้ำกันหลายครั้งใน part code เดียวกัน ซึ่งจริง ๆ แล้วต้องมีแค่อย่างละ 1 ตัวค่ะ จากตารางข้อมูลตัวอย่าง
เช่น CBDGD0002QS24 ก็จะแสดงค่า R ในช่องวันที่ 17 และ D ในช่องวันที่ 11 เท่านั้น แต่โค้ดที่อาจารย์ให้มารันแล้วพบว่า มี R และ D ในทุกช่องที่อยู่ในช่วงวันที่ของข้อมูลในชีท Data ควรแก้ไขให้ถูกต้องอย่างไรคะ
Sub test()
Dim lr As Long, r As Range
Dim i As Integer, j As Integer
Application.ScreenUpdating = False
With Sheets("Data")
lr = .Range("A" & Rows.Count).End(xlUp).Row
.Range("H1").Value = "R"
.Range("I1").Value = "D"
For Each r In .Range("F2:F" & lr)
If r.Value <> "" Then
r.Offset(0, 2).Value = Left(r, 2)
End If
Next r
For Each r In .Range("G2:G" & lr)
If r.Value <> "" Then
r.Offset(0, 2).Value = Left(r, 2)
End If
Next r
.Range("A2:A" & lr).Copy Sheets("Schedule").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
.Range("H1:I" & lr).Copy Sheets("Schedule").Range("AH3")
.Columns("H:I").ClearContents
End With
lr = Range("A" & Rows.Count).End(xlUp).Row
lc = Cells(3, Columns.Count).End(xlToLeft).Column - 2
For i = 4 To lr
For j = 3 To lc
If Cells(i, 34) = Cells(2, j) Then
Cells(i, j).Value = "R"
End If
Next j
Next i
For i = 4 To lr
For j = 3 To lc
If Cells(i, 35) = Cells(2, j) Then
Cells(i, j).Value = "D"
End If
Next j
Next i
Columns("AH:AI").ClearContents
Application.ScreenUpdating = True
End Sub
Sub test()
Dim lr As Long, r As Range
Dim i As Integer, j As Integer
Application.ScreenUpdating = False
With Sheets("Data")
lr = .Range("A" & Rows.Count).End(xlUp).Row
.Range("H1").Value = "R"
.Range("I1").Value = "D"
For Each r In .Range("F2:F" & lr)
If r.Value <> "" Then
r.Offset(0, 2).Value = Left(r, 2)
End If
Next r
For Each r In .Range("G2:G" & lr)
If r.Value <> "" Then
r.Offset(0, 2).Value = Left(r, 2)
End If
Next r
.Range("A2:A" & lr).Copy Sheets("Schedule").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
.Range("H1:I" & lr).Copy Sheets("Schedule").Range("AH3")
.Columns("H:I").ClearContents
End With
Sheets("schedule").Activate
lr = Range("A" & Rows.Count).End(xlUp).Row
lc = Cells(3, Columns.Count).End(xlToLeft).Column - 2
For i = 4 To lr
For j = 3 To lc
If Cells(i, 34) = Cells(2, j) Then
Cells(i, j).Value = "R"
End If
Next j
Next i
For i = 4 To lr
For j = 3 To lc
If Cells(i, 35) = Cells(2, j) Then
Cells(i, j).Value = "D"
End If
Next j
Next i
Columns("AH:AI").ClearContents
Application.ScreenUpdating = True
End Sub
รบกวนอีกรอบนะคะ ถ้าเราต้องการเพิ่มเงื่อนไข โดยให้ part code ซ้ำกันรวมเป็นแถวเดียว โดยมีเงื่อนไขดังนี้
1.ถ้าได้รับงานก่อนวัน Del date (หรือวันที่เดียวกัน) จะแสดงผลเป็น R/D ในช่องวัน Del date
2.ถ้าได้รับงานหลังวัน Del date จะแสดงผลเป็น R ในช่อง Del date และ D ในช่อง Posts date (เงื่อนไขเก่า)
เงื่อนไขตามชีท data test
ผลลัพธ์ที่ต้องการตามชีท result ค่ะ
พอจะมีวิธีไหมคะ รบกวนแนะนำด้วยค่ะ ปวดสมองจริง ๆ
Sub DeleteDups()
Dim x As Long
Dim LastRow As Long
LastRow = Range("A65536").End(xlUp).Row
For x = LastRow To 1 Step -1
If Application.WorksheetFunction.CountIf(Range("A1:A" & x), Range("A" & x).Text) > 1 Then
Range("A" & x).EntireRow.Delete
End If
Next x
End Sub