ตามรูปภาพด้านบนคะ จะมี column ที่ใส่ lot number เช่น 891112 ซึ่ง จะมาคำนวน ตามรูปภาพ ต่อจาก Lotnumber
8 = year 9 = Month 11 = Day
เเล้วบรรทัดล่างสุด คือการกำหนดค่า ใน มองช่อง จากดานบนโดย กำหนด ค่า > 1 Year
ซึ่งในเลข lot number นั้น เรากำหนด ถ้าเป็นเดือน 10 = x 11 = y 12 = z
ส่วนปีจะอ้างอิง จาก date ตามตัวเครื่อง
เเต่ตอนนี้ อยากได้ คำสั่งที่ เวลา User ใส่เลข lotnumber ที่มี String อยู่ตรงหน้า เช่น HB8X1112 เเต่จะไม่ให้โปรแกรม มันมอง ตรง 2 ตัวหน้า อะคะ ให้มองเเค่ 8X1112 เเล้วเอามา คำนวนเอง เเต่บาง Lot number User จะใส่ 891214 บางตัวถึงจะมี String อยู่ข้างหน้า Lot อะคะ
รบกวนผู้รู้ ช่วยหน่อยนะคะ
Code (VB.NET)
Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("$E$18").Address Or Target.Address = Range("$F$18").Address Or Target.Address = Range("$G$18").Address Or Target.Address = Range("$H$18").Address Or Target.Address = Range("$I$18").Address Then
Dim lot As String
lot = Range("$E$18").Value
Dim lot1 As String
lot1 = Range("$F$18").Value
Dim lot2 As String
lot2 = Range("$G$18").Value
Dim lot3 As String
lot3 = Range("$H$18").Value
Dim lot4 As String
lot4 = Range("$I$18").Value
If Len(lot) <= 0 Then
Exit Sub
End If
Dim a As String
Dim x As String
a = getLotabc(x)
x = Range("E18:E18").Value
'------------------------------------------------------------------'
Dim a1 As String
Dim x1 As String
a1 = getLotabc1(x1)
x1 = Range("F18:F18").Value
'--------------------------------------------------------------------'
Dim a2 As String
Dim x2 As String
a2 = getLotabc2(x2)
x2 = Range("G18:G18").Value
'--------------------------------------------------------------------'
Dim a3 As String
Dim x3 As String
a3 = getLotabc3(x3)
x3 = Range("H18:H18").Value
'--------------------------------------------------------------------'
Dim a4 As String
Dim x4 As String
a4 = getLotabc4(x4)
x4 = Range("I18:I18").Value
Call ShowMFGDate(lot)
Call ShowMFGDate1(lot1)
Call ShowMFGDate2(lot2)
Call ShowMFGDate3(lot3)
Call ShowMFGDate4(lot4)
Call ExpDate(lot)
Call ExpDate1(lot1)
Call ExpDate2(lot2)
Call ExpDate3(lot3)
Call ExpDate4(lot4)
End If
If Target.Address = Range("$E$18:$I$18").Address Then
If UCase(Target.Value) = UCase(Range("E36:E36").Value) Then
Range("E18:E18").Value = lot
End If
End If
'--------------------Exp-----------------------
If Target.Address = Range("$E$18:$I$18").Address Then
If UCase(Target.Value) = UCase(Range("E37:E37").Value) Then
Range("E18:E18").Value = lot
End If
End If
'--------------------Exp-----------------------
If Target.Address = Range("$E$18:$I$18").Address Then
If UCase(Target.Value) = UCase(Range("F36:F36").Value) Then
Range("F18:F18").Value = lot
End If
End If
'--------------------Exp-----------------------
If Target.Address = Range("$E$18:$I$18").Address Then
If UCase(Target.Value) = UCase(Range("F37:F37").Value) Then
Range("F18:F18").Value = lot1
End If
End If
'--------------------Exp-----------------------
If Target.Address = Range("$E$18:$I$18").Address Then
If UCase(Target.Value) = UCase(Range("G36:G36").Value) Then
Range("G18:G18").Value = lot
End If
End If
'--------------------Exp-----------------------
If Target.Address = Range("$E$18:$I$18").Address Then
If UCase(Target.Value) = UCase(Range("G37:G37").Value) Then
Range("G18:G18").Value = lot2
End If
End If
'--------------------Exp-----------------------
If Target.Address = Range("$E$18:$I$18").Address Then
If UCase(Target.Value) = UCase(Range("H36:H36").Value) Then
Range("H18:H18").Value = lot
End If
End If
'--------------------Exp-----------------------
If Target.Address = Range("$E$18:$I$18").Address Then
If UCase(Target.Value) = UCase(Range("H37:H37").Value) Then
Range("H18:H18").Value = lot3
End If
End If
'--------------------Exp-----------------------
If Target.Address = Range("$E$18:$I$18").Address Then
If UCase(Target.Value) = UCase(Range("I36:I36").Value) Then
Range("I18:I18").Value = lot
End If
End If
'--------------------Exp-----------------------
If Target.Address = Range("$E$18:$I$18").Address Then
If UCase(Target.Value) = UCase(Range("I37:I37").Value) Then
Range("I18:I18").Value = lot4
End If
End If
'--------------------Exp-----------------------
End Sub
Function ConvertDate(lot As String) As Date
Dim nyear As Integer
Dim nmonth As Integer
Dim nday As Integer
Dim sTmp As String
sTmp = Mid(CStr(VBA.Year(Now())), 1, 3)
nyear = CInt("0" & sTmp & Mid(lot, 1, 1))
sTmp = Mid(UCase(lot), 2, 1)
Select Case sTmp
Case "X"
nmonth = 10
Case "Y"
nmonth = 11
Case "Z"
nmonth = 12
Case Else
nmonth = CInt("0" & sTmp)
End Select
nday = CInt("0" & Mid(lot, 3, 2))
Dim dt As Date
dt = DateSerial(nyear, nmonth, nday)
ConvertDate = dt
End Function
Sub ShowMFGDate(lot As String)
Dim showlot As String
showlot = Range("E18:E18").Value
If UCase(showlot) = "NO" Then
Range("E18:E18").Value = showlot
Exit Sub
End If
Dim dt As Date
dt = ConvertDate(showlot)
lot = Range("E36:E36")
Range("E36:E36").Value = Format(dt, "dd-MMM-YY")
End Sub
Function getLotabc(x As String)
Dim a As String
a = Range("E18:E18").Value
getLotabc = a
End Function
'--------------------Exp-----------------------
Function ConvertDateExp(lot As String) As Date
Dim nyear As Integer
Dim nmonth As Integer
Dim nday As Integer
Dim sTmp As String
sTmp = Mid(CStr(VBA.Year(Now())), 1, 3)
nyear = CInt("0" & sTmp & Mid(lot, 1, 1))
sTmp = Mid(UCase(lot), 2, 1)
Select Case sTmp
Case "X"
nmonth = 10
Case "Y"
nmonth = 11
Case "Z"
nmonth = 12
Case Else
nmonth = CInt("0" & sTmp)
End Select
nday = CInt("0" & Mid(lot, 3, 2))
Dim dt As Date
dt = DateSerial(nyear + 1, nmonth, nday - 1)
ConvertDateExp = dt
End Function
Sub ExpDate(lot As String)
Dim showlot As String
showlot = Range("E18:E18").Value
If UCase(showlot) = "NO" Then
Range("E18:E18").Value = showlot
Exit Sub
End If
Dim dt As Date
dt = ConvertDateExp(showlot)
lot = Range("E37:E37")
Range("E37:E37").Value = Format(dt, "dd-MMM-YY")
End Sub
'--------------------Exp-----------------------
'-----------------------------------------------------------------------------------'
Sub ShowMFGDate1(lot1 As String)
Dim showlot As String
showlot = Range("F18:F18").Value
If UCase(showlot) = "NO" Then
Range("F18:F18").Value = showlot
Exit Sub
End If
Dim dt As Date
dt = ConvertDate(showlot)
lot1 = Range("F36:F36")
Range("F36:F36").Value = Format(dt, "dd-MMM-YY")
End Sub
Function getLotabc1(x1 As String)
Dim a1 As String
a1 = Range("F18:F18").Value
getLotabc1 = a1
End Function
'---------------------------------------------------------------------------------------'
'--------------------Exp-----------------------
Function ConvertDateExp1(lot As String) As Date
Dim nyear As Integer
Dim nmonth As Integer
Dim nday As Integer
Dim sTmp As String
sTmp = Mid(CStr(VBA.Year(Now())), 1, 3)
nyear = CInt("0" & sTmp & Mid(lot, 1, 1))
sTmp = Mid(UCase(lot), 2, 1)
Select Case sTmp
Case "X"
nmonth = 10
Case "Y"
nmonth = 11
Case "Z"
nmonth = 12
Case Else
nmonth = CInt("0" & sTmp)
End Select
nday = CInt("0" & Mid(lot, 3, 2))
Dim dt As Date
dt = DateSerial(nyear + 1, nmonth, nday - 1)
ConvertDateExp1 = dt
End Function
Sub ExpDate1(lot1 As String)
Dim showlot As String
showlot = Range("F18:F18").Value
If UCase(showlot) = "NO" Then
Range("F18:F18").Value = showlot
Exit Sub
End If
Dim dt As Date
dt = ConvertDateExp1(showlot)
lot1 = Range("F37:F37")
Range("F37:F37").Value = Format(dt, "dd-MMM-YY")
End Sub
'--------------------Exp-----------------------
Function ConvertDateExp2(lot As String) As Date
Dim nyear As Integer
Dim nmonth As Integer
Dim nday As Integer
Dim sTmp As String
sTmp = Mid(CStr(VBA.Year(Now())), 1, 3)
nyear = CInt("0" & sTmp & Mid(lot, 1, 1))
sTmp = Mid(UCase(lot), 2, 1)
Select Case sTmp
Case "X"
nmonth = 10
Case "Y"
nmonth = 11
Case "Z"
nmonth = 12
Case Else
nmonth = CInt("0" & sTmp)
End Select
nday = CInt("0" & Mid(lot, 3, 2))
Dim dt As Date
dt = DateSerial(nyear + 1, nmonth, nday - 1)
ConvertDateExp2 = dt
End Function
Sub ExpDate2(lot2 As String)
Dim showlot As String
showlot = Range("G18:G18").Value
If UCase(showlot) = "NO" Then
Range("G18:G18").Value = showlot
Exit Sub
End If
Dim dt As Date
dt = ConvertDateExp2(showlot)
lot = Range("G37:G37")
Range("G37:G37").Value = Format(dt, "dd-MMM-YY")
End Sub
'--------------------Exp-----------------------
Sub ShowMFGDate2(lot2 As String)
Dim showlot As String
showlot = Range("G18:G18").Value
If UCase(showlot) = "NO" Then
Range("G18:G18").Value = showlot
Exit Sub
End If
Dim dt As Date
dt = ConvertDate(showlot)
lot2 = Range("G36:G36")
Range("G36:G36").Value = Format(dt, "dd-MMM-YY")
End Sub
Function getLotabc2(x2 As String)
Dim a2 As String
a2 = Range("G18:G18").Value
getLotabc2 = a2
End Function
'---------------------------------------------------------------------------------------'
'--------------------Exp-----------------------
Function ConvertDateExp3(lot As String) As Date
Dim nyear As Integer
Dim nmonth As Integer
Dim nday As Integer
Dim sTmp As String
sTmp = Mid(CStr(VBA.Year(Now())), 1, 3)
nyear = CInt("0" & sTmp & Mid(lot, 1, 1))
sTmp = Mid(UCase(lot), 2, 1)
Select Case sTmp
Case "X"
nmonth = 10
Case "Y"
nmonth = 11
Case "Z"
nmonth = 12
Case Else
nmonth = CInt("0" & sTmp)
End Select
nday = CInt("0" & Mid(lot, 3, 2))
Dim dt As Date
dt = DateSerial(nyear + 1, nmonth, nday - 1)
ConvertDateExp3 = dt
End Function
Sub ExpDate3(lot3 As String)
Dim showlot As String
showlot = Range("H18:H18").Value
If UCase(showlot) = "NO" Then
Range("H18:H18").Value = showlot
Exit Sub
End If
Dim dt As Date
dt = ConvertDateExp3(showlot)
lot = Range("H37:H37")
Range("H37:H37").Value = Format(dt, "dd-MMM-YY")
End Sub
'--------------------Exp-----------------------
Sub ShowMFGDate3(lot3 As String)
Dim showlot As String
showlot = Range("H18:H18").Value
If UCase(showlot) = "NO" Then
Range("H18:H18").Value = showlot
Exit Sub
End If
Dim dt As Date
dt = ConvertDate(showlot)
lot3 = Range("H36:H36")
Range("H36:H36").Value = Format(dt, "dd-MMM-YY")
End Sub
Function getLotabc3(x3 As String)
Dim a3 As String
a3 = Range("H18:H18").Value
getLotabc3 = a3
End Function
'---------------------------------------------------------------------------------------'
'--------------------Exp-----------------------
Function ConvertDateExp4(lot As String) As Date
Dim nyear As Integer
Dim nmonth As Integer
Dim nday As Integer
Dim sTmp As String
sTmp = Mid(CStr(VBA.Year(Now())), 1, 3)
nyear = CInt("0" & sTmp & Mid(lot, 1, 1))
sTmp = Mid(UCase(lot), 2, 1)
Select Case sTmp
Case "X"
nmonth = 10
Case "Y"
nmonth = 11
Case "Z"
nmonth = 12
Case Else
nmonth = CInt("0" & sTmp)
End Select
nday = CInt("0" & Mid(lot, 3, 2))
Dim dt As Date
dt = DateSerial(nyear + 1, nmonth, nday - 1)
ConvertDateExp4 = dt
End Function
Sub ExpDate4(lot4 As String)
Dim showlot As String
showlot = Range("I18:I18").Value
If UCase(showlot) = "NO" Then
Range("I18:I18").Value = showlot
Exit Sub
End If
Dim dt As Date
dt = ConvertDateExp4(showlot)
lot = Range("I37:I37")
Range("I37:I37").Value = Format(dt, "dd-MMM-YY")
End Sub
'--------------------Exp-----------------------
Sub ShowMFGDate4(lot4 As String)
Dim showlot As String
showlot = Range("I18:I18").Value
If UCase(showlot) = "NO" Then
Range("I18:I18").Value = showlot
Exit Sub
End If
Dim dt As Date
dt = ConvertDate(showlot)
lot4 = Range("I36:I36")
Range("I36:I36").Value = Format(dt, "dd-MMM-YY")
End Sub
Function getLotabc4(x4 As String)
Dim a4 As String
a4 = Range("I18:I18").Value
getLotabc4 = a4
End Function
Function ConvertDate(lot As String) As Date
Dim nyear As Integer
Dim nmonth As Integer
Dim nday As Integer
Dim a As String
Dim x As String
a = getLotabc(x)
x = Range("E18:E18").Value
If Len(lot) = 6 Then
lot = a
End If
If Len(lot) = 8 Then
lot = Mid(a, 3, 6)
End If
Dim sTmp As String
sTmp = Mid(CStr(VBA.Year(Now())), 1, 3)
nyear = CInt("0" & sTmp & Mid(lot, 1, 1))
sTmp = Mid(UCase(lot), 2, 1)
Select Case sTmp
Case "X"
nmonth = 10
Case "Y"
nmonth = 11
Case "Z"
nmonth = 12
Case Else
nmonth = CInt("0" & sTmp)
End Select
nday = CInt("0" & Mid(lot, 3, 2))
Dim dt As Date
dt = DateSerial(nyear, nmonth, nday)
ConvertDate = dt
End Function
Function ConvertDateExp(lot As String) As Date
Dim nyear As Integer
Dim nmonth As Integer
Dim nday As Integer
Dim a As String
Dim x As String
a = getLotabc(x)
x = Range("E18:E18").Value
If Len(lot) = 6 Then
lot = a
End If
If Len(lot) = 8 Then
lot = Mid(a, 3, 6)
End If
Dim sTmp As String
sTmp = Mid(CStr(VBA.Year(Now())), 1, 3)
nyear = CInt("0" & sTmp & Mid(lot, 1, 1))
sTmp = Mid(UCase(lot), 2, 1)
Select Case sTmp
Case "X"
nmonth = 10
Case "Y"
nmonth = 11
Case "Z"
nmonth = 12
Case Else
nmonth = CInt("0" & sTmp)
End Select
nday = CInt("0" & Mid(lot, 3, 2))
Dim dt As Date
dt = DateSerial(nyear + 1, nmonth, nday - 1)
ConvertDateExp = dt
End Function