VLookup in VBA userforms - string lookup value - vba

I have got an issue with a vlookup function in VBA userform. Using below code it only seems to work when it is numeric value, however I have got values like G9000 which when selected throw error back I have tried to cast it as string but mismatch error comes up. There is multiple threads on the subject online, however it doesn't seem to work in my case. I am using this to update value in one of the boxes using change event. What am I doing wrong?
Private Sub cb_safety_observers_id_Change()
With Me
.tb_safety_observersName = Application.VLookup(Val(Me.cb_safety_observers_id), employeeSheet.Range("emp_list"), 2, 0)
End With
End Sub
.

Related

Getting a Run-time error 424 on command button click for MS Access VBA code

I created a button on an Access form that changes the value of column New_column to "YES" but I keep getting a run-time error 424 Object Required. Here's the code generating the error.
Private Sub Command1_Click()
New_column.Value = "YES"
End Sub
But if I simply change the column name to another one that accepts text (e.g., Old_column) I don't get the error.
Private Sub Command1_Click()
Old_column.Value = "YES"
End Sub
What is the difference between New_column and Old_column that would cause one to generate the error but not the other? Both are text fields that already contain text values. I compared the properties for both fields and didn't see any differences. I added New_column using an update query after some original VBA code was written, if that's a clue.

How to make my module move an object using value set in UserForm/

I'm trying to automatize some processes I often do at work. Unfortunately I have no clue about programming so I've been struggling a lot with this.
I have an userform that has a textbox where you're supposed to type in how much you want the object to be moved on X axis. There are multiple objects and I can't wait to experiment how to move them all in proper direction, but the issue is, I can't even move one.
I created a variable, but I can't seem to be able to use it with Object.Move.
Right now VBA tells me to assign the values to an array, so I did that, but I can't put variables into an array apparently.
I've tried simply declaring the variable as public, calling it let's say for example "Value", and using Object.Move (Value), 0
or
Object.Move Value(), 0
Tried both, because I was not sure which one is correct.
After many trials I finally skipped most of the previous errors I was getting, and now I'm stuck at trying to set up an Array using a variable from the UserForm.
Here's the code inside Userform
VBA
Public Sub TextBox1_Initialize()
M = TextBox1
Unload M
End Sub
And here's the code inside the module
VBA
Public M As Integer
Sub Move()
Dim XY(M, 0) As Integer
Object.Move XY
End Sub

invalid outside procedure Error In Access?? Looked at other questions on this site but still do not understand

My code in vba for access was working fine, I had procedures that I wrote for different events that all worked. Then when I went to give my combo boxes and text boxes an assigned row source and now nothing works. I get this invalid outside procedure error. So after hours of trouble shooting I ended up deleting the entire form and then rebuilding it and I still get the same error. So now I just working with one event just until I can get things figured out.
Public Sub cb_op1_AfterUpdate()
tb_LbrRate1.Value = DLookup("LaborRate", "tbloperationsType", "[operationsID] = cb_op1.value")
End Sub
This use to work but now it won't?? tb_LbrRate1.value is a text box and cb_op1.value is a combo box.
I have tried this too
Public Sub cb_op1_AfterUpdate()
End Sub
I left everything blank and I still get the error when I change a value in my combo box?? I just don't get it!!!! any help would be appreciated.
Can you do this:
and check what you get?
This error in MS Access is usually because of code flying not between Sub and End Sub.

Why does my comparator not work after first loop?

I have a pretty simple If Else statement in my code that looks like this:
If GUI.editedFH = "" Then
fhNumField.Value = fhNumField.List(0)
Else
fhNumField.Value = editedFH
End If
This block of code is in a UserForm_Initialize() sub that I call fairly often after users use the form to edit or delete rows of data. I unload the form and reload it as a way to "refresh" the data presented in the form.
The code fails at the first line in that If statement. VBE throws this error
Run-time error '1004':
Application-defined or object-defined error
GUI.editedFH is a global string that is declared in a separate module. GUI is the name of the module, and editedFH is the string variable name. I put a watch on GUI.editedFH and I'm getting String as the type and the value that I want.
When I hardcode string values in, instead of GUI.editedFH, it still doesn't work i.e.
If "abc" = "" Then
'....
End If
The part that is most confusing is that I can compare strings like this elsewhere, and it is seemingly random on when the error will throw. It will work the first time through the initialize and a few subsequent times, but eventually it will throw an error.

VBA UDF fails to call Range.Precedents property

I am trying to write a simple UDF, which is supposed to loop through each of the cells of a given_row, and pick up those cells which do not have any precedent ranges, and add up the values.
Here is the code:
Public Function TotalActualDeductions(given_row As Integer) As Integer
Total_Actual_Deduction = 0
For present_column = 4 To 153
Set precedent_range = Nothing
If Cells(2, present_column) = "TDS (Replace computed figure when actually deducted)" Then
On Error Resume Next
Set precedent_range = Cells(given_row, present_column).Precedents
If precedent_range Is Nothing Then
Total_Actual_Deduction = Total_Actual_Deduction + Cells(given_row, present_column)
End If
End If
Next
TotalActualDeductions = Total_Actual_Deduction
End Function
If I try to run it by modifying the top declaration, from:
Public Function TotalActualDeductions(given_row As Integer) As Integer
to:
Public Function TotalActualDeductions()
given_row = 4
then it runs successfully, and as expected. It picks up exactly those cells which match the criteria, and all is good.
However, when I try to run this as a proper UDF from the worksheet, it does not work. Apparently, excel treats those cells which have no precedents, as though they have precedents, when I call the function from the worksheet.
I don't know why this happens, or if the range.precedents property is supposed to behave like this.
How can this be fixed?
After a lot of searching, I encountered this:
When called from an Excel VBA UDF, Range.Precedents returns the range and not its precedents. Is there a workaround?
Apparently, "any call to .Precedents in a call stack that includes a UDF gets handled differntly".
So, what I did was use Range.Formula Like "=[a-zA-Z]" , because it satisfied my simple purpose. It is in no way an ideal alternative to the range.precedents property.
Foe a more detailed explanation, please visit that link.