Application.Match with number stored as text - vba

I am getting a type mismatch error with the code below. ENT.Cells(2,1) contains a number - 709150 - which is stored as text.
Dim test As Long
test = Application.Match(ENT.Cells(2, 1), RDS.Range("A:A"), 0)
If I change the argument to a number, it works. However, I cannot pass a number as we have some "0xxxxx" codes
Dim test As Long
test = Application.Match(709150, RDS.Range("A:A"), 0)

Related

Exception thrown: 'System.IndexOutOfRangeException' in VB.net

So I keep getting the error
Exception thrown: 'System.IndexOutOfRangeException'
For my array counter. I'm still learning Visual Basic but I have programming knowledge in other languages, so I'm not sure if I'm just not translating between syntax properly here, or if it's just a logic error I'm not seeing.
The arrayNumsMultiply() array is an Integer array declared with 6 values. My counter variable was also declared as an Integer previously in the program.
The following code is meant to count through the 6 numbers, check if they have two integers in them (ex: 47, so 4 and 7), and then add them together. From there, it stores it back into the array and wipes the previous value. I have a System.Console.WriteLine in the code to see if the If statement is even being initiated and it's not, but the counter console log is. Any idea on what I may be doing wrong? (The specific line where the error is being thrown is the If arrayNumsMultiply(counter).ToString.Length = 2 Then)
For counter = 0 To 5
If arrayNumsMultiply(counter).ToString.Length = 2 Then
Dim numOne As String = arrayNumsMultiply(counter).ToString().Substring(1, 1)
Dim numTwo As String = arrayNumsMultiply(counter).ToString().Substring(2, 1)
Convert.ToInt32(numOne)
Convert.ToInt32(numTwo)
Dim totalNum As Integer
totalNum = numOne + numTwo
arrayNumsMultiply(counter) = totalNum
System.Console.WriteLine("If statement for adding in an integer working")
End If
System.Console.WriteLine("Counter working")
Next counter
If the length of you string is 2, the the first character is at position 0 and the second character is at position 1. Like most things in .net this is zero based. So the correct code for these lines is...
Dim numOne As String = arrayNumsMultiply(counter).ToString().Substring(0, 1)
Dim numTwo As String = arrayNumsMultiply(counter).ToString().Substring(1, 1)

Error 2042 When Using Vlookup To Find Date in VBA

