Activation of sheets based on selection of checkbox in userform - excel

I have checkboxes in my User form and based on selection Of ID from Checkboxes, I want to activate the sheets for particular user in my workbook. I came across some portions of the following code but it's not working properly.
Option Explicit
Private Sub Add_Click()
Dim ctrl As Control
For Each ctrl In UserForm1.Controls
If TypeName(ctrl) = "CheckBox" Then
TransferValues ctrl
End If
Next
End Sub
Sub TransferValues(cb As MSForms.CheckBox)
Dim ws As Worksheet
Dim emptyRow As Long
If cb.Value = True Then
'Define the worksheet based on the CheckBox.Name property:
Set ws = Sheets(Left(cb.Name, 1))
emptyRow = WorksheetFunction.CountA(ws.Range("F:F")) + 1
With ws
If Trim(Me.ComboBox3.Value) = "" Or Trim(Me.ComboBox6.Value) = "" Then
MsgBox ("Please enter text in all fields")
Exit Sub
End If
If WorksheetFunction.CountIf(ws.Range("F:F"), ComboBox3.Value) = 0 Or WorksheetFunction.CountIf(ws.Range("G:G"), ComboBox6.Value) = 0 Then
Cells(emptyRow, 6).Value = ComboBox3.Value
Cells(emptyRow, 7).Value = ComboBox6.Value
Cells(emptyRow, 8).Value = TextBox1.Value
Else
MsgBox ("Warning:Duplicate Entries found. Please update the existing entries")
End If
End With
End If
End Sub

Found solution by own. Please use the following code for such kind of issues if anyone face.
Private Sub CommandButton1_Click()
Dim ctrl As Control
For Each ctrl In Userform1.Controls
If TypeName(ctrl) = "CheckBox" Then
TransferValues ctrl
End If
Next
End Sub
Sub TransferValues(cb As MSForms.CheckBox)
Dim ws As Worksheet
Dim emptyRow As Long
'Dim ID As String
If cb.Value = True Then
Set ws = Sheets(Left(cb.Caption, 6))
If Trim(Me.ComboBox3.Value) = "" Or Trim(Me.ComboBox6.Value) = "" Then
MsgBox ("Please Enter the text in All Fields")
End If
emptyRow = WorksheetFunction.CountA(ws.Range("F:F")) + 1
With ws
If WorksheetFunction.CountIf(ws.Range("F:F"), ComboBox3.Value) = 0 Or WorksheetFunction.CountIf(ws.Range("G:G"), ComboBox6.Value) = 0 Then
.Cells(emptyRow, 6).Value = ComboBox3.Value
.Cells(emptyRow, 7).Value = ComboBox6.Value
.Cells(emptyRow, 8).Value = TextBox1.Value
Else
MsgBox ("Warning:Duplicate Entries Found. Please edit existing entries")
End If
End With
End If
End Sub

Related

VBA insert data next row if exists

