Invalid procedure call or argument 5 with VBA Right function - excel

I am getting 'Invalid procedure call or argument: 5' error on the following piece of code bolded below. It has run successfully in the past with strings that longer than the one causing the error today. I have tried to include all pertinent information for troubleshooting. I did not write this code so I am lost as to what the issue could be. Any help would be appreciated to resolve the error. Debugger highlights the commented line below.
If ActiveWorkbook.Sheets("Macro-Tool").Cells(6, 5) <> "" Then
testsetfolderpath = ActiveWorkbook.Sheets("Macro-Tool").Cells(6, 5)
'get project name from Test SetFolder Path.
'cycle_int = InStrRev(testsetfolderpath, "\")
'test_mode = VBA.Right(testsetfolderpath, Len(testsetfolderpath) - cycle_int)
'inter_str = VBA.Left(testsetfolderpath, cycle_int - 1)
'inter_str1 = VBA.Left(inter_str, InStrRev(inter_str, "\") - 1)
'release_name = VBA.Right(inter_str1, Len(inter_str1) - InStrRev(inter_str1, "\"))
release_name = "^" + ActiveWorkbook.Sheets("Macro-Tool").Cells(7, 5) + "^"
Set tsetfactory = QcConnection.TestSetFactory
Set LabFilter = tsetfactory.Filter
LabFilter.Filter("CY_FOLDER_ID") = "^" + testsetfolderpath + "^"
Set testsets = LabFilter.NewList
i = 3
For Each tset In testsets
Set tsetfold = tset.TestSetFolder
' Error here ----------------------------------------------
parse_str = Right(tsetfold.Path, Len(tsetfold.Path) - Len(testsetfolderpath) - 1)
' ---------------------------------------------------------
pos1 = InStr(1, parse_str, "\")
If pos1 = 0 Then
func_area = parse_str
bus_process = parse_str
stream = parse_str
Else
func_area = Left(parse_str, pos1 - 1)
bus_process = Right(parse_str, Len(parse_str) - pos1)
pos2 = InStr(1, bus_process, "\")
If pos2 = 0 Then
stream = bus_process
Else
stream = Left(bus_process, pos2 - 1)
End If
End If
Additional information within the code that may be useful for troubleshooting:
Public field_names, parse_str, testsetfolderpath, inter_str1, test_mode, cycle_folder As String
Public tsetfold As TestSetFolder
testsetfolderpath = Root\System Test\2021 Projects\XXXX123456 Xxxxxxx Xxxxxxxxxxxxxx XX Xxxxxxxxxxxxxx - Xxxxxx X\Xxxxxx X.X_XXX

Related

Replace not working with SAP Gui Scripting retrieved data

So I created a scripting to retrieve data from SAP Gridview object to an Excel sheet. Some columns I needed to replace some characters because this data is consumed by a Power Bi report. For example:
4,350.00 will be replaced for the value 4350. So I do two replaces, the first removing the . and the second replacing the , with .
The problem is that the replace is being applied in every data retrieved. Here's the code.
For i = 0 To GridView.RowCount - 1
For j = 0 To GridView.ColumnCount - 1
shtInput.Cells(z + i, j + 1) = GridView.GetCellValue(i, GridView.ColumnOrder(j))
If j > 8 And j < 19 Then:
rep = GridView.GetCellValue(i, GridView.ColumnOrder(j))
rep = replace(rep, ".", "")
rep = replace(rep, ",", ".")
shtInput.Cells(z + i, j + 1) = rep
Next j
shtInput.Cells(z + i, Area) = "Finance"
If i Mod 32 = 0 Then
GridView.SetCurrentCell i, CStr(Columns(0))
GridView.firstVisibleRow = i
End If
Next i
There's a data column that the script capture the value 21.02.2021 and replace it with 21,02,2021.

Step gives error sound but not description

I am trying to run some code on Visual basic and about an hour ago it was working fine. I did something(not sure what) and now it gives an error sound but no description. This only happens when I try to create my own function in a module.
I have tried turning my pc on then off. I have opened different excel spreadsheets and yet it still won't let me run my function even with steps. My code below is incomplete but I'm sure it isn't the code itself thats the problem.
Function onetube(Thi As Double, Tho As Double, Tci As Double, Tco As Double) As Double
Dim phih, phic, phicf As Double
Dim ai(1 To 4, 1 To 4) As Variant
phih = (Thi - Tho) / (Thi - Tci)
phic = (Tco - Tci) / (Thi - Tci)
phicf = (phih - phic) / Application.WorksheetFunction.Ln((1 - phic) / (1 - phih))
ai(1, 1) = -0.462
ai(1, 2) = -0.0313
ai(1, 3) = -0.174
ai(1, 4) = -0.042
ai(2, 1) = 5.08
ai(2, 2) = 0.529
ai(2, 3) = 1.32
ai(2, 4) = 0.347
ai(3, 1) = -15.7
ai(3, 2) = -2.37
ai(3, 3) = -2.93
ai(3, 4) = -0.853
ai(4, 1) = 17.2
ai(4, 2) = 3.18
ai(4, 3) = 1.99
ai(4, 4) = 0.649
onetube = phicf
End Function

Fastest way to conditionally strip off the right part of a string

I need to remove the numeric part at the end of a string. Here are some examples:
"abcd1234" -> "abcd"
"a3bc45" -> "a3bc"
"kj3ih5" -> "kj3ih"
You get the idea.
I implemented a function which works well for this purpose.
Function VarStamm(name As String) As String
Dim i, a As Integer
a = 0
For i = Len(name) To 1 Step -1
If IsNumeric(Mid(name, i, 1)) = False Then
i = i + 1
Exit For
End If
Next i
If i <= Len(name) Then
VarStamm = name.Substring(0, i - 1)
Else
VarStamm = name
End If
End Function
The question is: is there any faster (more efficient in speed) way to do this? The problem is, I call this function within a loop with 3 million iterations and it would be nice to have it be more efficient.
I know about the String.LastIndexOf method, but I don't know how to use it when I need the index of the last connected number within a string.
You can use Array.FindLastIndex and then Substring:
Dim lastNonDigitIndex = Array.FindLastIndex(text.ToCharArray(), Function(c) Not char.IsDigit(c))
If lastNonDigitIndex >= 0
lastNonDigitIndex += 1
Dim part1 = text.Substring(0, lastNonDigitIndex)
Dim part2 = text.Substring(lastNonDigitIndex)
End If
I was skeptical that the Array.FindLastIndex method was actually faster, so I tested it myself. I borrowed the testing code posted by Amessihel, but added a third method:
Function VarStamm3(name As String) As String
Dim i As Integer
For i = name.Length - 1 To 0 Step -1
If Not Char.IsDigit(name(i)) Then
Exit For
End If
Next i
Return name.Substring(0, i + 1)
End Function
It uses your original algorithm, but just swaps out the old VB6-style string methods for newer .NET equivalent ones. Here's the results on my machine:
RunTime :
- VarStamm : 00:00:07.92
- VarStamm2 : 00:00:00.60
- VarStamm3 : 00:00:00.23
As you can see, your original algorithm was already quite well tuned. The problem wasn't the loop. The problem was Mid, IsNumeric, and Len. Since Tim's method didn't use those, it was much faster. But, if you stick with a manual for loop, it's twice as fast as using Array.FindLastIndex, all things being equal
Given your function VarStamm and Tim Schmelter's one named VarStamm2, here is a small test performance I wrote. I typed an arbitrary long String with a huge right part, and ran the functions one million times.
Module StackOverlow
Sub Main()
Dim testStr = "azekzoerjezoriezltjreoitueriou7657678678797897898997897978897898797989797"
Console.WriteLine("RunTime :" + vbNewLine +
" - VarStamm : " + getTimeSpent(AddressOf VarStamm, testStr) + vbNewLine +
" - VarStamm2 : " + getTimeSpent(AddressOf VarStamm2, testStr))
End Sub
Function getTimeSpent(f As Action(Of String), str As String) As String
Dim sw As Stopwatch = New Stopwatch()
Dim ts As TimeSpan
sw.Start()
For i = 1 To 1000000
f(str)
Next
sw.Stop()
ts = sw.Elapsed
Return String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10)
End Function
Function VarStamm(name As String) As String
Dim i, a As Integer
a = 0
For i = Len(name) To 1 Step -1
If IsNumeric(Mid(name, i, 1)) = False Then
i = i + 1
Exit For
End If
Next i
If i <= Len(name) Then
VarStamm = name.Substring(0, i - 1)
Else
VarStamm = name
End If
End Function
Function VarStamm2(name As String) As String
Dim lastNonDigitIndex = Array.FindLastIndex(name.ToCharArray(), Function(c) Not Char.IsDigit(c))
If lastNonDigitIndex >= 0 Then
lastNonDigitIndex += 1
Return name.Substring(0, lastNonDigitIndex)
End If
Return name
End Function
End Module
Here is the output I got:
RunTime :
- VarStamm : 00:00:38.33
- VarStamm2 : 00:00:02.72
So yes, you should choose his answer, his code is both pretty and efficient.

index and length must refer to a location within the string vb

I have to import an Excel file into an Access db. My Excel has a column called 'Description'. Usually the description is long a cell, but it can happens that is more long. If it is long more than 255 characters, I cut the string and I modify last 3 characters with '...'.
When I run the program, though, I have an error "Index and length must refer to a location within the string".
I tried even to erase the check on the length, but obviously I had this error "Field is too small to accept the amount of data you attempted to add"
If flag = 2 Then
stringVoice = grid(r, 3).Text
voc.Description = voc.Description & stringVoice
If voc.Description.Length > 255 Then
voc.Description = grid(r, 3).Text.ToString.Substring(0, 252) + "..."
End If
End If
This is the code that produced the Index and length error. Same check has done before if inside a single cell there are more than 255 characters
Case 1 'voce
id = id + 1
flag = 2
voc = New cVoce
c_Voc = voc.Cod_Chapter
p_Voc = voc.Cod_Paragraph
voc.Cod_Chapter = grid(r, 1).Text.Substring(0, 1)
voc.Cod_Paragraph = grid(r, 1).Text.Split(".")(0)
voc.Cod_Voice = Right(vett(0), 2)
If grid(r, 3).Text.Length > 255 Then
voc.Description = grid(r, 3).Text.ToString.Substring(0, 252) + "..."
Else
voc.Description = grid(r, 3).Text.ToString
If voc.Description.EndsWith("-") Then
a = Replace(voc.Description, "-", "")
voc.Description = a
End If
End If
stringVoice = voc.Description
voices.Add(voc)
but in this case I've never had an error.
Can anyone help me to fix my code?

ASP equivalent to strtr PHP function

I'm looking for an ASP equivalent to strtr PHP function.
I use it to encrypt in ROT47
This is my PHP code:
function rot47_encrypt($str)
{
return strtr($str,
'!"#$%&\'()*+,-./0123456789:;<=>?#ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~',
'PQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~!"#$%&\'()*+,-./0123456789:;<=>?#ABCDEFGHIJKLMNO'
);
}
Thank you
I believe there is no builtin function to do the same, so it will need to be implemented with a loop.
Something along the lines of:
Public Function rot47(str)
fromChars = "!""#$%&\'()*+,-./0123456789:;<=>?#ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
toChars = "PQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~!""#$%&\'()*+,-./0123456789:;<=>?#ABCDEFGHIJKLMNO"
rot47 = ""
For i = 1 To Len(str)
Position = InStr(fromChars, Mid(str, i, 1))
If Position = 0 Then
rot47 = rot47 & Mid(str, i, 1)
Else
rot47 = rot47 & Mid(toChars, Position, 1)
End If
Next
End Function

Resources