Average of TextBoxes that are not blank - vba

I need the average of five TextBoxes but there is a chance that some of them may be empty. If so, it should only consider the filled TextBoxes. I have some code to find the sum of those but couldn't succeed in finding the average:
Tot = Tot + CDbl(TextBox117.Text)
Tot = Tot + CDbl(TextBox118.Text)
Tot = Tot + CDbl(TextBox119.Text)
Tot = Tot + CDbl(TextBox120.Text)
Tot = Tot + CDbl(TextBox121.Text)
TextBox70.Text = Tot
I even tried counting the number of boxes but still couldn't get the result.

Dim arr, i As Long, n As Long, t As Double, v
arr = Array(TextBox117, TextBox118, TextBox119, TextBox120, TextBox121)
n = 0
t = 0
For i = lbound(arr) to ubound(arr)
v = Trim(arr(i).Text)
If Len(v) > 0 Then
t = t + CDbl(v)
n = n + 1
End If
Next i
TextBox70.Text = t
TextBox71.Text = t/n

Dim divNum As Integer
Dim Tot As Double
Dim numsAdd(4) As String
numsAdd(0) = TextBox1.Text
numsAdd(1) = TextBox2.Text
numsAdd(2) = TextBox3.Text
numsAdd(3) = TextBox4.Text
numsAdd(4) = TextBox5.Text
divNum = 0
For i = 0 To UBound(numsAdd)
If numsAdd(i) <> "" Then
Tot = Tot + CDbl(numsAdd(i))
divNum = divNum + 1
End If
Next i
TextBox6.Text = Tot / divNum

Related

Count lines not 0 found Textboxes

I want to calculate the amount in a multiline Textbox where the value 0 is not found.
If TxtListScanValue.Text = ("2") Then
TxtDrawR2.Text &= Environment.NewLine & lastDraw2
Dim ListScan = TxtNumberListScan.Lines.ToList.Select(Function(o, i) New With {.scan = o, .Index = i})
Dim DrawR2 = TxtDrawR2.Lines.ToList.Select(Function(o, i) New With {.draw = o, .Index = i})
Dim list2 = From a In ListScan From b In DrawR2 Where a.Index = b.Index Select LstScan = a.scan, DrwR2 = ("00" & b.draw).Substring(("00" & b.draw).Length - 2) Order By DrwR2 Descending
TxtListScanTxt.Text = String.Join(vbCrLf, list2)
End If
If TxtdrawR5 =
2
4
0
0
1
3
5
In output I want to display: 5 because:
I want to calculate the count lines where the value 0 is not found. Count lines no have 0 value :D (2+4+1+3+5 = 5) (5 lines no have 0 value).
You create function like this:
'For Counting
Private Function CountNonZero(ByVal TheCtrl As TextBox) As Integer
Dim myCnt As Integer = 0
For Each Content In TheCtrl.Lines
Dim ContentVal As Integer = 0
Integer.TryParse(Content, ContentVal)
If ContentVal <> 0 Then myCnt += 1
Next
Return myCnt
End Function
'For Counting
Private Function SummingNonZero(ByVal TheCtrl As TextBox) As Integer
Dim mySum As Integer = 0
For Each Content In TheCtrl.Lines
Dim ContentVal As Integer = 0
Integer.TryParse(Content, ContentVal)
If ContentVal <> 0 Then mySum += ContentVal
Next
Return mySum
End Function
And you can count or sum now:
dim TxtdrawR5Count as integer = CountNonZero(TxtdrawR5)
dim TxtdrawR5Sum as integer = SummingNonZero(TxtdrawR5)

taking average of 10 sample in vb2010

I have used below code to find average of 10 sample . But during first time it take sample and do the averaging . during next cycle counter not become Zero.and text box not updating
Static counter As Integer = 0
DIm average_sum As Double = 0
If counter < 10 Then
counter = counter + 1
Count_val.Text = counter
Dim array(10) As Double
For value As Integer = 0 To counter
array(counter) = k
average_sum = average_sum + array(counter)
Next
If counter = 10 Then
average_sum = average_sum / array.Count
System.Threading.Thread.Sleep(250)
Array_count.Text = average_sum
End If
If counter > 10 Then
average_sum = 0
counter = 0
End If
End If
If Avg_count < 10 Then
Dim array(10) As Double
For value As Double = 0 To Avg_count
array(Avg_count) = k
average_sum = average_sum + array(Avg_count)
Avg_count = Avg_count + 1
Next
If Avg_count = 10 Then
average_sum = average_sum / Avg_count
System.Threading.Thread.Sleep(250)
Average.Text = average_sum
Avg_count = 0
End If
End If
Here count value setting properly . But after 2 to3 cycle Average will done earlier itself same thing i writen in excel to compare averages but not matching with average and excel sheet data
Below is excel sheet code.Both code are in timer1 block.
If counter < 10 Then
'counter = 0
'average_sum = 0
Dim headerText = ""
Dim csvFile As String = IO.Path.Combine(My.Application.Info.DirectoryPath, "Current.csv")
If Not IO.File.Exists((csvFile)) Then
headerText = "Date,TIME ,Current, "
End If
Using outFile = My.Computer.FileSystem.OpenTextFileWriter(csvFile, True)
If headerText.Length > 0 Then
outFile.WriteLine(headerText)
End If
Dim date1 As String = "25-10-2014"
Dim time1 As String = TimeOfDay()
Dim x As String = date1 + "," + time1 + "," + distance
outFile.Write(x)
End Using
End If
If counter > 10 Then
counter = 0
End If