(VBA Beginner) Row 15 is my first row of the table.
I can add data using a UserForm into Row 15 but i want to if i have data in Row 15 add into the next row (in my case Row 16 always +1) this is the function that im using:
Public Function GetLastRow(TargetWorksheet As Worksheet, ColumnNo As Variant) As Long
If TargetWorksheet Is Nothing Then Exit Function
GetLastRow = TargetWorksheet.Cells(TargetWorksheet.Rows.Count, ColumnNo).End(xlUp).Row
End Function
And this is my add button:
Private Sub CommandButton1_Click()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("OIL_")
Dim GetLastRow As Long
'Validações-----------------------------------------------------
If Me.txtID.Value = "" Then
MsgBox "Insira um ID!", vbCritical
Exit Sub
End If
If Me.txtDesc.Value = "" Then
MsgBox "Insira uma Descrição!", vbCritical
Exit Sub
End If
'---------------------------------------------------------------
If GetLastRow + 1 <= 15 Then GetLastRow = 15 Else GetLastRow = GetLastRow + 1
sh.Cells(GetLastRow, 2).Value = Me.txtID.Value
sh.Cells(GetLastRow, 11).Value = Me.txtDesc.Value
sh.Cells(GetLastRow, 29).Value = Me.txtData.Value
End Sub
For now its only inserting data into my Row 15 and if i try to add another one it just replaces for the new data, i tried a lot of things and i cant manage to add data into the next row always.
Your function is named the same as your variable, this is confusing and if you debug your code you will find the GetLastRow will always equal zero and that is causing your issue. My biggest suggestion is to learn to use the debugger!
You could change your code to something like this:
Private Sub CommandButton1_Click()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("OIL_")
Dim lastRow As Long
'Validações-----------------------------------------------------
If Me.txtID.Value = "" Then
MsgBox "Insira um ID!", vbCritical
Exit Sub
End If
If Me.txtDesc.Value = "" Then
MsgBox "Insira uma Descrição!", vbCritical
Exit Sub
End If
'---------------------------------------------------------------
lastRow = GetLastRow(sh, 3) 'I am not sure what column you need I just used 3 for example
If lastRow + 1 <= 15 Then lastRow = 15 Else lastRow = lastRow + 1
sh.Cells(lastRow, 2).Value = Me.txtID.Value
sh.Cells(lastRow, 11).Value = Me.txtDesc.Value
sh.Cells(lastRow, 29).Value = Me.txtData.Value
End Sub
Well, problem solved this is the right way to do it
Private Sub AddButton_Click()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("OIL_")
Dim lastRow As Long
'Validações-----------------------------------------------------
If Me.txtID.Value = "" Then
MsgBox "Insira um ID!", vbCritical
Exit Sub
End If
If Me.txtDesc.Value = "" Then
MsgBox "Insira uma Descrição!", vbCritical
Exit Sub
End If
'---------------------------------------------------------------
lastRow = GetLastRow(sh, 2)
If lastRow + 1 <= 15 Then lastRow = 15 Else lastRow = lastRow + 1
sh.Cells(lastRow, 2).Value = Me.txtID.Value
sh.Cells(lastRow, 11).Value = Me.txtDesc.Value
sh.Cells(lastRow, 29).Value = Me.txtData.Value
End Sub
And this is the right function:
Public Function GetLastRow(TargetWorksheet As Worksheet, ColumnNo As Variant) As Long
If TargetWorksheet Is Nothing Then Exit Function
GetLastRow = TargetWorksheet.Cells(TargetWorksheet.Rows.Count, ColumnNo).End(xlUp).Row
End Function
Thanks to everyone

How to Call Another Module in VBA Excel

I Have an Issue to call Another Module (Run Time Error'424': Object Required). I have 2 Modules, Module 1 and Module 2. And Below is the code in Module 1 :
Private Sub test()
Dim Work As Worksheet: Set work= Sheets("S_BDN")
For i = 1 To 2
Set f = work.Range("A5", work.Range("A5").End(xlDown))
Set a = f.Find(i, LookIn:=xlValues)
If a.Offset(0, 10).Value = "January" Then
Call Module3.Proceed_B
End If
Next i
End Sub
And Below is the code in Module 2 :
sub Module3.Proceed_B()
If a.Offset(0, 6).Value = "A" Then
Debug.Print a.Offset(0, 4).Value
else
Debug.Print a.Offset(0, 5).Value
end if
end sub
All help is greatly appreciated. Thank you.
I can't see your "fixed" code or your sheet, so here are some generic suggestions for improvement:
Private Sub test()
Dim Work As Worksheet, f As Range, a As Range
Set work= Sheets("S_BDN")
Set f = work.Range("A5", work.Range("A5").End(xlDown)) 'take out of loop
For i = 1 To 2
Set a = Nothing
Set a = f.Find(i, LookIn:=xlValues, lookat:=xlWhole) 'provide *all* relevant parameters
If Not a Is Nothing Then 'make sure you got a match
If a.Offset(0, 10).Value = "January" Then
Proceed_B a 'pass a to the other sub
End If
End If
Next i
End Sub
sub Proceed_B(a As Range)
If a.Offset(0, 6).Value = "A" Then
Debug.Print a.Offset(0, 4).Value
else
Debug.Print a.Offset(0, 5).Value
end if
end sub

