Listbox value from userform intermittently not updating a cell - excel

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.

Related

Excel VBA - Change tab colours based on user

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.

Excel VBA 2016 - Trying to find name of checkbox, Unable to get value property error

Here's one that I'm stuck on.
I have a macro that opens all workbooks in a folder/directory one at a time, and performs certain actions, one of them is it unchecks every checkbox found in .Range("K25:U56"), but there is one checkbox in that range that I would like it to skip (or what I have it doing now is, store it's current value before the unchecking of all the boxes in the range happens, and then re-instate that value after all the unchecking happens, whichever). I receive "The item with the specified name wasn't found." using the below line:
' Store checkbox50's value to return later
Dim checkbox As Boolean
If sh.CheckBoxes("Check Box 50").Enabled = True Then checkbox = True
If sh.CheckBoxes("Check Box 50").Enabled = False Then checkbox = False
I've also tried:
' Store checkbox50's value to return later
Dim checkbox As Boolean
If sh.Shapes("Check Box 50").OLEFormat.Object.Value = True Then checkbox = True
If sh.Shapes("Check Box 50").OLEFormat.Object.Value = False Then checkbox = False
...and I get "Unable to get the Value property of the CheckBox class." Here is a screenshot of what I'm working with (notice Check Box 50 as the name?).
Any ideas? If you want the full code posted, just leave a comment, but I'm wondering if there's a different way of searching for that one particular checkbox? Thanks!
Thanks to #BigBen, we discovered the issue was because I was looping through all sheets in the workbook, but not every worksheet has a "Check Box 50" in it, so it was looking for something that didn't exist in those secondary sheets. I will work on my code to reflect this. Thanks!!!!

Excel VBA - List Box Value Not Stored

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.

Excel-VBA combo box value on form load

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

VBA listbox issue

I've got an issue with my listbox. In the user form initialize event I'm using the following code to populate it:
RecordSelectionBox.List = WorkingCopy.Worksheets(1).Range("A2:P20").Value
Which works out fine. I have column width adjustments which also work out fine. Once the user has selected a record, a line from the listbox I'm setting the captions of a bunch of labels to the value of the listbox columns. It fills out label captions 1 to 15 just fine. When it hits 16 I get an error "Could Not Get the Column Property. Invalid Argument" "Run-time error '-2147024809 (80070057)'"
Here is the code:
Explanation.Caption = RecordSelectionBox.Column(16)
a debug.print of RecordSelectionBox.ColumnCount shows that I indeed have 16 columns. The explanation field is the longest of the fields I'm using, but I'm not sure that I see how that would become an issue. If anyone has an idea, I'm all ears.
That is because the First Column of the listbox starts with 0
Your first Label should be
Label1.Caption = RecordSelectionBox.Column(0)
and the 16th should be
Explanation.Caption = RecordSelectionBox.Column(15)

Resources