Autofill Item name based on ID item in Excel VBA Userform

liaaa

New Member
Joined
Sep 29, 2023
Messages
20
Office Version
  1. 2010
Platform
  1. Windows
Hello,
So I try to make a userform. The user only needs to fill in the ID item (column H) and the item name (column I) will be filled automatically. Is this possible for multiple ID ITEM in one textbox? So user will input multiple ID Items and then the Item name will be filled automatically. Or is there another alternative to do this?
1697554263332.png

1697554323910.png


This is the link for the file

Thank you for your help :)
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
I remember your project :)
VBA Code:
Range("H" & iRow).Value = IDITEM(j)
With Worksheets("ITEM")
Range("I" & iRow).Value = .Cells(Application.Match(IDITEM(j), .Range("A:A"),0), "B").Value
End With
But this code may give error if item number does not appear in ITEM sheet. It must exist.
 
Upvote 0
Hi,
Thank you very much for your response. I have tried it. Is it possible to make it like this?
There will be some items that don't have ID, so i must input the item name manually.
 

Attachments

  • Screenshot_20231018-122140.jpg
    Screenshot_20231018-122140.jpg
    50.4 KB · Views: 13
Upvote 0
I see.. Then do it like quantity. Make an itemNames array. Input them with comma. Use like:
VBA Code:
Range("I" & iRow).Value = itemNames(j)
 
Upvote 0
If i do like this, the item that have Id Will be filled automatically or i must also input it manually?
 
Upvote 0
With this method you must input manually with seperating comma.
If you want automatically, then you must predifine the item list and try the first method.
Which one you want exactly?
 
Upvote 0
I want to try both method. For example, there are 4 items. item 1 & item 2 have Id, so i just need to input the id and items name Will be filled automatically. But item 3 & item 4 don't have id, so i must input the item name manually. Is it possible to do this in 1 userform?
 
Upvote 0
Ok then this can be an option. Dots (.) are there to represent other code. You can remove them.
VBA Code:
Sub test()
  Dim itemName As String
  With wBook.Sheets("Database")
  'Your other code
  'Your other code
  'Your other code
  .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)
  'Your other code
  'Your other code
  'Your other code
End Sub
 
Upvote 0
Hi, I have tried your code, but it's not working.
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 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

And then appears error messages like this
1697640202047.png
 
Last edited:
Upvote 0
Sorry, it's my bad
VBA Code:
  End If
  End With
Delete End With here
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,151
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