Converting RGB value into integers

I'm making an app with a color dropper tool on it using g.CopyFromScreen(screenpoint, Point.Empty, Bmp2.Size) (the dropper tool works currently), once I have the dropper values I want to convert the RBG values into individual integers.
The values that i'm converting are in this format
Color [A=255, R=240, G=240, B=240]
which needs to be in four different integers
My code is giving me odd results and I'm lost now
My code:
Dim text1Conv As String
text1Conv = TextBox1.Text
Dim myChars() As Char = text1Conv.ToCharArray()
For Each ch As Char In myChars
If Char.IsDigit(ch) And Not ch = " " And Not ch = "," And Not count > 2 Then
color1Conv = color1Conv + ch
TextBox2.Text = TextBox2.Text + color1Conv 'test result
count = count + 1
ElseIf Char.IsDigit(ch) And Not ch = " " And Not ch = "," And count < 2 And Not count > 5 Then
color2Conv = color2Conv + ch
TextBox2.Text = TextBox2.Text + color2Conv 'test result
count = count + 1
ElseIf Char.IsDigit(ch) And Not ch = " " And Not ch = "," And count < 5 And Not count > 8 Then
color3Conv = color3Conv + ch
TextBox2.Text = TextBox2.Text + color3Conv 'test result
count = count + 1
ElseIf Char.IsDigit(ch) And Not ch = " " And Not ch = "," And count < 8 And Not count > 11 Then
color4Conv = color4Conv + ch
TextBox2.Text = TextBox2.Text + color4Conv 'test result
count = count + 1
End If
Next
results: 225 255 118 112 122
results: 225 255 116 772 721
probably an easy one but I can't see it
Using regular expressions:
I used "[A=255, R=241, G=24, B=2]" as a test string and split it into four integers.
Dim a as Integer, r as Integer, g as Integer, b as Integer
Dim s as String = "[A=255, R=241, G=24, B=2]"
Dim mc as MatchCollection = System.Text.RegularExpressions.Regex.Matches( s, "(\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+", RegexOptions.None )
Integer.TryParse( mc(0).Groups(1).Value, a )
Integer.TryParse( mc(0).Groups(2).Value, r )
Integer.TryParse( mc(0).Groups(3).Value, g )
Integer.TryParse( mc(0).Groups(4).Value, b )
NOTE: it will have no problems with numbers being 1, 2, or any number of digits long.
You can use regular expressions:
Imports System.Text.RegularExpressions
Dim input As String = "Color [A=255, R=240, G=240, B=240]"
Dim re As New Regex("Color \[A=(\d+), R=(\d+), G=(\d+), B=(\d+)\]")
Dim m As Match = re.Match(input)
Dim integer1 As Integer = Convert.ToInt32(m.Groups(1).Value) '255
Dim integer2 As Integer = Convert.ToInt32(m.Groups(2).Value) '240
Dim integer3 As Integer = Convert.ToInt32(m.Groups(3).Value) '240
Dim integer4 As Integer = Convert.ToInt32(m.Groups(4).Value) '240

Visual Basic - variable names auto increment / loop

Is there a way when I want to do this:
textbox1.text = x + i
textbox2.text = x + i
textbox3.text = x + i
To do for example like this?
for l = 0 to 2
textbox(l) = x + i
next
Assuming these controls are inside a userform, do something like:
Private Sub FillTextboxes()
Dim i As Long
Dim x As Long
x = 10
For i = 0 To 2
Me.Controls("textbox" & i + 1).Text = x + i
Next i
End Sub

addition of items in a listbox

Bellow pls find the code in VB 2008. I must sum up the numbers in the xlistscorebox
Dim tot As Integer
Dim scoresentered As Integer
For index As Integer = 0 To Me.xListscoreBox.SelectedItems.Count - 1
tot = tot + Val(Me.xListscoreBox.SelectedItems.Count)
For se As Integer = 0 To Me.xListscoreBox.SelectedItems.Count - 1
scoresentered = Me.xListscoreBox.SelectedItems.Count
Next
Next
Me.xscoreave.Text = tot / scoresentered.ToString
Me.xtotalscoreTextBox.Text = tot.ToString
Me.xtotscoreentered.Text = scoresentered.ToString
The issue is that the calculation is giving wrong results.
Any comment will be highly appreciate.
Cecilia
Dim TotalScore as Integer
For Each x As String In Me.xListscoreBox.SelectedItems
TotalScore = TotalScore + Convert.ToInt32(x)
Next
dim AverageScore as Decimal = TotalScore / Me.xListscoreBox.SelectedItems.Count
dim ScoresEntered = Me.xListscoreBox.SelectedItems.Count