Import checked checkbox captions into a table - excel

I have a userform from which I want to import values into a predefined table (called "Overzicht").
I have 12 checkboxes (one for each month) and I want one row for each month if that checkbox is checked. I want to put the Caption of the checkbox in one of the columns in the table.
For example, if I check January, February and March, I want to import all values into a table into three rows where the first row the 'Month' columns says "January", the second one "February" etc.
I have code that checks the number of checked checkboxes and creates the same number of rows. I don´t know how to get correct captions in the rows. This is what I have so far:
Private Sub CommandButton1_Click()
Dim rng As Range
Dim newrow As ListRow
Set rng = ThisWorkbook.Worksheets("Kostenoverzicht").Range("Overzicht")
Dim answer As Integer
'check number of rows to insert based on # of checked months
Dim ctl As MSForms.Control
Dim rows As Long
For Each ctl In Kostenoverzicht.Frame2.Controls
If TypeOf ctl Is MSForms.CheckBox Then
If Kostenoverzicht.Frame2.Controls(ctl.Name).Value = True Then
rows = rows + 1
End If
End If
Next
answer = MsgBox("Are you sure you want to continue?", vbQuestion + vbYesNo + vbDefaultButton1, "Zet in overzicht?")
If answer = vbYes Then
rng.Select
Set newrow = Selection.ListObject.ListRows.Add(alwaysinsert:=True)
With ws
For rows = 1 To rows
newrow.Range.Cells(rows, 1).Value = Me.TextBox1.Value
newrow.Range.Cells(rows, 2).Value = Me.CategorieBox.Value
newrow.Range.Cells(rows, 3).Value = Me.SubCategorieBox.Value
newrow.Range.Cells(rows, 4).Value = Me.BankrekeningBox.Value
If OptionButton1.Value = True Then newrow.Range.Cells(rows, 5).Value = "Af" Else newrow.Range.Cells(1, 5).Value = "Bij"
newrow.Range.Cells(rows, 6).Value = Me.TextBox2.Value
newrow.Range.Cells(rows, 7).Value = Me.CheckBox1.Caption
If OptionButton3.Value = True Then newrow.Range.Cells(rows, 8).Value = "Ja" Else newrow.Range.Cells(1, 8).Value = "Nee"
Next
End With
End If
End Sub
It will give the value "January" to all rows.
I need code to replace the newrow.Range.Cells(rows, 7).Value = Me.CheckBox1.Caption to take the caption of the checked checkbox.
I´ve tried to find my answer on several sites.

Nevermind my first answer, I misunderstood and thought it would always go from jan to some other month. But now It seems it could also be just be sept and nov for example. Find old/wrong answer at the bottom.
Your code is increadibly hard for me to understand for some reason. You check how many rows to insert at the top, then all the rows seem to get the same information put into it since cells 1-6 are values from other non variable/dynamic sources? Im unsure if I understand whats happening.What even is ws? It also kind of calls a userform right? I can"t really rebuilt what you have cuz I don`t quite know.
Best shot:
Private Sub CommandButton1_Click()
Dim rng As Range
Dim newrow As ListRow
Set rng = ThisWorkbook.Worksheets("Kostenoverzicht").Range("Overzicht")
Dim answer As Integer
Dim ctl As MSForms.Control
Dim rows As Long
answer = MsgBox("Are you sure you want to continue?", vbQuestion + vbYesNo + vbDefaultButton1, "Zet in overzicht?")
If Not answer = vbYes Then Exit Sub
For Each ctl In Kostenoverzicht.Frame2.Controls
If TypeOf ctl Is MSForms.CheckBox Then
If Kostenoverzicht.Frame2.Controls(ctl.Name).Value = True Then
Set newrow = rng.ListObject.ListRows.Add(alwaysinsert:=True)
newrow.Range.Cells(rows, 1).Value = Me.TextBox1.Value
newrow.Range.Cells(rows, 2).Value = Me.CategorieBox.Value
newrow.Range.Cells(rows, 3).Value = Me.SubCategorieBox.Value
newrow.Range.Cells(rows, 4).Value = Me.BankrekeningBox.Value
If OptionButton1.Value = True Then newrow.Range.Cells(rows, 5).Value = "Af" Else newrow.Range.Cells(1, 5).Value = "Bij"
newrow.Range.Cells(rows, 6).Value = Me.TextBox2.Value
newrow.Range.Cells(rows, 7).Value = ctl.Name 'Or maybe ctl.Caption ?
If OptionButton3.Value = True Then newrow.Range.Cells(rows, 8).Value = "Ja" Else newrow.Range.Cells(1, 8).Value = "Nee"
End If
End If
Next
End Sub
I hope you see what I'm trying to do here and can fix it to your needs /bug fix it. Like I said, I didnt rebuilt a sheet to test it. Maybe it helps you regardless.
Old:
newrow.Range.Cells(rows, 7).Value = Format(3 + 31 * (rows - 1), "MMMM")

