VB - Index outside of bounds? - vb.net

Oh my god i hate this thing, i tried millions of ways but couldn't find a working one. Let me explain:
I'm testing each line and checking the first word to be "copy" alright ? After the word copy i want to see if the next word is "1" , the third is "<" and the last is ">" , if all these conditions are fullfilled then the text between "<" and ">" needs to be stored in the variable "copy1" (even if there is more than 1 word between them).
What my code is:
For i = 0 To lstCode.Items.Count - 1
Dim str As String = lstCode.Items.Item(i)
Dim strA() As String = Split(str)
Dim copy1 as string
Dim copy2 as string
Select Case strA(0)
Case copy
If strA(1) = "1" And strA(2) = "<" And strA(strA.Count - 1) = ">" Then
copy1 = ""
For lr As Integer = 3 To strA.Count - 2
copy1 &= (strA(lr) & " ")
Next
End if
End select
And, when i debug it i get the error: Index was outside the bounds of the array ... Does anybody have any idea ?
There is something important i forgot to add, this is the whole code:
Case "copy"
If strA(1) = "1" And strA(2) = "<" And strA(strA.Count - 1) = ">" Then
copy1 = ""
For lr As Integer = 3 To strA.Count - 2
copy1 &= (strA(lr) & " ")
Next
ElseIf strA(1) = "2" And strA(2) = "<" And strA(strA.Count - 1) = ">" Then
copy2 = ""
For lrs As Integer = 3 To strA.Count - 2
copy2 &= (strA(lrs) & " ")
Next
ElseIf strA(1) = "run" Then
Try
IO.File.Copy(copy1, copy2)
Catch ex As IO.IOException
End Try
End If
End Select
So everything works like a charm: copy 1 < c:\csb.log > , copy 2 < c:\blabla.txt > but when the " copy run " statement comes in it gives me the error...

You need to change the operator And with AndAlso.
The second one applies Short Circuit Evaluation to your expression, meaning if the first expression is false the second, third and so on expressions on the same line are not evaluated.
In your line
If strA(1) = "1" And strA(2) = "<" And .......
when the value is "Run" you still evaluate the expression strA(2) = "<" but there is no element at index 2 so you get the error.

Related

Conditional Formatting in word vba

Greeting to all Members and Experts, I am trying to automate
the formatting process in word. The formatting is done by applying styles. But before applying styles I need to trim extra spaces between characters of serial numbers, for example, 1. a. i. and insert tabs after dot(.) and then apply the style. I have attached a sample document. Plz have a look. I have tried to get the desired result by using the following code but it doesn't get the work done
I am new here so i dont know how to attach sample files so, here is the link for sample file. https://docs.google.com/document/d/1Z1dB6tvPKVrxHlw7qV8VNyiy49c5lRZN/edit?usp=sharing&ouid=101706223056224820285&rtpof=true&sd=true
Any help or suggestion would be of great help. Thanks in advance...
Sub formatts()
Dim a As Integer
Dim i As Integer, n As Long, para As Paragraph, rng As Range, doc As Document
Set doc = ActiveDocument
With doc
For i = 1 To .Range.Paragraphs.Count
For n = 1 To doc.Paragraphs(i).Range.Characters.Count
If .Paragraphs(i).Range.Characters(n).Text = " " Or .Paragraphs(i).Range.Characters(n).Text = Chr(9) Or .Paragraphs(i).Range.Characters(n).Text = Chr(160) Then
.Paragraphs(i).Range.Characters(n).Select
'This line checks whether the first character is whitespace character or not and delete it.
doc.Paragraphs(i).Range.Characters(n).Delete
ElseIf .Paragraphs(i).Range.Characters(n).Text = "." Then
.Paragraphs(i).Range.Characters(n).InsertAfter (vbTab)
n = n + 1
a = a + 1
ElseIf .Paragraphs(i).Range.Characters(n).Text Like "[a-z]." And .Paragraphs(i).Range.Characters(n).Next.Next.Text <> "i" Then
Exit For
End If
If a >= 3 Then Exit For
Next
For n = 1 To doc.Paragraphs(i).Range.Characters.Count
If .Paragraphs(i).Range.Characters(n).Text = "i" And .Paragraphs(i).Range.Characters(n).Next.Text = "." And .Paragraphs(i).Range.Characters(n).Next.Next.Text = " " Then
doc.Range.Paragraphs(i).Style = "shh"
Exit For:
ElseIf .Paragraphs(i).Range.Characters(n).Text = "a" Or .Paragraphs(i).Range.Characters(n).Text = "b" Or .Paragraphs(i).Range.Characters(n).Text = "c" And .Paragraphs(i).Range.Characters(n).Next.Text = "." And .Paragraphs(i).Range.Characters(n).Next.Next.Text = " " Then
doc.Range.Paragraphs(i).Style = "sh"
Exit For
End If
Next
Next
End With
End Sub