Autofill listbox from textbox text VBA

I want a userform where as text is being typed into textbox1, possible values that match the text from the sheet "Data" populate listbox1 and similarly for textbox2 and listbox2. Currently when I type in textbox1, it's filling listbox2 with values from column A in the sheet that is open, instead of the sheet "Data".
Private Sub TextBox1_Change()
Dim i As Integer, ws As Worksheet
ListBox1.Visible = True
Set ws = Sheets("Data")
For i = 2 To Sheets("Data").Range("D6000").End(xlUp).Row
If UCase(Left(ws.Cells(i, 1), Len(TextBox1.Text))) = UCase(TextBox1.Text) Then
ListBox1.AddItem Cells(i, 1)
End If
Next i
End Sub
Private Sub TextBox2_Change()
Dim i As Integer, ws As Worksheet
ListBox2.Visible = True
Set ws = Sheets("Data")
For i = 2 To Sheets("Data").Range("B6000").End(xlUp).Row
If UCase(Left(ws.Cells(i, 1), Len(TextBox2.Text))) = UCase(TextBox2.Text) Then
ListBox2.AddItem Cells(i, 1)
End If
Next i
End Sub
Edit: What I ended up using:
Private Sub TextBox1_Change()
Dim i As Long
Dim arrList As Variant
Me.ListBox1.Clear
If Sheet7.Range("D" & Sheet7.Rows.Count).End(xlUp).Row > 1 And Trim(Me.TextBox1.Value) <> vbNullString Then
arrList = Sheet7.Range("D1:D" & Sheet7.Range("D" & Sheet7.Rows.Count).End(xlUp).Row).Value2
For i = LBound(arrList) To UBound(arrList)
If InStr(1, arrList(i, 1), Trim(Me.TextBox1.Value), vbTextCompare) Then
Me.ListBox1.AddItem arrList(i, 1)
End If
Next i
End If
If Me.ListBox1.ListCount = 1 Then Me.ListBox1.Selected(0) = True
End Sub
Private Sub TextBox2_Change()
Dim i As Long
Dim arrLists As Variant
Me.ListBox2.Clear
If Sheet7.Range("B" & Sheet7.Rows.Count).End(xlUp).Row > 1 And Trim(Me.TextBox2.Value) <> vbNullString Then
arrLists = Sheet7.Range("B1:B" & Sheet7.Range("B" & Sheet7.Rows.Count).End(xlUp).Row).Value2
For i = LBound(arrLists) To UBound(arrLists)
If InStr(1, arrLists(i, 1), Trim(Me.TextBox2.Value), vbTextCompare) Then
Me.ListBox2.AddItem arrLists(i, 1)
End If
Next i
End If
If Me.ListBox2.ListCount = 1 Then Me.ListBox2.Selected(0) = True
End Sub
Use ws.Cells(i, 1) instead of Cells(i, 1).
Also, should the part concerning ListBox2 be moved to TextBox2_Change() ?

Requiring Listbox Selection for userform