I am trying to write code that looks up a batch ID based on the date entered in the "TSDate" field.
I keep getting error 2042 when trying to use the Application.VLookup Function in VBA:
'Timesheet Date
Private Sub TSdate_KeyDown(ByVal KeyCode As _
MSForms.ReturnInteger, ByVal shift As Integer)
Dim TimesheetDate As Date
Dim batch As Variant
Dim DRange As Range
Set DRange = Range("Table_PayPeriods")
If KeyCode = 13 Or KeyCode = 9 Then
TSDate.Value = Format(TSDate.Value, "dd-mmm-yy")
TimesheetDate = TSDate.Value
batch = Application.VLookup(TSDate.Value, DRange, 2, 0)
MsgBox (DRange(2, 2))
BatchID.Text = batch
End If
End Sub
The messagebox proves that the data being looked up is being pulled properly, the problem is I am getting the error in the "batch" variable.
Any help would be appreciated. Thanks!
Application.VLookup will return Error 2042 when the lookup value is not found.
You need to test for error, and handle appropriately:
If KeyCode = 13 Or KeyCode = 9 Then
TSDate.Value = Format(TSDate.Value, "dd-mmm-yy")
TimesheetDate = TSDate.Value
batch = Application.VLookup(TSDate.Value, DRange, 2, 0)
If IsError(batch) Then
'Do something
Else
MsgBox (DRange(2, 2))
BatchID.Text = batch
End If
End If
As for why the value is not found, it's not possible to answer without more detail from you regarding the input data and respective formats -- perhaps the value really doesn't exist, or perhaps it appears to exist but really does not: (generally I would expect a string will not match a date type and vice-versa).
If the cells contain Date type values (even if they are formatted to look like strings, an Error is expected. In this case, convert the string input (TSDate.Value) to a Date type, and convert that to a Long type and do the Vlookup with its long numeric equivalent:
batch = Application.VLookup(CLng(CDate(TSDate.Value)), DRange, 2, 0)
You'll still need Error handling in the event that the date value literally doesn't exist in the table.

Range(Cells(... working incorrectly

I have a little problem with a VBA range being used in excel:
dim frameRefPoint As String
frameRefPoint = "4,4"
range(Cells(frameRefPoint).Offset(0,0), Cells(frameRefPoint).Offset(7, 7)).Interior ...
This isn't behaving as I expect it to. I think that the first cell in the specified Range(Cells(4,4).Offset(0,0)) should be "D4", but when I use the range in a code, the first cell of the range is "D1" ~ cells(1,4).
The address property of cells(frameRefPoint) returns $D$1. What am I missing here?
You cannot span two paramters (e.g. .Cells(<row>, <column>)) by concatenating the values with an interim comma. Just making it 'look' like what you code is not the same as legitimate code. You can however, use variables for each parameter.
dim r as long, c as long, frameRefPoint As string
r = 4
c = 4
cells(r, c).resize(7, 7) = "this works"
frameRefPoint = "4,4"
'split the string on the comma and convert teh text-that-looks-numbers to actual numbers
cells(int(split(frameRefPoint, ",")(0))), int(split(frameRefPoint, ",")(1)).resize(7, 7) = "this also works"
Range(Cells(CInt(Split(frameRefPoint, ",")(0)), CInt(Split(frameRefPoint, ",")(1))).Offset(0, 0), Cells(CInt(Split(frameRefPoint, ",")(0)), CInt(Split(frameRefPoint, ",")(1))).Offset(7, 7)).Interior....

How can I convert an Userform Textbox value which is a String into a Long in Excel Vba?

Here I am trying to read a Textbox.Value which is a String into a Variable (i.e. text_bval) and converting that String into a Long using CLng function for my calculations. The CLng function is showing run time error like type mismatch.
See my code below:
Dim text_bval As String
Dim text_long As Long
Dim per As Integer
text_bval = text_box.value
user_entry = CLng(text_bval)
According to the MS Documentation, "If expression lies outside the acceptable range for the Long subtype, an error occurs."
In this case, your expression is text_bval, a string. If your string contains characters that cannot be converted to a number, an error will occur.
For example:
text_bval = "2" then CLng(text_bval) = 2
text_bval = "2.3" then CLng(text_bval) = 2
text_bval = "hello" then CLng(text_bval) = ERROR
In order to prevent an error from occuring, you should do something like the following:
user_entry = 0
On Error Resume Next
user_entry = CLng(text_bval)
On Error GoTo 0

Excel VBA: Need Workaround for 255 Transpose Character Limit When Returning Variant Array to Selected Range

I am struggling with a common problem involving an apparent Excel 255-character-limit. I encounter an error when attempting to return a variant-array from a Function to the selected range on the worksheet. When each of the cells in the Function's returning array are under 255 characters, they post to the sheet just as they should: one element appears in each cell within the selected range. However, if any element in my returning variant array is longer than 255 characters I get a Value! error. These errors are bad because I need my long elements and want to keep the data together!
Versions of this problem appear over and over again in many forums, yet I am able to find a clear simple, all-purpose solution for returning variant arrays to the selected range (not necessarily containing formulas) when the array cells exceed 255 characters. My largest elements are around 1000, but it would be better if the solution could accommodate elements up to 2000 characters.
Preferably, I want this to be implemented with a function, or lines of additional code which can be added to my function (not a subroutine). My reason for wanting to avoid subroutines: I do not want to have to hard-code any ranges. I want this to be flexible and for the output location to be dynamically based on my current selection.
Please, if you can help find a way to produce a function, which takes a Variant Array as input, and which maintains the desired array:cell 1:1 relationship, I'd appreciate it greatly.
So this function with short cells works:
Function WriteUnder255Cells()
Dim myArray(3) As Variant 'this the variant array I will attempt to write
' Here I fill each element with less than 255 characters
' it should output them if you call the function properly.
myArray(0) = "dog"
myArray(1) = "cat"
myArray(2) = "bird"
myArray(3) = "fly"
WriteUnder255Cells = Application.Transpose(myArray())
End Function
But this fuction, with cells exceeding 255 will not output.
Function WriteOver255Cells()
Dim myArray(3) As Variant 'this the variant array I will attempt to write
' Here I fill each element with more than 255 characters
' exceeding the 255-character limit causes the VALUE! errors when you output them
myArray(0) = "ThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelaxydog"
myArray(1) = "ThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydog"
myArray(2) = "ThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydog"
myArray(3) = "ThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydogThequickbrownfoxjumpedoverthelazydog"
WriteOver255Cells = Application.Transpose(myArray())
End Function
This is how you produce the output and results:
First you need to create the two modules(to insert one function into each module, paste the code from one into the respective module). To run "WriteUnder255Cells()", select an area of 4 rows x 1 column on the sheet (this where you return the module) and type "=WriteUnder255Cells()" into the formula bar (do not enter the quotes). Note these are called like array formulas, so instead of hitting (enter) to create the output, you need to hit (control + shift + enter). Repeat the same process for WriteOver255Cells() to produce the errors.
Here are some documents/forums discussions which address it. The solutions seem to be either overly specific or clunky because they evoke subroutines (which I want to avoid):
https://support.microsoft.com/en-us/kb/213841
http://www.mrexcel.com/forum/excel-questions/852781-visual-basic-applications-evaluate-method-255-character-limit.html
Excel: Use formula longer that 255 characters
VBA code error when array value exceeds 255 characters
http://dailydoseofexcel.com/archives/2005/01/10/entering-long-array-formulas-in-vba/
https://forums.techguy.org/threads/solved-vba-access-to-excel-255-char-limit-issue.996495/
http://www.mrexcel.com/forum/excel-questions/494675-255-character-cell-limit-visual-basic-applications-workaround.html
Array formula with more than 255 characters
http://www.mrexcel.com/forum/excel-questions/388250-size-limit-transferring-variant-range-excel-2007-a.html
This works for me:
Function Over255()
Dim myArray(3) As String '<<<<< not variant
myArray(0) = String(300, "a")
myArray(1) = String(300, "b")
myArray(2) = String(300, "c")
myArray(3) = String(300, "d")
'Over255 = Application.Transpose(myArray())
Over255 = TR(myArray)
End Function
'like Application.Transpose...
Function TR(arrIn) As String()
Dim arrOut() As String, r As Long, ln As Long, i As Long
ln = (UBound(arrIn) - LBound(arrIn)) + 1
ReDim arrOut(1 To ln, 1 To 1)
i = 1
For r = LBound(arrIn) To UBound(arrIn)
arrOut(i, 1) = arrIn(r)
i = i + 1
Next r
TR = arrOut
End Function
Seems like you need to return a string array and Application.Transpose doesn't do that