VBA: loop losing variable value

I've got a strange problem with a loop in VBA, as it seems to be losing the value of my variable. Any ideas why? If i delete the loop, debug.print shows "test", otherwise it's empty (unless I print the value of "dupa" inside the loop)... Seems very strange.
Function carbon_copy(indeks As String) As String
Dim emails(1 To 3) As String
Dim i As Integer
Dim dupa As String
emails(1) = "abc#wp.pl"
emails(2) = "pabc#wp.pl"
emails(3) = "rabc#wp.pl"
i = 1
dupa = "test"
Do While emails(i) <> ""
If i = indeks Then
GoTo NextIteration
End If
dupa = dupa & ";" & emails(i)
NextIteration:
i = i + 1
Loop
Debug.Print dupa
carbon_copy = dupa
End Function
You should get a runtime error 9 since you index i will be 4 after you looped through your emails String array. As soon as it tries to compare the value of emails(4) with "" it should produce the "index out of range" since you have defined your Array to be only 3 elements long.
For a little clarification try this example code, it should produce the same error:
Function littleTest()
Dim teststr(1 To 3) As String
Dim i As Integer
teststr(1) = "abc"
teststr(2) = "def"
teststr(3) = "ghi"
i = 1
Do While teststr(i) <> ""
Debug.Print "i do it for the " & i & " time!"
i = i + 1
Loop
End Function
You have already found the solution yourself since UBound() is returning the actual length of your array which is in your case three so it will never search beyond the array.
You're indexing out of the array bounds. The condition Do While emails(i) <> "" is always true given your array, so the this fails on emails(4). Just test the array bounds and loop over that:
For i = LBound(emails) To UBound(emails)
If emails(i) <> "" And i = indeks Then
dupa = dupa & ";" & emails(i)
End If
Next
Actually, I've already solved the problem by using other loop type (For i = 1 To UBound(emails), Next i), but why the previous loop did not work is still quite mysterious to me... If anyone can explain, I'd appreciate it, as I prefer to understand things rather thank just do them correctly.
W.
this should work (explanations in comments):
Function carbon_copy(indeks As Long) As String
Dim emails(1 To 3) As String
Dim i As Long
Dim dupa As String
emails(1) = "abc#wp.pl"
emails(2) = "pabc#wp.pl"
emails(3) = "rabc#wp.pl"
i = 1
Do While emails(i) <> ""
If i <> indeks Then dupa = dupa & ";" & emails(i) ' update 'dupa' if current index doesn't natch passed 'indeks'
i = i + 1
If i > UBound(emails, 1) Then Exit Do ' be sure to exit upon exceeding 'emails()' array size
Loop
carbon_copy = dupa
End Function

for to next loop every word the first letter will be capital

