Add info from a form to a table - excel

I have list of fields (on a sheet) in EXCEL document and a table. I need to add all info from input fields to this table after click the button. I need to write code of event on VBA. Can someone help with exemple how to do this?
Here ia an example of my table:

Demo of a method for doing this
Table name List1
Text Boxes names TextBox1 and TextBox2
Button name CommandButton1
Button click code
Private Sub CommandButton1_Click()
Dim lst As ListObject
Dim rng As Range
Set lst = Me.ListObjects("List1")
lst.Range.Activate
Set rng = lst.InsertRowRange
rng.Cells(1, lst.ListColumns("Item A").Index) = TextBox1.Value
rng.Cells(1, lst.ListColumns("Item B").Index) = TextBox2.Value
End Sub
EDIT
If the List is on another sheet use this version
Private Sub CommandButton1_Click()
Dim lst As ListObject
Dim rng As Range
Dim lstRow As ListRow
Set lst = Me.Parent.Worksheets("Sheet2").ListObjects("List1")
Set lstRow = lst.ListRows.Add
Set rng = lstRow.Range
rng.Cells(1, lst.ListColumns("Item A").Index) = TextBox1.Value
rng.Cells(1, lst.ListColumns("Item B").Index) = TextBox2.Value
End Sub

Related

Trying to use ComboBox to use supplier name and populate form in other sheet

on one sheet I have a list of suppliers and their details, I have a userfrom containing a combobox that automatically populates from the list of suppliers. In the columns next to the suppliers, I have details with address, phone number etc. What I am attempting to do is after the user makes the selection, I would like the code to take the details in the adjacent columns and fill in the form. I have tried using the lookup function however I am constantly being given an error stating that the object could not be found. Below is what I have so far
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Set ws = Worksheets("RFQ Information")
'Take supplier name from combobox
'Copy row data in supplier sheet and paste (transposed) into form
Dim xRg As Range
Set xRg = Worksheets("Suppliers").Range("A2:H15")
Set Cells(53, 1) = Application.WorksheetFunction.VLookup(Me.ComboBox1.Value, xRg, 2, False)
Unload Me
End Sub
Private Sub UserForm_Initialize()
Dim SupplierName As Range
Dim SupSheet As Worksheet
Dim tbl As ListObject
Dim SupArray As Variant
Dim SupString As String
Set SupSheet = Sheets("Suppliers")
Set tbl = SupSheet.ListObjects("Table1")
Set SupplierName = tbl.ListColumns(1).DataBodyRange
SupArray = SupplierName.Value
ComboBox1.List = SupArray
UserForm1.Show
MsgBox ("done")
End Sub
I would recommend using the ComboBox Change event instead of a button, since you want the info on list selection. You can also take advantage of the ComboBox.ListIndex property to get the selected item's location in the list, and then use that to get adjacent values from your data table. Here's a quick example of how to do so:
Private Sub ComboBox1_Change()
Dim wb As Workbook: Set wb = ThisWorkbook
Dim wsSup As Worksheet: Set wsSup = wb.Worksheets("Suppliers")
Dim rData As Range: Set rData = wsSup.ListObjects("Table1").DataBodyRange
Dim i As Long: i = Me.ComboBox1.ListIndex + 1
If i = 0 Then Exit Sub 'Nothing selected
'Second number is the column
' Column 1 is the Supplier
' Column 2 is the next column (phone maybe?)
' Column 3 is the column after that (address maybe?)
MsgBox rData.Cells(i, 2) & Chr(10) & _
rData.Cells(i, 3)
'Load the values you want into the necessary form controls
End Sub

How to link each items in a combobox to a specific row?

I am creating a userform that is entering data on a form. And in that userform I have a Combobox that list out all the products. For each product there a row and the data that is being inputted in would only be on that row.
Private sub cwbSave_Click()
Dim ws as Worksheet
Set ws = ActiveWorkbook.ActiveSheet
with ws
Select Case cbProduct
Case Is = "Apple"
With ActiveSheet.Range("A14:P14")
.Font.Bold = True
End With
ws.Cell(14,4) = Me.tbPrice
ws.Cell(14,5) = Me.tbColor
ws.Cell(14,6) = Me.tbSell
Case Is = "Pineapple"
With ActiveSheet.Range("A15:P15")
.Font.Bold = True
End With
ws.Cell(15,4) = Me.tbPrice
ws.Cell(15,5) = Me.tbColor
ws.Cell(15,6) = Me.tbSell
End Select
End With
End Sub
But the thing is, I got like 30 products. And it a lot of manually putting in. I was wondering if there an easier way to code this.
Thank you
There are several ways to do this.. here is one:
In the UserForm_Initialize, add the below code:
Private Sub UserForm_Initialize()
Dim aValues As Variant: aValues = WorksheetFunction.Transpose(ThisWorkbook.Worksheets("Sheet2").Range("A2:A5")) ' Change sheet name and range to where your products are
Dim iRow As Long
' Clear combobox
Me.cmbCmbBox.Clear
' Fill combobox with the values from product range
For iRow = LBound(aValues) To UBound(aValues)
Me.cmbCmbBox.AddItem aValues(iRow)
Next
End Sub
Above code uses your product range to populate the combobox. Now in cmbCmbBox_Change, add the following code:
Private Sub cmbCmbBox_Change()
Dim oWS As Worksheet: Set oWS = ThisWorkbook.Worksheets("Sheet2")
Dim rProdRange As Range: Set rProdRange = oWS.Range("A2:A5")
Dim rItemRange As Range
' Find the selected item
Set rItemRange = rProdRange.Find(Me.cmbCmbBox.Value)
' Set value in the sheet
If Not rItemRange Is Nothing Then
oWS.Cells(rItemRange.Row, 4) = Me.tbPrice
oWS.Cells(rItemRange.Row, 5) = Me.tbColor
oWS.Cells(rItemRange.Row, 6) = Me.tbSell
End If
End Sub
You can add validation for when product is not found