Private Sub CommandButton1_Click()
whichSheet = ListBox1.Value
Dim n As Integer
Do
n = n + 1
ListBox1.AddItem Sheets(n).Name
Loop Until n = Worksheets.Count
Worksheets(whichSheet).Activate
Dim lastrow
lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
lastrow = lastrow + 1
Cells(lastrow, 1) = TextBox1
answer = MsgBox("Are you sure you want to add the record?", vbYesNo + vbQuestion, "Add Record")
If answer = vbYes Then
Cells(lastrow, 1) = TextBox1.Text
Cells(lastrow, 2) = TextBox2.Text
Cells(lastrow, 3) = TextBox3.Value
Cells(lastrow, 4) = TextBox4.Text
Cells(lastrow, 5) = TextBox5.Text
Cells(lastrow, 6) = TextBox6.Text
Else
Cells(lastrow, 1) = ""
Exit Sub
End If
End Sub
Private Sub UserForm_Initialize()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
ListBox1.AddItem (ws.Name)
Next ws
End Sub
Hello guys, I am using the code above which works perfectly for my userform. The only issue I'm having is that when someone doesn't pick a choice from the listbox1 and submits the info, the "Runtime Error" window pops up. I would like to stop that from happening by making a message box appear that tells users to make a choice--->click ok on the message box---> and then resume. If the user doesn't select an option still the same procedure should occur every time. If you have any ideas, I would love to try them out. Thanks.
My preferred way of handling this situation is to disable CommandButton1 if nothing in the ListBox1 is selected. Or, in other words, enable the button when something is selected.
Private Sub UserForm_Initialize()
Dim ws As Worksheet
CommandButton1.Enabled = False ' <--- here.
For Each ws In ThisWorkbook.Worksheets
ListBox1.AddItem (ws.Name)
Next ws
End Sub
I don't have Excel open at the moment, but you want a corresponding listbox event to enable/disable the button. The following is an example and untested.
Private Sub ListBox1_Change()
CommandButton1.Enabled = ListBox1.ListIndex <> -1
End Sub
Another approach is to check to see if something has been selected when you enter the CommandButton1_Click routine and handle it there. But I prefer to prevent bad user input in the first place - less complicated.

Excel vba, display value in a textbox that I fetch from a table

I have a table that ranges from F2 to G230 . The F column is employee number and G column is employee name. Im trying to make a simple program that registers food orders for the employees and exports the data into a table. Everything works fine except when I choose a employee from a drop down list I want the employee number to appear in a textbox.
Here is my whole code:
Private Sub cmdbutton_add_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("listi yfir skráningar")
'find first empty row in database
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
'check for a Name number
If Trim(Me.combobox_name.Value) = "" Then
Me.combobox_name.SetFocus
MsgBox "Please complete the form"
Exit Sub
End If
'copy the data to the database
ws.Cells(iRow, 1).Value = Me.textbox_staffnr.Value
ws.Cells(iRow, 2).Value = Me.combobox_name.Value
ws.Cells(iRow, 3).Value = Me.combobox_rettir.Value
ws.Cells(iRow, 4).Value = Me.textbox_verd.Value
ws.Cells(iRow, 5).Value = Me.textbox_dags.Value
MsgBox "Komið :)", vbOKOnly + vbInformation, "Komið"
'clear the data
Me.combobox_name.Value = ""
Me.combobox_name.SetFocus
Unload Me
End Sub
Private Sub Cmdbutton_close_Click()
Unload Me
End Sub
Private Sub ComboBox1_Change()
End Sub
Private Sub combobox_name_Change()
textbox_staffnr.Value = Application.VLookup(Me.combobox_name.Value, Sheet3.Range("F2:G230"), 1, 0)
End Sub
Private Sub combobox_rettir_Change()
textbox_verd.Value = Application.VLookup(Me.combobox_rettir.Value, Sheet3.Range("C2:D23"), 2, 0)
End Sub
Private Sub Name_Click()
End Sub
Private Sub TextBox1_Change()
End Sub
Private Sub textbox_staffnr_Change()
End Sub
Private Sub textbox_verd_Change()
End Sub
Private Sub UserForm_Initialize()
textbox_dags.Value = Format(Date, "dd/mm/yyyy")
End Sub
And here Im getting error:
Private Sub combobox_name_Change()
textbox_staffnr.Value = Application.VLookup(Me.combobox_name.Value, Sheet3.Range("F2:G230"), 1, 0)
End Sub
Please help.
EDIT: From user's comment:
The number is in column 1 and the name is in column 2
In that case, Vlookup won't work, you'll need to use Index/Match, or a Range.Find.Offset. Personally I prefer the Range.Find.Offset while in VBA:
Dim rngFound as Range
Set rngFound = Sheet3.Range("G2:G230").Find(Me.combobox_name.Value, , xlValues, xlWhole)
If not rngFound is Nothing Then textbox_staffnr.Value = rngFound.Offset(0, -1).Text

Resources