So I am currently working on Userforms in Excel to input and edit database of employees in my department.
Something like this:
example of database
Using Userform to input new data works fine. You just sets Cells value in active Sheet to correspond with Value Choosen in the Userform.
My problem appears when I try to edit old data. What I did is basically made a simple search system based on ID#. When I click the Edit button it will store all relevant info to Public Variable I stored in a module called "Var." All variable is stored as String.
Dim rowsearach as Long, Name As String, BranchRegion As String, BranchProvince As String, BranchCity As String, Phone As String
rowsearch = WorksheetFunction.match(Var.editid, Range("A:A"), 0)
Var.Name = Cells(rowsearch, 2).Value
Var.BranchRegion = Cells(rowsearch, 3).Value
Var.BranchProvince = Cells(rowsearch, 4).Value
Var.BranchCity = Cells(rowsearch, 5).Value
Var.Phone = Cells(rowsearch, 6).Value
Now, the storing of those value in the Public variable worked fine, I've tried to write all of them to empty Cells and it worked.
The next step is calling the edit userform (which is the same as userform I used for new data input) in which the content of the userform will be filled with those variables above stored in "Var" module.
Name : Textbox
Branch Region : Listbox, listbox item is updated during Userform initialization
Branch Province : Listbox, listbox item is updated on Branch Region listbox change (the Province shown depends on what Region is chosen)
Branch City : Combo-box, combo-box item is updated on Branch Province listbox change (the City shown depends on what Province is chosen)
Telephone : Textbox
My problem lies within getting the value of Branch Region. Here is my code:
Private Sub UserForm_Initialize
With ListboxRegion
.Additem "West"
.Additem "Central"
.Additem "East"
.Value = Var.Region
End With
My problem is, on the Region section, while the userform seemingly able to import the value from Var.Region as follows (the region is set to Central in this example):pictured. In the picture you could see that while Central section is selected, when I finish the editing through a "Finish Button" in which:
Cells(rowid, 3).Value = ListboxRegion.Value
The value cells in Cells(rowid, 3) will be null/empty. I've tried code it to write the value of ListboxRegion to other cells before clicking "Finish Button", but it also returns empty.
The problem, seemingly, is while Listbox has shown that the Region has been chosen, in actual the value is not stored in ListboxRegion.Value
This is a problem because user might now want to edit the region at all and just edit the other entries, but if they do no re-click the correct Region again, when they finish, the Region part of the database will begone.
I have 0 idea whats wrong with my code or how I implement it.
It may be a simple typo. Var.BranchRegion = Cells(rowsearch, 3).Value is used while storing cell value to var, while .Value = Var.Region is used in UserForm_Initialize
Any way use Debug.Print ListboxRegion.ListIndex at the end of UserForm_Initialize and at the start of Finish Button Event to get some indication of what's going wrong.
Related
Am creating a form in which the user will be able to select 4 checkboxes. Blue, Green, Yellow, Red. They have to do this atleast twice, up to 5 times in total.
The Form Example
The first checkboxes are on the form, the other 4 grouped checkboxes are on multipages.
I've tried giving them a group name "Reeks" to "Reeks4", to then print their values into a single cell separated by comma.
The code I've found so far does print the values neatly, however, it grabs every available checkbox on my form instead of just from the group "Reeks".
The ways I've tried to add the groupname (or tag, named them both the same) to the code, only gives me errors.
For Each Control In Me.Controls
If TypeName(Control) = "CheckBox" Then
'If Control.GroupName = "Reeks" Then
If Control.Value Then
Reeks = IIf(Reeks <> "", Reeks & ", ", "") & Control.Caption
End If
End If
Next
With ws
.Cells(iRow, 5).Value = Reeks
End With
Any help is welcome, but if at all possible please add comments as I only started on VBA last week.
(Side note. I also have framed checkboxes on the multipages in which the user can select the location of said colour. Side + Top view. Which will need to work the same as with the colours, but printed in different columns. )
I have a userform in Excel. On loading, the form pulls data from the last row of a spreadsheet. There is a button on the form to print the form.
Prior to printing the form, it updates the selection in a list box for work order status to "Assigned" and then copies this status to the spreadsheet.
Sometimes the work order status in the spreadsheet is updated to "". It is like the selection of the list box is not being recognized. It is intermittent and I have not been able to determine a pattern.
This is the code
PrintWOForm.LB_WOStatus.value = "Assigned" 'Updates WO status to Assigned
To write this to the spreadsheet I have this code
'Update WO Status to Complete if there is a date in Date Completed
'Else update Status based on Selection in WO Status list box
If PrintWOForm.TB_DateComplete = "" Then
ws.Cells(cRow, 4) = PrintWOForm.LB_WOStatus.value
Else
ws.Cells(cRow, 4) = "Complete"
ws.Cells(cRow, 23) = PrintWOForm.LB_RepairCode.value
End If
It appears when I first open the form and use the print button, which updates the selection in the list box LB_WOStatus to "Assigned", it copies a blank into cRow 4.
If I manually select a status in the list box, that time and every time forward it will work correctly even when the status is selected by the code.
I am not sure this is best the fix for my issue, but so far it appears to work well. I added the below first line of code in front the prexesting second line of code.
Me.LB_WOStatus.SetFocus 'Must SetFocus to the WOStatus list box for the application to realize the next line of code
Me.LB_WOStatus.value = "Assigned" 'Updates WO status to Assigned
I did not try using the SetFocus method previously because I read on one of these websites that this was only used to set the focus to the object for the user of the form.
I'm setting up a spreadsheet for multiple users on my team for testing purposes.
The idea is that a spreadsheet gets passed around and any feedback whether it be a pass or fail is noted on the spreadsheet.
I've currently added validation on certain cells which are red until something is filled in by, let's call them the primary tester.
I've added further validation via VBA to check that all red cells have something entered, otherwise the tab colour will turn red.
My problem is that the spreadsheet then gets accessed by the secondary tester then I want the tab to stay red until they have passed or failed the work (again based on cell validation).
So I think I've found a solution whereby the
In a module I've got:
Public Function UserName()
UserName = Environ$("UserName")
End Function
Range("M5").Value = Environ("username")
In another worksheet I've got:
Set myRange3 = ActiveSheet.Range("P21")
If UserName <> Range("M5").Value Then
If UserName = Range("E15").Value Then
If Application.WorksheetFunction.CountA(myRange3) = 0 Then
ActiveWorkbook.ActiveSheet.Tab.Color = vbRed
Else
ActiveWorkbook.ActiveSheet.Tab.Color = xlColorIndexNone
End If
End If
End If
'M5 = Primary tester
'E15 = Secondary tester
I expect the primary tester to have filled in all their requirements, making the tab turn from Red to neutral.
I would then expect the secondary tester to open up the spreadsheet and notice that a tab has been flagged as Red, meaning they need to add their validation of pass/fail for the tab to go neutral.
The idea is to build a userform that have numerous "lines" of two comboboxes to choose from: one let choose a category (main filed) and the second one (dependent filed) let you choose subcategory for the category user have chosen in main field.
Number of this comboboxes pairs may be variable and chosen by user - for adding or deleting line of two comboboxes there are buttons on the top. When pressing "Add Field" button, userform adds new pair at the bottom and gives each combobox name with ordinal number at the end, e.g.:
Main_field_1, Dependent_field_2;
Main_field_2, Dependent_Field_2
and so on.
Visual example of userform
But there is a problem: if no name of combobox is predetermined, how can a code be written for them?
For example I want to register a change in Main_field to be able to run a code, that will fill in data for Dependent_field, based on Main_field value. But if this comboboxes were created by user with "Add field" button there was no code written for them like:
Private Sub Main_field_1_Change()
Select Case UserForm1.Main_field_1.Value
Case "Category1"
UserForm1.Dependent_field_1.AddItem "SubCategory_1"
UserForm1.Dependent_field_1.AddItem "SubCategory_2"
Case "Category2"
UserForm1.Dependent_field_1.AddItem "SubCategory_88"
UserForm1.Dependent_field_1.AddItem "SubCategory_99"
Case Else
End Select
End Sub
because name of main and dependent fields have variables in them, like Main_field_999 and Dependent_field_999 (the numbers at the end will be identical for each line) and as i understand there is code can exist like this:
Private Sub Main_field_XX_Change()
Select Case UserForm1.Main_field_XX.Value
Case "Category1"
UserForm1.Dependent_field_XX.AddItem "SubCategory_1"
UserForm1.Dependent_field_XX.AddItem "SubCategory_2"
Case "Category2"
UserForm1.Dependent_field_XX.AddItem "SubCategory_88"
UserForm1.Dependent_field_XX.AddItem "SubCategory_99"
Case Else
End Select
End Sub
The question is: how can a code be written to recognize a change in combobox, that have a variable name to then execute different piece of code, based on value in this combobox?
I have a VBA form which is used to enter data on a sheet. I am currently coding the form so as it will load any data already existing in the sheet back into the form.
For simple text strings it works perfectly.
e.g.
ReqSetup.ReqText = Application.Worksheets("Req Sheet").Range("F11").Value
However, I have some combo boxes, that on the form, when they are selected will enter a number in the corresponding cell.
Fail 1. - Run Time Error 380 - Invalid property value.
ReqSetup.MinPerKgCB = Application.Worksheets("Req Sheet").Range("C27").Value
Fail 2.
Dim MinPerKg As Range
Set MinPerKg = Application.Worksheets("Req Sheet").Range("C27")
ReqSetup.MinPerKgCB = MinPerKg
I'm obviously doing something really simple wrong but I can't work out what it is!!
Kind Regards!
I have some combo boxes, that on the form, when they are selected will
enter a number in the corresponding cell
Then you'd need to do the opposite of your code attempt, i.e.:
Worksheets("Req Sheet").Range("C27").Value = ReqSetup.MinPerKgCB.Value
That you'd better wrap inside a check that any combobox value is actually selected :
With ReqSetup.MinPerKgCB
If .ListIndex <> -1 Then Worksheets("Req Sheet").Range("C27").Value = .Value
End With