textbox shows date as number while vlookuping

I've created a userform with listbox that contains several columns and textbox. The textbox value changes when an item in listbox is selected via Application.WorksheetFunction.Vlookup. but instead of date I get the number in textbox. Can someone help me to fix this problem?
Private Sub ListBox_Change()
Dim lnItem As Long
Dim ws, ws2 As Worksheet
Dim rn2 As Range
Set ws = Workbooks("TOOLS").Worksheets("TOOLS")
Set ws2 = Workbooks("TOOLS").Worksheets("CONSOLIDATE")
Set rn2 = ws2.Range("JOURNAL1")
Me.TextPurchaseDate.Value = Application.WorksheetFunction.Vlookup(Me.ListBox.List(lnItem, 0), rn2, 3, 0)
End Sub
Dates are stored as numbers in Excel - what you see in a cell is a formatted version of that - you can use Format() to get a text version if that's what's needed
Private Sub ListBox_Change()
Dim lnItem As Long
Dim ws, ws2 As Worksheet
Dim rn2 As Range, res
Set ws = Workbooks("TOOLS").Worksheets("TOOLS")
Set ws2 = Workbooks("TOOLS").Worksheets("CONSOLIDATE")
Set rn2 = ws2.Range("JOURNAL1")
res = Application.Vlookup(Me.ListBox.List(lnItem, 0), rn2, 3, 0)
Me.TextPurchaseDate.Value = Format(res, "mm/dd/yyyy") 'for example
End Sub

How can I add a row with buttons to an excel table using VBA?

Currently I'm using a form to add values to a table in excel:
Private Sub btnSave_Click()
Dim lo As Excel.ListObject
Dim newRow As Excel.ListRow
Dim sheetName As String, tableName As String
sheetName = "Dados"
tableName = "articles"
Set lo = getTable(sheetName, tableName)
Dim ref As Integer
ref = getMaxRef(tableName)
Dim btnEdit As Button, btnDelete As Button
'How can I add this 2 buttons to the newRow?
Set newRow = lo.ListRows.Add(AlwaysInsert:=True)
newRow.Range = Array(ref, cboStores.Value, cboTypes.Value, cboMaterials.Value, txtDescription.Value, txtWeight.Value, txtPrice.Value)
Unload Me
End Sub
Is it possible to add the buttons directly in the newRow or do I have to search for the cells after adding the row and then add the buttons?
Here's general code. It aligns button's dimensions with dimensions of A1 cell:
Sub Test()
Dim btn As Button
With Range("A1")
Set btn = Sheet1.Buttons.Add(.Left, .Top, .Width, .Height)
End With
End Sub

VBA: Userform initialize method not being hit when userform initializes - UserForm.UserForm_Activate solution is not working

I have a button on my worksheet where on click I have a assigned macro which calls the userform.show. See below:
Sub newjoin()
UserForm.UserForm_Activate
NewJoinerEntry.Show
End Sub
I've added the UserForm.UserForm_Activate looking at you above post.
Firstly I'm getting an error on UserForm.UserForm_Activate Run-time error '424' : Object required.
Additionally In my user form I have:
Public Sub UserForm_Initialize()
Dim cntr As Integer
cntr = Application.WorksheetFunction.CountA(Sheets("SITES").Range("B2:B65536"))
Combosite.Clear
For obt = 2 To cntr
Me.Combosite.AddItem Cells(obt, 5)
Next obt
End Sub
which I'm using to populate the combobox values & it's not happening.
On the user form I have other code of on command button click to submit the form results and it's all fine there only if the combo box get the data when userform initializes.
You can loop in your range and add the values in a ComboBox like this:
Private Sub UserForm_Initialize()
Dim rng As Range
Dim SelectedRange As Range
Dim LastRow As Long
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("SITES")
LastRow = sh.Range("B" & Rows.Count).End(xlUp).Row
Set SelectedRange = sh.Range("G2:G" & LastRow)
ComboBox1.Clear
For Each rng In SelectedRange
ComboBox1.AddItem rng.Value
Next rng
End Sub
This code will set a dynamic range in your sheet based on column B. As I saw in your original code, you want the values from column G. So, the SelectedRange is getting the the values from column G and putting it in a ComboBox.

Resources