Userform throws error when using "." or "-" or "backspace" - vba

Hello my userform in Excel keeps throwing an error whenever I try to input anything that isnt a number i.e. "." or "-" or if I backspace my number to clear the row.
I believe it because I defined the variable that the userform will be assigned to as a long.
This prevents me from inputting negative numbers or decimals without throwing an error.
Any idea's as to how I can allow my userform to accept the Long variable type while still allowing me to enter other characters that aren't directly a number?
To be clear the error occurs while entering the number into the UF not after I action the Userform.
EDIT:
Code for this is quite simple
Public LTB As Long
Public Sub LotTextBox_Change()
LTB = LotTextBox.Value
End Sub
Error Msg is - Run time error 13 - Mismatch.

The problem that you are facing is because you are trying to store whatever is being typed in the textbox which is a String at runtime to a Long Variable.
So if you want to type -123 then the moment you type -, you will get an error as you are trying to store a String i.e - in a Long variable.
Try this
Public LTB As Long
Private Sub LotTextBox_Change()
On Error GoTo Whoa
LTB = Val(LotTextBox.Value)
Whoa:
'Debug.Print LTB
End Sub
What this does is, it tries to store the textbox value to the variable and if it is not of correct datatype, it exits gracefully.

Related

VLookup in VBA userforms - string lookup value

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
.

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.

Type Mismatch Error InStr with two string variables

I am trying to to check if a current value in a string already exists in a given string. I am using two variables for the parent string(existing string in a cell, active cell) and the search string . If the current string exactly exists in the parent string then a message box pops asking if the user wants to paste the same content .
I get a type mismatch error at the first IF condition . Can you please help me understand what the issue is ?
If (InStr(ActiveCell.Value, NRemark, vbBinaryCompare)) = 0 Then
Exit Sub
Else
Lresponse = MsgBox("You have already synced the same content . Would you like to continue ?", vbYesNo)
If Lresponse = vbYes Then
GoTo V1 ' goes to the loop to append NRemark to the parent string
Else
Exit Sub
End If
End If
Way late on the answer here, but ran into this same problem today - because the ActiveCell.Value is a Variant (Type Undetermined), the system sees that as you trying to use the optional [start] variable, so the type mismatch actually occurs because it thinks your compare variable should be a string.
I was using cel.FormulaR1C1 to try to manipulate a variable and that is also a Variant type, so it caused this same problem.
Take a look at arguments in InStr:
Function InStr([Start], [String1], [String2], [Compare As VbCompareMethod = vbBinaryCompare])
The first argument is [Start], which in your case should probably be a constant of 1 value.
If (InStr(1, ActiveCell.Value, NRemark, vbBinaryCompare)) = 0 Then ...
This should work, at least temporarily, until you encounter empty cells, cells with errors, etc., in which case it will break again.
Also, I can't tell what type your NRemark variable is. It has to be a string, so if it's a cell, you should put NRemark.Value instead.

Hyperlinks in VBA

I want to create a button in Excel which links to:
http://datafeed.api.productserve.com/datafeed/download/apikey/50f70c7de11f99fe127d7ad4c8e37e31/cid/97,98,142,144,
...
,567,569/fid/4319/columns/merchant_product_id,merchant_category,brand_name,product_name,mpn,search_price,aw_deep_link,specifications,valid_from,valid_to,in_stock,warranty,aw_product_id,merchant_image_url,description/format/csv/delimiter/,/compression/gzip/adultcontent/1/
I've cut out a large section in the middle, but it is just a long sequence of numbers separated by commas. In total the URL is 1939 characters long.
Copying the URL into a browser works fine - it is a download link and the file opens as it should.
The code for the button is simple:
Private Sub download_button_Click()
Dim feed_hyperlink As String
feed_hyperlink = *"http://data... "*
ActiveWorkbook.FollowHyperlink feed_hyperlink
End Sub
When I run the procedure, I get the following error:
Run-time error '5': Invalid procedure call or argument
Hyperlinking a cell restricts the destination URL to 255 characters. Is a character limit what's causing the issue here, or is there another problem?
I think you're right. It's probably too long as the longest one I can use before getting the same error is 1033 characters;
Sub Main()
Dim h As String
h = String(1034, "a")
Debug.Print Len(h)
ActiveWorkbook.FollowHyperlink h
End Sub

Compile Error Expected Function Or Variable

I'm new and trying to learn VBA. When I'm typing in the code I get Compile Error Expected Function Or Variable.
Is something regarding the activecell, but can't figure it out.
Sub Testare()
Dim FilmName As String
Dim FilmLenght As Integer
Dim FilmDescription As String
Range("b10").Select
FilmName = ActiveCell.Value
FilmLenght = ActiveCell.Offset(0, 2).Value
If FilmLenght < 100 Then
FilmDescription = "Interesant"
Else
FilmDescription = "Suficient"
End If
MsgBox FilmName & " is " & FilmDescription
End Sub
This error happens also when a Sub is called the same as variable (i.e. in one Sub you have for loop with iterator "a", whilst another Sub is called "a").
This gives an error that fits to your description.
Regards
It is possible to make your code fail in two different ways:
Place a very large value in D10
Place a text value in D10
This will result in either an overflow error or type mismatch error.
I know this was asked a while ago, but I ended up with the same error when I created a Sub within a worksheet with the Sub Name the same as the Worksheet name.
Unfortunately when this happens, no error is highlighted by a compile; the error only appears at run time. Also, the offending line is not highlighted, which makes it a bit difficult to find.
Changing the Sub name solves it though.
Here is what caused this error in my case:
Inside a new Public Function that I was starting to write, I began to type a statement, but needed to copy a long varname from another location, so I just inserted the letter a to prevent Excel from popping-up the uber-annoying Compile Error: Expected Expression dialog. So I had this:
myVarName = a
I then attempted to step-through my code to get to that point, and when Excel entered that function I received the message "Compile Error: Expected Function Or Variable". To resolve, I just changed the placeholder to a number:
myVarName = 1
And all continued just fine.