Input multiple ID and QTY in Excel VBA userform

liaaa

New Member
Joined
Sep 29, 2023
Messages
20
Office Version
  1. 2010
Platform
  1. Windows
Hello,
I'm trying to input multiple ID in the textbox IDITEM and the result is each id will be transferred to multiple rows in the worksheet "database". I also want to input qty for each id. I have tried this code but it's not working. Any help or tips are really appreciated.

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 k As Long


'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
For k = LBound(QTY) To UBound(QTY)


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)
.Range("I" & iRow).Value = UserForm1.TxtITEM.Value
.Range("J" & iRow).Value = QTY(k)
.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 j
Next k

End Sub


Here is the link for the file
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
You are proceeding to wrong Next item between j and k at the end of the code. Your code formatting can be improved to avoid such instances. Please try:
Option Explicit

VBA Code:
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 k As Long
 
  '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
 
    For k = LBound(QTY) To UBound(QTY)
      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)
      .Range("I" & iRow).Value = UserForm1.TxtITEM.Value
      .Range("J" & iRow).Value = QTY(k)
      .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 k
  Next j

End Sub
 
Upvote 0
Hi,
Thank you very much for your help. I have tried your code.

I have input data in userform like this.
1697204912160.png

And in the database sheets, the data appears like this, the output is repeated three times. Can you help me to fix this? Thank youu
1697205097421.png
 
Upvote 0
This should be OK:
VBA Code:
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
 
  '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)
    .Range("I" & iRow).Value = UserForm1.TxtITEM.Value
    .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
 
Upvote 0
Solution

Forum statistics

Threads
1,224,820
Messages
6,181,162
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top