Sub Submit()
Application.ScreenUpdating = False
Dim App As New Excel.Application
Dim wBook As Excel.Workbook
Dim FileName As String
Dim iRow As Long
FileName = ThisWorkbook.Path & "\Database.xlsm"
Dim IDITEM As Variant
Dim QTY As Variant
Dim j As Long
Dim itemName As String
'CheckFile exist Or Not
If Dir(FileName) = "" Then
MsgBox "Database is missing!", vbOKOnly + vbCritical, "Error"
Exit Sub
End If
' Check to makesure the txtIDITEM is splittable
If InStr(UserForm1.TxtIDITEM.Value, ",") > 0 Then
IDITEM = Split(UserForm1.TxtIDITEM.Value, ",")
Else
IDITEM = Array(UserForm1.TxtIDITEM.Value)
End If
For j = LBound(IDITEM) To UBound(IDITEM)
' Check to makesure the txtQTY is splittable
If InStr(UserForm1.TxtQTY.Value, ",") > 0 Then
QTY = Split(UserForm1.TxtQTY.Value, ",")
Else
QTY = Array(UserForm1.TxtQTY.Value)
End If
Set wBook = App.Workbooks.Open(FileName)
With wBook.Sheets("Database")
iRow = .Range("A" & Application.Rows.Count).End(xlUp).Row + 1
.Range("A" & iRow).Value = iRow - 1
.Range("B" & iRow).Value = UserForm1.TxtNODOCUMENT.Value
.Range("C" & iRow).Value = UserForm1.TxtNUMBER.Value
.Range("D" & iRow).Value = [Text(Now()+1, "DD-MM-YYY")]
.Range("E" & iRow).Value = UserForm1.CmbNIP.Value
.Range("F" & iRow).Value = UserForm1.TxtPROJECTNAME.Value
.Range("G" & iRow).Value = UserForm1.TxtNOCONTRACT.Value
.Range("H" & iRow).Value = IDITEM(j)
If IsError(Application.Match(IDITEM(j), Worksheets("ITEM").Range("A:A"), 0)) Then
itemName = InputBox("Please provide Item Name for ID: " & IDITEM(j))
Else
itemName = Worksheets("ITEM").Cells(Application.Match(IDITEM(j), Worksheets("ITEM").Range("A:A"), 0), "B").Value
End If
End With
.Range("I" & iRow).Value = itemName
.Range("J" & iRow).Value = QTY(j)
.Range("K" & iRow).Value = UserForm1.TxtSATUAN.Value
.Range("L" & iRow).Value = UserForm1.TxtDELIVERYDATE.Value
.Range("M" & iRow).Value = UserForm1.TxtSUPPLIER.Value
.Range("N" & iRow).Value = Application.UserName
End With
wBook.Close savechanges:=True
App.Quit
Set App = Nothing
Next
End Sub