This code should go behind your UserForm
Here are some suggestions for when you're coding:
Use option explicit so you don't have unexpected behavior with undefined variables
Always indent your code (see www.rubberduckvba.com a free tool that help you with that)
Try to separate your logic defining variables and the reusing them
Review and customize the code so it fits your needs.
You can see what's happening in the code by adding a stop and pressing F8 and executing it line by line.
Code:
Option Explicit
Private Sub CommandButton1_Click()
ProcessForm
End Sub
Private Sub ProcessForm()
Dim targetControl As MSForms.control
Dim continue As Boolean
continue = MsgBox("Are you sure you want to continue?", vbQuestion + vbYesNo + vbDefaultButton1, "Zet in overzicht?") = vbYes
' If it's not yes
If Not continue Then Exit Sub
For Each targetControl In Me.Controls
If TypeOf targetControl Is MSForms.CheckBox Then
Select Case targetControl.Value
Case True
' Call the RecordData procedure (remember to add the controls in the parameters)
RecordData targetControl.Caption, Me.TextBox1.Value, Me.TextBox2.Value
Case False
' Do something?
End Select
End If
Next targetControl
End Sub
Private Sub RecordData(ByVal recordMonth As String, ByVal textbox01Value As String, ByVal textbox02Value As String)
Dim targetTable As ListObject
Dim newRow As ListRow
' Refer to the table (no need for the worksheet reference)
Set targetTable = Range("Overzicht").ListObject
' Add a new row an set a reference
Set newRow = targetTable.ListRows.Add
' Refer to the columns by their header (replace with yours)
newRow.Range.Cells(1, targetTable.ListColumns("Month").Index).Value = recordMonth
newRow.Range.Cells(1, targetTable.ListColumns("Textbox1").Index).Value = textbox01Value
newRow.Range.Cells(1, targetTable.ListColumns("Textbox2").Index).Value = textbox02Value
End Sub
Let me know if it works or you need more help

Related

VBA Userform posting data twice....sometimes

I have a userform with a combobox on a sheet "PostHistory" that draws it's data from the "Staff" sheet. When you press Add on the userform it's suppose to locate the name on the Staff Sheet and replace the date next to the name. Occasionally, it will replace the date and the date next to the name below it. Using Excel 2016
Private Sub CommandButton7_Click()
Application.ScreenUpdating = False
Sheets("Staff").Visible = True
Sheets("Engine").Visible = True
Dim TargetRow As Integer
Dim nameRange As Range
Set nameRange = Sheets("Staff").Range("C3:C200")
TargetRow = Sheets("Engine").Range("D3").Value
Sheets("PostHistory").Range("B3").EntireRow.Insert Shift:=xlDown
Sheets("PostHistory").Range("B3").Value = txt_date
Sheets("PostHistory").Range("C3").Value = cb_staff
Sheets("PostHistory").Range("D3").Value = txt_post
Sheets("PostHistory").Range("E3").Value = txt_notes
If (Augment.txt_date.Text) = "" Then
GoTo Skip1
ElseIf IsNull(Augment.txt_date.Value) = False Then
End If
For Each cell In nameRange.Cells
If cell.Text = [cb_staff] Then
cell.Offset(0, -1).Value = txt_date
End If
Next
Skip1:
Unload Augment
Sheets("Staff").Visible = False
Sheets("Engine").Visible = False
Sheets("List").Visible = False
Application.ScreenUpdating = True
Augment.Show
End Sub
To start: I didn't find the reason why your code should write more than once. But I believe the code below will not write anything twice.
Private Sub CommandButton7_Click()
' 209
Dim nameRange As Range
Dim Fnd As Range
Dim Ctls() As String
Dim i As Integer
Ctls = Split("txt_Date,cb_Staff,txt_Post,txt_Notes", ",")
If Len(txt_Date) Then
With Worksheets("Staff")
Set nameRange = .Range(.Cells(3, 3), .Cells(.Rows.Count, 3).End(xlUp))
End With
Set Fnd = nameRange.Find(cb_Staff.Value, , xlValues, xlWhole)
If Not Fnd Is Nothing Then Fnd.Offset(0, -1).Value = txt_Date.Value
End If
With Worksheets("PostHistory")
.Rows(3).EntireRow.Insert Shift:=xlDown
With .Rows(3)
For i = 0 To UBound(Ctls)
.Cells(3 + i).Value = Me.Controls(Ctls(i)).Value
Me.Controls(Ctls(i)).Value = ""
Next i
End With
End With
End Sub
In principle, you don't need to unhide a sheet in order to read from or write to it. Also, if the sheet to which you write is hidden, there is no point in stopping ScreenUpdating. Finally, I did like the way you found to clear all controls but believe that it will interfere with your management of the list in the combo box. Therefore I showed you another method above.
Oh, yes. I created a userform called Augment with one combo box, 3 text boxes and one CommandButton7. I hope that is what you also have.

