Return the value from a checklist on a UserForm - excel

This makes a UserForm checklist and it works.
Sub UserForm_Initialize()
Dim LastRow As Long
Dim i As Long
Dim chkBox As MSForms.CheckBox
LastRow = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To LastRow
Set chkBox = Me.Controls.Add("Forms.CheckBox.1", "CheckBox_" & i)
chkBox.Caption = Worksheets("Sheet1").Cells(i, 1).Value
chkBox.Left = 5
chkBox.Top = 5 + ((i - 1) * 20)
Next i
End Sub
I want to select as many of the boxes as I need, hit a command button and have the resulting values pasted in a different cell.
For example I have 1-10 in cells A1-A10. The first piece makes a checklist for each value 1-10. If I check the boxes next to 2, 3, 5, and 7, hit the command button and then want 2, 3, 5, and 7 to be entered into cells G2, G3, G5 and G7.
I cant figure out how to get this last part to happen. I have tried to make If statement
Sub CommandButton1_Click()
If chkBox1 = False Then GoTo Here
Else
Range("G1").Value = Me.TextBox1.Text
End If
Here
End Sub
I get
"Compile error: Variable not defined"
I tried different names instead of "chkbox1" but get the same error.

I think this does what you want.
You can reference the controls using their name and loop through them in a similar way to the Initialize code. By declaring 'LastRow` before the sub we can use it in both subs.
Dim LastRow As Long
Private Sub CommandButton1_Click()
Dim i As Long
For i = 1 To LastRow
If Me.Controls("CheckBox_" & i) Then
Range("G" & i).Value = i
End If
Next i
End Sub
Sub UserForm_Initialize()
Dim i As Long
Dim chkBox As MSForms.CheckBox
LastRow = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To LastRow
Set chkBox = Me.Controls.Add("Forms.CheckBox.1", "CheckBox_" & i)
chkBox.Caption = Worksheets("Sheet1").Cells(i, 1).Value
chkBox.Left = 5
chkBox.Top = 5 + ((i - 1) * 20)
Next i
End Sub

Related

what if i double click on the listbox, then it displays the value in the userform

so the listbox is in the userform3. after I do a data search, it will appear in the listbox. when I double click on the listbox, it will show the value of the textbox in userform2. i used this code but it didn't work
Dim r, lr as integer
lr= sheet7. Cells(rows.count, 2).end(xlup).row
For r=2 to lr
If sheet7.cells(r, 1).value=listbox1.list(listbox1.listindex, 1) and sheet7.cells(r, 2).value=listbox1.list(listbox1.listindex, 2) then
Unload me
Userform2.Textbox2.value = sheet7.cells(r,1)
End if
End with
Next r
Why that code isn't working? Fyi there are 2 kriteria
too little context for the code you showed
maybe this will help you get on the route
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
'define the criteria parameters
With Me ' reference the parent Userform object
With .ListBox1
Dim refVal1 As String, _
refVal2 As String
refVal1 = .List(.ListIndex, 1)
refVal2 = .List(.ListIndex, 2)
End With
End With
Dim r As Long, lr As Long
With Sheet7 ' reference "sheet7" sheet
lr = .Cells(.Rows.Count, 2).End(xlUp).Row
For r = 2 To lr
If .Cells(r, 1).Value = refVal1 And .Cells(r, 2).Value = refVal2 Then
UserForm2.TextBox2.Value = .Cells(r, 1) ' hoping UserForm2 has no "Initialize" event active...
Exit For
End If
Next
unload me 'are you sure you want to unload the parent userform?
End With
End Sub

Excel vba simple textbox insert sub error

