Excel-VBA combo box value on form load - excel

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

Related

Extracting time phased work effort to excel

Has anyone been able to get the time-phased work effort VBA extract macro by Jack Dalhgren, its quite an old macro.
I've been able to create the form and the macro works for the default timephase which is weeks, but if I select another say months I get an error on this line of code. (runtime error 13, type mismatch)
Set pTSV = ActiveProject.ProjectSummaryTask.TimeScaleData(tbstart.Value, tbend.Value, , cboxTSUnits.Value)
http://zo-d.com/blog/archives/programming/analyze-microsoft-project-resource-usage-data-in-excel.html
Thank you
The comboBox in the code uses 2 columns, the text and the internal representation which is a number. The code uses predefined constants for it like pjTimescaleWeeks. The call to TimeScaleData expects this number as parameter.
It is likely that you get the text (a String) as result from the comboBox, (eg "Week"), and this causes the Type mismatch.
You can specify that you want the value of the 2nd column as Value from the combobox. You can do this using the VBE form designer, set property BoundColumn to 2, or you can do this in you code:
Sub fillTSUnitsBox()
...
cboxTSUnits.List = myArray
cboxTSUnits.BoundColumn = 1
cboxTSUnits.Value = 3
End Sub

Listbox value from userform intermittently not updating a cell

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.

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.

How to use Match (and replace) function when the source you are matching with is a dynamic (textbox) value (in userforms)?

I am extremely new to vba code/coding of any sort, and am trying to develop a loaning system for equipment within my discipline using excel sheets and forms.
Essentially, I want a cmdbutton_click to update the availability status of the specific piece of equipment to "On Loan" within the inventory list spreadsheet. The problem I am having however, is with the fact that the form user will enter the ItemID (number) which obviously will always be different. The item ID is the value of which I am looking to use the Match function with.
I have tried to refer to the form's ItemID text box using the match function, but it does not work whatsoever - I understand this is probably completely incorrect.
Set ws2 = Worksheets("MajorInventoryList")
ws2.Cells(WorksheetFunction.Match(me.txtID.value), 10) = "On Loan"
This brings about the Compile error: 'Argument not optional'
Is there any way to ameliorate this?
Use Find like this:
Set ws2 = Worksheets("MajorInventoryList")
Set fnd = ws2.Range("A:A").Find(Me.txtID.Value)
If Not fnd Is Nothing Then ws2.Cells(fnd.Row,10).Value = "On Loan"
This will look for Text Input in Column A and if found, will place "On Loan" in column J of that row in which the value exists.

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!!!!

Resources