Check duplicate on multiple userform textbox value

Im still working to improve my library booklist. This is previous question to check duplicate value while user entering the details (https://stackoverflow.com/a/60014470/6409413).
While Im testing this, I thinking its better and faster to create a "templist" on sheet2, then click a button on the userform to get all the details instead of typing or copy/pasting into the userform textbox. So,
Private Sub GetButton_Click()
With Me
.TitleTextBox.Value = Cells(ActiveCell.Row, "B").Value
.AuthorTextBox.Value = Cells(ActiveCell.Row, "C").Value
.CopyTextBox.Value = Cells(ActiveCell.Row, "D").Value
.ISBNTextBox.Value = Cells(ActiveCell.Row, "E").Value
.CallNoTextBox.Value = Cells(ActiveCell.Row, "F").Value
.PublicationTextBox.Value = Cells(ActiveCell.Row, "G").Value
End With
''Selection.EntireRow.Font.Strikethrough = True
End Sub
Then, once all the textbox filled, I want to check for the duplicate data on the main list. So, I try something like below:
Private Sub CheckButton_Click()
Dim FoundCell As Range
Dim Search As String
Dim ws As Worksheet
Set ws = Worksheets("booklist")
Search = TitleTextBox.Text
Set FoundCell = ws.Columns(2).Find(Search, LookIn:=xlValues, Lookat:=xlWhole)
On Error GoTo ErrMsg
If FoundCell Is Nothing Then
Title_checker.Caption = ChrW(&H2713)
Else
Title_checker.Caption = "Duplicate" & " " & FoundCell.Address
End If
FoundCell.Select
Exit Sub
ErrMsg:
MsgBox "Select booklist sheet!"
End Sub
Im learned from previous question, Im only can "find" once each time. But, I want to check on three data for the duplicate value which is title on column B, ISBN on column E and CallNo at column F. Im not sure how to implement this answer (https://stackoverflow.com/a/60014470/6409413) on my previous question into my new "CheckButton". COuld someone help?
Using the same Sub from the previous question:
Private Sub CheckButton_Click()
DupCheck TitleTextBox.Text, 2, Title_checker
DupCheck ISBNTextBox.Text, 5, ISBN_checker
DupCheck CallNoTextBox.Text, 6, CallNo_checker
End Sub
Sub DupCheck(txt, ColNo As Long, theLabel As Object)
Dim m
With Worksheets("booklist")
m = Application.Match(txt, .Columns(ColNo), 0)
If Not IsError(m) Then
theLabel.Caption = "Duplicate" & " " & .Cells(m, ColNo).Address
.Activate '<< added: select the sheet
.Rows(m).Select
Else
theLabel.Caption = ChrW(&H2713)
End If
End With
End Sub

Update data with Userform textbox

I have a userform to add or remove inventory from a series of tables.
The issue is when the info from the userform is added to ONLY the "Received" table the quantity cell is not acknowledged. It is populated but is not picked up by the table formulas I have in place.
I can got to the table and manually type the quantities in and hit refresh all on the data tab and the quantities update correctly.
I have a pivot table setup on each of these sheets to get the dates of entry, material, and quantity from the "Received" table the product that was entered via the userform shows a zero for the quantity.*
I have tried several converters and format changes but I am still not getting results.
Cdbl, Cint, Cdec, Format(), Changed formats in the Excel Table in question
Private Sub Add_Button_Click()
MsgBox "Are You Sure You Want To Add To Inventory?"
Unload Me
Transaction_Form.Show
Dim the_sheet As Worksheet
Dim table_list_object As ListObject
Dim table_object_row As ListRow
Set the_sheet = Sheets("MATERIALS REC.")
Set table_list_object = the_sheet.ListObjects(1)
Set table_object_row = table_list_object.ListRows.Add
table_object_row.Range(1, 1).Value = CDate(Me.Date_Box)
table_object_row.Range(1, 2).Value = Me.Material_Code_Box
table_object_row.Range(1, 4).Value = Me.Transaction_Qty_Box
table_object_row.Range(1, 5).Value = Me.Mat_Price_Box
table_object_row.Range(1, 6).Value = Me.cboSupplier_List
table_object_row.Range(1, 7).Value = Me.Order_Date_Box
End Sub
Private Sub cboSupplier_List_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
HookListBoxScroll Me, cboSupplier_List
End Sub
Private Sub Date_Box_AfterUpdate()
On Error Resume Next
Me.Date_Box.Value = CDate(Me.Date_Box)
End Sub
Private Sub Mat_Price_Box_AfterUpdate()
On Error Resume Next
Me.Mat_Price_Box.Value = Format(Me.Mat_Price_Box.Value, "$####.##")
End Sub
Private Sub Material_Code_Box_Change()
On Error Resume Next
Me.Material_Code_Box.Value = StrConv(Me.Material_Code_Box.Value, vbUpperCase)
End Sub
Private Sub Next_Button_Click()
MsgBox "The Transaction Form Will Be Closed"
Unload Me
End Sub
Private Sub Order_Date_Box_AfterUpdate()
On Error Resume Next
Me.Order_Date_Box.Value = CDate(Me.Order_Date_Box)
End Sub
Private Sub Overide_Button_Click()
MsgBox "Are You Sure You Want To Overide"
rspn = InputBox("Enter Password")
If rspn <> "ENVenv11" Then
MsgBox "Wrong password"
End If
Unload Me
Transaction_Form.Show
Dim the_sheet As Worksheet
Dim table_list_object As ListObject
Dim table_object_row As ListRow
Set the_sheet = Sheets("MATERIAL USAGE")
Set table_list_object = the_sheet.ListObjects(1)
Set table_object_row = table_list_object.ListRows.Add
table_object_row.Range(1, 1).Value = Me.Date_Box
table_object_row.Range(1, 4).Value = Me.Material_Code_Box
table_object_row.Range(1, 6).Value = Me.Transaction_Qty_Box
table_object_row.Range(1, 3).Value = Me.Process_Box
table_object_row.Range(1, 2).Value = Me.Customer_Box
End Sub
Private Sub Today_Button_Click()
Date_Box.Value = Date
End Sub
Private Sub Transaction_Qty_Box_AfterUpdate()
On Error Resume Next
Me.Transaction_Qty_Box.Value = CDec(Me.Transaction_Qty_Box.Value)
End Sub
Private Sub Use_Button_Click()
MsgBox "Are You Sure You Want To Remove From Inventory"
Unload Me
Transaction_Form.Show
Dim the_sheet As Worksheet
Dim table_list_object As ListObject
Dim table_object_row As ListRow
Set the_sheet = Sheets("MATERIAL USAGE")
Set table_list_object = the_sheet.ListObjects(1)
Set table_object_row = table_list_object.ListRows.Add
table_object_row.Range(1, 1).Value = Me.Date_Box
table_object_row.Range(1, 4).Value = Me.Material_Code_Box
table_object_row.Range(1, 6).Value = Me.Transaction_Qty_Box
table_object_row.Range(1, 3).Value = Me.Process_Box
table_object_row.Range(1, 2).Value = Me.Customer_Box
End Sub
Private Sub UserForm_Click()
End Sub
Private Sub UserForm_Initialize()
'Populate Supplier combo box.
Dim rngSupplier As Range
Dim ws As Worksheet
Set ws = Worksheets("Lookup_Lists")
For Each rngSupplier In ws.Range("Suppliers")
Me.cboSupplier_List.AddItem rngSupplier.Value
Next rngSupplier
End Sub
When the add button is pushed the quantity cell in the Received table is populated. This happens no issues with it putting data in the table.
The issue is the formulas do not see the data that the form enters into that cell. All other cell data populates and can be seen by various other formulas I have used to test it. It is just the quantity cell.
Here's what I suspect is happening: The input TextBoxes on your userform contain String. That's all they can ever contain. You're attempting to cast that string as numeric here:
Private Sub Transaction_Qty_Box_AfterUpdate()
On Error Resume Next
Me.Transaction_Qty_Box.Value = CDec(Me.Transaction_Qty_Box.Value)
'Confirm my suspicions:
MsgBox TypeName(Me.Transaction_Qty_Box.Value)
End Sub
But then you're passing that Decimal instance back into the TextBox.Value, which implicitly converts it back to String.
So, you've got the right idea, but you're doing it in the wrong place, I think. Try casting here instead:
table_object_row.Range(1, 6).Value = CDec(Me.Transaction_Qty_Box)
Note that this is already what you're doing with the Me.DateBox:
table_object_row.Range(1, 1).Value = CDate(Me.Date_Box)
You can probably remove the _AfterUpdate event handlers and just dump the textbox values to the sheet. Cast them as needed, and apply formatting directly to the cell if needed.

I cant able to pull the information from the main source

I created a userform that will autofill in all the information using the ID# but I cant pull the source from the specific folder, workbook and range.
Here is my code:
Private Sub TextBox4_Change()
Dim rSource As Range
If Not r Is Nothing Then
'// Get value in cell r.row, column 2 into textbox2
TextBox2.Text = Sheet1.Cells(r.Row, 4).Value
ComboBox3.Value = Sheet1.Cells(r.Row, 6).Value
ComboBox4.Value = Sheet1.Cells(r.Row, 8).Value
ComboBox5.Value = Sheet1.Cells(r.Row, 9).Value
End If
End sub
Thank you!
See my answer in the code below (explanation inside the code as comments):
Option Explicit
Private Sub TextBox4_Change()
Dim wb As Workbook
Dim rSource As Range
' === first set the Workbook object ===
' if the workbook (Excel file) is already open >> use the line below
Set wb = Workbooks("Request ID.xlsm")
' if its close, then use the alternative line below
Set wb = Workbooks.Open("\\Path\")
' now use the Find function
Set rSource = wb.Worksheets("Sheet1").Range("A:A").Find(What:=TextBox4.Text, LookAt:=xlWhole, MatchCase:=False)
If Not rSource Is Nothing Then '<-- you need to use the same Range variable you used for the Find
'// Get value in cell r.row, column 2 into textbox2
TextBox2.Text = Sheet1.Cells(rSource.Row, 4).Value
ComboBox3.Value = Sheet1.Cells(rSource.Row, 6).Value
ComboBox4.Value = Sheet1.Cells(rSource.Row, 8).Value
ComboBox5.Value = Sheet1.Cells(rSource.Row, 9).Value
End If
End Sub

Return on Textbox1 value based upon two criterias in combobox and label

hope u're well. Need an expert help after trying a lot without sucess, please.
I have a price list in Sheet1 with 3 columns:
Medical Procedure
Type
Value of Procedure
In a userform, I need to return in Textbox1 the value of the procedure based on the criteria selected in combobox1 (with values that can be found in Medical Procedure column in Sheet1) and the caption in label1 (wich alrealdy is populated with a value that can be encounter in the Type column in Sheet1).
I tried this found here in stackoverflow from the user B Hart (thanks, B Hart!), but I wasn't able to change it to return in a textbox as a numerical value (this vba insert the found value in a listbox instead). Another issue is that the criteria below is in two combobox. I need the two criterias to be in a combobox and another in a label.
Private Sub GetCondStrandValue()
Dim iRow As Long
Dim strValue As String
strValue = vbNullString
If Me.ComboBox1.Value = vbNullString Or Me.ComboBox2.Value = vbNullString Then Exit Sub
With Planilha1
For iRow = 2 To .Range("A65536").End(xlUp).Row
If StrComp(.Cells(iRow, 1).Value, Me.ComboBox1.Value, 1) = 0 And _
StrComp(.Cells(iRow, 2).Value, Me.ComboBox2.Value, 1) = 0 Then
strValue = .Cells(iRow, 3).Value
Exit For
End If
Next
End With
If strValue = vbNullString Then Exit Sub
With Me.ListBox1
'If you only want a single value in the listbox un-comment the .clear line
'Otherwise, values will continue to be added
'.Clear
.AddItem strValue
.Value = strValue
.SetFocus
End With
End Sub
Maybe something like this:
Private Sub combobox1_Change()
Dim lastRow As Integer
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
With Me
For r = 2 To lastRow
If Sheets("Sheet1").Cells(r, 1) = .ComboBox1.Value And Sheets("Sheet1").Cells(r, 2) = .Label1.Caption Then
.TextBox1.Text = Sheets("Sheet1").Cells(r, 3)
Exit For
End If
Next
End With
End Sub

Resources