I'm making a very easy application to insert names and some other info , and I'm getting a problem in the sub. I don't know what's happening , been a long time since I used vba ....
Private Sub button_Click()
Dim linha As Long
linha = Worksheets("FAMINHO_ESCOLAS").cell(Rows.Count, 1).End(xlUp).Row + 1
Range("A" & linha).Value = boxname.Value
Range("B" & linha).Value = boxinstr.Value
Range("C" & linha).Value = boxescola.Value
Range("D" & linha).Value = boxtel.Value
Range("E" & linha).Value = boxemail.Value
End Sub
I'm getting error 438
I'm trying to return the values , when i press the "buttonright" it changes to the next data , and when i press buttonleft it shows me previous data and so on
Private Sub CommandButton1_Click()
GetFAMINHO_ESCOLASLastRow boxname1.Value, boxinstr1.Value, boxescola1.Value,
boxtel1.Value, boxemail1.Value
End Sub
Function GetFAMINHO_ESCOLASLastRow() As Range
Dim Target As Range
With Worksheets("FAMINHO_ESCOLAS")
Set Target = .Cells(.Rows.Count, 1).End(xlUp)
Set Target = Intersect(Target.EntireRow, Target.CurrentRegion)
End With
Set GetFAMINHO_ESCOLASLastRow = Target
End Function
linha is set to the last row but LR is the variable that is actually used for the last row.
linha = Worksheets("FAMINHO_ESCOLAS").Cell(Rows.Count, 1).End(xlUp).Row + 1
Cell( should be changes to Cells(.
linha = Worksheets("FAMINHO_ESCOLAS").Cells(Rows.Count, 1).End(xlUp).Row + 1
It would be better to qualify Rows.Count to the worksheet.
I prefer to write a separate sub routine to add the values. In this way, I can test the code without having to instantiate a userform.
Alternative Solution
Note: AddRowToFAMINHO_ESCOLAS will accept anywhere from 1 to 69 values.
Private Sub button_Click()
AddRowToFAMINHO_ESCOLAS boxname.Value, boxname.Value, boxinstr.Value, boxescola.Value, boxtel.Value, boxemail.Value
End Sub
Sub AddRowToFAMINHO_ESCOLAS(ParamArray Args() As Variant)
With Worksheets("FAMINHO_ESCOLAS")
.Cells(.Rows.Count, 1).End(xlUp).Offset(1).Resize(1, UBound(Args) + 1).Value = Args
End With
End Sub
AddRowToFAMINHO_ESCOLAS Demo
Addendum
This function will return the last row with values in column A.
Function GetFAMINHO_ESCOLASLastRow() As Range
Dim Target As Range
With Worksheets("FAMINHO_ESCOLAS")
Set Target = .Cells(.Rows.Count, 1).End(xlUp)
Set Target = Intersect(Target.EntireRow, Target.CurrentRegion)
End With
Set GetFAMINHO_ESCOLASLastRow = Target
End Function
You can test this function by entering the following code into the Immediate Window:
Application.Goto GetFAMINHO_ESCOLASLastRow
Response to Question Update
I changed things up a bit because the OP wants to write and retrieve the values.
Private Sub buttonleft_Click()
Dim Target As Range
Set Target = GetFAMINHO_ESCOLASLastRow
With Target
boxname.Value = .Cells(1, 1).Value
boxinstr.Value = .Cells(1, 2).Value
boxescola.Value = .Cells(1, 3).Value
boxtel.Value = .Cells(1, 4).Value
boxemail.Value = .Cells(1, 5).Value
End With
End Sub
Private Sub buttonright_Click()
Dim Target As Range
Set Target = GetFAMINHO_ESCOLASNewRow
With Target
.Cells(1, 1).Value = boxname.Value
.Cells(1, 2).Value = boxinstr.Value
.Cells(1, 3).Value = boxescola.Value
.Cells(1, 4).Value = boxtel.Value
.Cells(1, 5).Value = boxemail.Value
End With
End Sub
Function GetFAMINHO_ESCOLASLastRow() As Range
Dim Target As Range
With Worksheets("FAMINHO_ESCOLAS")
Set Target = .Cells(.Rows.Count, 1).End(xlUp)
Set Target = Intersect(Target.EntireRow, Target.CurrentRegion)
End With
Set GetFAMINHO_ESCOLASLastRow = Target
End Function
Function GetFAMINHO_ESCOLASNewRow() As Range
Set GetFAMINHO_ESCOLASNewRow = GetFAMINHO_ESCOLASLastRow.Offset(1)
End Function

Combo box drop down method in vba calling change method again and again

Part 1: A combo box in sheet1 to dynamically list unique values from a column in sheet2 put it in the drop down list
Part 2: Display the related entries of sheet2 in sheet1 based on the combo box selection.
I have done the part 1 in a method call fillCombo and have called it under ComboBox1_DropButtonClick() and part 2 under the method ComboBox1_Change()
First time when i click dropdown arrow of combo box it lists the unique entries and on making the selection in it, the related entries are displayed in sheet1 and everything is fine.
During the next selection of dropdown arrow it goes to ComboBox1_DropButtonclick() method then ComboBox1_change() method, ComboBox1_change() method without the dropdown list appearing and me selecting
So it works correctly only in the first instance.
Can you please correct the error.
Private Sub ComboBox1_Change()
Dim sht2, sht1, a As Long, X As Long, i As Long
Dim Lastrow As Long
Set sht1 = Worksheets("Sheet1")
Set sht2 = Worksheets("Sheet2")
a = sht2.Cells(Rows.Count, 1).End(xlUp).Row
X = 8
Lastrow = sht1.Range("D" & Rows.Count).End(xlUp).Row
sht1.Range("G8:J" & Lastrow).Clear
For i = 2 To a
If sht2.Cells(i, 3).Value = "Payments" Then
sht2.Cells(i, "C").Resize(1, 4).Copy sht1.Cells(X, "G")
X = X + 1
End If
Next
sht1.Select
sht1.Cells(1, 1).Select
End Sub
Private Sub ComboBox1_DropButtonClick()
Call fillCombo
End Sub
Sub fillCombo()
Dim ws2 As Worksheet
Set ws2 = ThisWorkbook.Sheets("Sheet2")
Group = 3
firstTime = True
strValue = Sheet1.ComboBox1.Value
'last row
wsLR = ws2.Cells(Rows.Count, 1).End(xlUp).Row
'loop thru rows
For l = 2 To wsLR
If ws2.Cells(l, Group) <> "" And (InStr(uE, "|" & ws2.Cells(l, Group) & "|") = 0) Then
If firstTime = True Then
firstTime = False
uE = "|" & uE & ws2.Cells(l, Group) & "|"
Else
uE = uE & ws2.Cells(l, Group) & "|"
End If
End If
Next l
dropValues = Split(uE, "|")
Sheet1.ComboBox1.Clear
For Each cell In dropValues
If cell <> "" Then
Sheet1.ComboBox1.AddItem cell
End If
Next cell
Sheet1.ComboBox1.Value = strValue
End Sub

VBA obtaining checkbox values from userform

I'm having trouble obtaining values from checkboxes of a userform. The problem I have is that the userform creates a variable number of checkboxes based on a value from a sheet. The code for this:
Private Sub UserForm_Initialize()
Dim LastRow As Long
Dim i As Long
Dim Teller As Long
Dim chkBox As MSForms.CheckBox
Teller = 1
LastRow = Worksheets("Sheet").Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To LastRow
If Worksheets("Sheet").Cells(i, 1).Value = Worksheets("Sheet").Range("S1").Value Then
Set chkBox = Me.Controls.Add("Forms.CheckBox.1", "CheckBox_" & Teller)
chkBox.Caption = Worksheets("Sheet").Cells(i, 9).Value
chkBox.Left = 5
chkBox.Top = 25 + ((Teller - 1) * 20)
Teller = Teller + 1
End If
Next i
End Sub
So this creates a number of checkboxes named CheckBox_1, CheckBox_2 etc.
The problem is when I try to get the value for CheckBox_1 in the module, CheckBox_1 has not yet been created so I'm not able to use it.
Dim x as String
With UserForm4
.Show
x = .CheckBox_1
MsgBox (x)
End
End With
You'll need to loop through .Controls the textbox is not a property on your form.

Public/global range does not seem to be available all the time

I want to have the worksheet run a macro when specific cells are changed.
I have used the code below to initialise the range that determines which cells will cause the macro to be run, but it seems that the lifetime of this range is not of the application?
Public ChangeCellList As Range
Private Sub Workbook_Open()
With Sheets("Program")
For i = 7 To .Cells(Rows.Count, "E").End(xlUp).Row
If Not IsEmpty(.Cells(i, "E")) Then
If ChangeCellList Is Nothing Then
Set ChangeCellList = .Range("E" & i)
Else
Set ChangeCellList = Union(ChangeCellList, .Range("E" & i))
End If
End If
Next i
End With
End Sub
Possible problems I would be grateful for advice on:
1) Correct place to declare public variable (eg module1/this workbook/sheet1?)
2) I presume it is a bad idea, but would it kill performance too much if I just initialised this range in Worksheet_Change() sub?
This is the code where the error occurs:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Fill As Long
Dim StartWeek As Integer
Dim Duration As Integer
'***Error due to ChangeCellList is nothing***
If Not Application.Intersect(ChangeCellList, Range(Target.Address)) _
Is Nothing Then
With Sheets("Program")
Fill = Sheets("macroData").Range("C1").Interior.Color
StartWeek = InputBox("Please enter the Start Week of this Activity", "Start Week")
Duration = InputBox("Please Enter the Duration of this Activity", "Duration")
StartCol = StartWeek + 9
For k = 0 To Duration - 1
.Cells(Target.Row, StartCol + k).Value = 1
.Cells(Target.Row, StartCol + k).Interior.Color = Fill
.Cells(Target.Row, StartCol + k).Font.Color = Fill
Next k
End With
End If
End Sub

Resources