what will i do to make it accept my equation and able to make all inserted word first letter capital? i f5 this equation and it show some yellow errors if i input a word.
i tried if i = 0 then
so it only shows first word first letter capital the rest isnt.
https://m.facebook.com/story.php?story_fbid=209840356412510&id=100021596419779&refid=17&tn=%2AW-R&_rdr
You didn't post your code on your post so i don't know what you tried but this code works perfectly in order to achieve your goal.
Dim textBoxString As String = TextBox1.Text.Trim
'Create a string array with every words'
Dim words() As String = textBoxString.Split(" ")
'labelString is our final result'
Dim labelString As String = ""
'cycle throught every word'
For i = 0 To textBoxString.Length - 1
Try
'substring(0,1) takes only the first char of the word'
words(i) = words(i).ToUpper().Substring(0, 1) & words(i).Substring(1, words(i).Length - 1)
labelString = labelString & words(i) & " "
Catch ex As Exception
Err.Clear()
Exit For
End Try
Next
Label1.Text = labelString
Little explanation of my code
words(i).ToUpper.Substring(0,1)
it takes only the first char of every word throught the cycle.
words(i).Substring(1, words(i).Lenght -1)
it takes the entire word without the first char
labelString = labelString & words(i) & " "
it concatenates every words back together.

Excel if cell contain "-" near number then move

What I need to do is to basically write lessons number. There are 3 colomns.
The second column is running by a custom formula called LessonsLeft done by someone from my second thread on stackoverflow and it is
Function LessonsLeft(rng As Range) As String
If rng.Count > 1 Then Exit Function
Dim spltStr() As String
Dim i As Long
spltStr = Split(rng.Value, ",")
LessonsLeft = ",1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,"
For i = LBound(spltStr) To UBound(spltStr)
LessonsLeft = Replace(LessonsLeft, "," & spltStr(i) & ",", ",")
Next i
LessonsLeft = Mid(LessonsLeft, 2, Len(LessonsLeft) - 2)
End Function
What I need to do is to add another, third colomn which is for lessons that my students did their first attempt but they couldnt pass exam.
How i want the data to be there, is to write for exemple a "-" or "+" near a number in first column so the number will move to third column.
How can it be done ?
use this function
Function LessonsAttemptedButNotDone(rng As Range) As String
If rng.Count > 1 Then Exit Function
Dim spltStr() As String, lessonDone As String
Dim i As Long
spltStr = Split(rng.Value, ",")
For i = LBound(spltStr) To UBound(spltStr)
lessonDone = spltStr(i)
If Right(lessonDone, 1) = "-" Then
lessonDone = Left(lessonDone, Len(lessonDone) - 1)
LessonsAttemptedButNotDone = LessonsAttemptedButNotDone & lessonDone & ","
End If
Next
If LessonsAttemptedButNotDone <> "" Then LessonsAttemptedButNotDone = Left(LessonsAttemptedButNotDone, Len(LessonsAttemptedButNotDone) - 1)
End Function

My For Loop skips my IF statement

I wonder if anyone can help. I am making a program that will convert Text to ASCII. However, I want my program to ignore spaces. Hence, "IT WAS A" should look like this: 7384 876583 65
When I use the Step Into Feature of VB I can see that my For loop is skipping my IF statement which should be giving me my spaces. I don't understand why. As you can probably tell, I am a beginner so any specific help would be greatly appreciated. My code looks like this:
Dim PlainText, ConvertedLetter As String
Dim LetterToConvert As Char
Dim AscNumber, Counter As Integer
ConvertedLetter = ""
PlainText = txtPlain.Text
For Counter = 1 To Len(PlainText)
LetterToConvert = Mid(PlainText, Counter, 1)
If PlainText = " " Then
ConvertedLetter = " "
Else : AscNumber = Asc(LetterToConvert)
ConvertedLetter = ConvertedLetter & AscNumber
End If
Next
txtAscii.Text = ConvertedLetter
Because you're comparing PlainText, which is the whole string, to " ". It would need to be:
If LetterToConvert = " " Then ....
Try this:
Dim PlainText, ConvertedLetter As String
ConvertedLetter = ""
PlainText = "IT WAS A"
For Each c As Char In PlainText 'iterate through each character in the input
If c <> " " Then ' check whether c is space or not
ConvertedLetter &= Asc(c).ToString()' ascii value is taken if c<>" "
Else
ConvertedLetter &= " " ' c is space means add a space
End If
Next
MsgBox(ConvertedLetter) ' display the result
You will get the output as
7384 876583 65