How to reduce case in VB Net? - vb.net

Can this code be somewhat shorter? values change from 10 in 10 cases to +1, as follows as follows. For each textbox, I have these 80 cases, and I have a total of 30 textboxes and the code is too long. Once again I have to repeat the code, because aryText = TextNSt1.Text.Split ("," c) - up to aryText = TextNSt30.Text.Split ("," c) - so 30 times you code this, and I do not really like it, comma, the code is too long. The code changes with +1 in the following cases from Case 1 to 10, Cases 11-20, Cases 21-30, Cases 31-40, Cases 41-50, Cases 51-60, Cases 61-70, 71- 80...
Dim i As Integer
Dim aryText() As String
'SPLIT THE TEXT INTO AN ARRAY'
aryText = TextNSt2.Text.Split(","c)
For i = 0 To UBound(aryText)
Select Case aryText(i)
Case "1"
LbNxSt1.Text = Val(LbNxSt1.Text) + 1
txtS10.Text = Val(txtS10.Text) + 1
txtDrawB1.Text = Val(txtDrawB1.Text) + 1
Case "2"
LbNxSt2.Text = Val(LbNxSt2.Text) + 1
txtS10.Text = Val(txtS10.Text) + 2
txtDrawB1.Text = Val(txtDrawB1.Text) + 1
Case "3"
LbNxSt3.Text = Val(LbNxSt3.Text) + 1
txtS10.Text = Val(txtS10.Text) + 3
txtDrawB1.Text = Val(txtDrawB1.Text) + 1
Case "4"
LbNxSt4.Text = Val(LbNxSt4.Text) + 1
txtS10.Text = Val(txtS10.Text) + 4
txtDrawB1.Text = Val(txtDrawB1.Text) + 1
Case "5"
LbNxSt5.Text = Val(LbNxSt5.Text) + 1
txtS10.Text = Val(txtS10.Text) + 5
txtDrawB1.Text = Val(txtDrawB1.Text) + 1
Case "6"
LbNxSt6.Text = Val(LbNxSt6.Text) + 1
txtS10.Text = Val(txtS10.Text) + 6
txtDrawB1.Text = Val(txtDrawB1.Text) + 1
Case "7"
LbNxSt7.Text = Val(LbNxSt7.Text) + 1
txtS10.Text = Val(txtS10.Text) + 7
txtDrawB1.Text = Val(txtDrawB1.Text) + 1
Case "8"
LbNxSt8.Text = Val(LbNxSt8.Text) + 1
txtS10.Text = Val(txtS10.Text) + 8
txtDrawB1.Text = Val(txtDrawB1.Text) + 1
Case "9"
LbNxSt9.Text = Val(LbNxSt9.Text) + 1
txtS10.Text = Val(txtS10.Text) + 9
txtDrawB1.Text = Val(txtDrawB1.Text) + 1
Case "10"
LbNxSt10.Text = Val(LbNxSt10.Text) + 1
txtS10.Text = Val(txtS10.Text) + 10
txtDrawB1.Text = Val(txtDrawB1.Text) + 1
Case "11"
LbNxSt11.Text = Val(LbNxSt11.Text) + 1
txtS20.Text = Val(txtS20.Text) + 1
txtDrawB2.Text = Val(txtDrawB2.Text) + 1
Case "12"
LbNxSt12.Text = Val(LbNxSt12.Text) + 1
txtS20.Text = Val(txtS20.Text) + 2
txtDrawB2.Text = Val(txtDrawB2.Text) + 1
Case "13"
LbNxSt13.Text = Val(LbNxSt13.Text) + 1
txtS20.Text = Val(txtS20.Text) + 3
txtDrawB2.Text = Val(txtDrawB2.Text) + 1
Case "14"
LbNxSt14.Text = Val(LbNxSt14.Text) + 1
txtS20.Text = Val(txtS20.Text) + 4
txtDrawB2.Text = Val(txtDrawB2.Text) + 1
Case "15"
LbNxSt15.Text = Val(LbNxSt15.Text) + 1
txtS20.Text = Val(txtS20.Text) + 5
txtDrawB2.Text = Val(txtDrawB2.Text) + 1
Case "16"
LbNxSt16.Text = Val(LbNxSt16.Text) + 1
txtS20.Text = Val(txtS20.Text) + 6
txtDrawB2.Text = Val(txtDrawB2.Text) + 1
Case "17"
LbNxSt17.Text = Val(LbNxSt17.Text) + 1
txtS20.Text = Val(txtS20.Text) + 7
txtDrawB2.Text = Val(txtDrawB2.Text) + 1
Case "18"
LbNxSt18.Text = Val(LbNxSt18.Text) + 1
txtS20.Text = Val(txtS20.Text) + 8
txtDrawB2.Text = Val(txtDrawB2.Text) + 1
Case "19"
LbNxSt19.Text = Val(LbNxSt19.Text) + 1
txtS20.Text = Val(txtS20.Text) + 9
txtDrawB2.Text = Val(txtDrawB2.Text) + 1
Case "20"
LbNxSt20.Text = Val(LbNxSt20.Text) + 1
txtS20.Text = Val(txtS20.Text) + 10
txtDrawB2.Text = Val(txtDrawB2.Text) + 1
Case "21"
LbNxSt21.Text = Val(LbNxSt21.Text) + 1
txtS30.Text = Val(txtS30.Text) + 1
txtDrawB3.Text = Val(txtDrawB3.Text) + 1
Case "22"
LbNxSt22.Text = Val(LbNxSt22.Text) + 1
txtS30.Text = Val(txtS30.Text) + 2
txtDrawB3.Text = Val(txtDrawB3.Text) + 1
Case "23"
LbNxSt23.Text = Val(LbNxSt23.Text) + 1
txtS30.Text = Val(txtS30.Text) + 3
txtDrawB3.Text = Val(txtDrawB3.Text) + 1
Case "24"
LbNxSt24.Text = Val(LbNxSt24.Text) + 1
txtS30.Text = Val(txtS30.Text) + 4
txtDrawB3.Text = Val(txtDrawB3.Text) + 1
Case "25"
LbNxSt25.Text = Val(LbNxSt25.Text) + 1
txtS30.Text = Val(txtS30.Text) + 5
txtDrawB3.Text = Val(txtDrawB3.Text) + 1
Case "26"
LbNxSt26.Text = Val(LbNxSt26.Text) + 1
txtS30.Text = Val(txtS30.Text) + 6
txtDrawB3.Text = Val(txtDrawB3.Text) + 1
Case "27"
LbNxSt27.Text = Val(LbNxSt27.Text) + 1
txtS30.Text = Val(txtS30.Text) + 7
txtDrawB3.Text = Val(txtDrawB3.Text) + 1
Case "28"
LbNxSt28.Text = Val(LbNxSt28.Text) + 1
txtS30.Text = Val(txtS30.Text) + 8
txtDrawB3.Text = Val(txtDrawB3.Text) + 1
Case "29"
LbNxSt29.Text = Val(LbNxSt29.Text) + 1
txtS30.Text = Val(txtS30.Text) + 9
txtDrawB3.Text = Val(txtDrawB3.Text) + 1
Case "30"
LbNxSt30.Text = Val(LbNxSt30.Text) + 1
txtS30.Text = Val(txtS30.Text) + 10
txtDrawB3.Text = Val(txtDrawB3.Text) + 1
Case "31"
LbNxSt31.Text = Val(LbNxSt31.Text) + 1
txtS40.Text = Val(txtS40.Text) + 1
txtDrawB4.Text = Val(txtDrawB4.Text) + 1
Case "32"
LbNxSt32.Text = Val(LbNxSt32.Text) + 1
txtS40.Text = Val(txtS40.Text) + 2
txtDrawB4.Text = Val(txtDrawB4.Text) + 1
Case "33"
LbNxSt33.Text = Val(LbNxSt33.Text) + 1
txtS40.Text = Val(txtS40.Text) + 3
txtDrawB4.Text = Val(txtDrawB4.Text) + 1
Case "34"
LbNxSt34.Text = Val(LbNxSt34.Text) + 1
txtS40.Text = Val(txtS40.Text) + 4
txtDrawB4.Text = Val(txtDrawB4.Text) + 1
Case "35"
LbNxSt35.Text = Val(LbNxSt35.Text) + 1
txtS40.Text = Val(txtS40.Text) + 5
txtDrawB4.Text = Val(txtDrawB4.Text) + 1
Case "36"
LbNxSt36.Text = Val(LbNxSt36.Text) + 1
txtS40.Text = Val(txtS40.Text) + 6
txtDrawB4.Text = Val(txtDrawB4.Text) + 1
Case "37"
LbNxSt37.Text = Val(LbNxSt37.Text) + 1
txtS40.Text = Val(txtS40.Text) + 7
txtDrawB4.Text = Val(txtDrawB4.Text) + 1
Case "38"
LbNxSt38.Text = Val(LbNxSt38.Text) + 1
txtS40.Text = Val(txtS40.Text) + 8
txtDrawB4.Text = Val(txtDrawB4.Text) + 1
Case "39"
LbNxSt39.Text = Val(LbNxSt39.Text) + 1
txtS40.Text = Val(txtS40.Text) + 9
txtDrawB4.Text = Val(txtDrawB4.Text) + 1
Case "40"
LbNxSt40.Text = Val(LbNxSt40.Text) + 1
txtS40.Text = Val(txtS40.Text) + 10
txtDrawB4.Text = Val(txtDrawB4.Text) + 1
Case "41"
LbNxSt41.Text = Val(LbNxSt41.Text) + 1
txtS50.Text = Val(txtS50.Text) + 1
txtDrawB5.Text = Val(txtDrawB5.Text) + 1
Case "42"
LbNxSt42.Text = Val(LbNxSt42.Text) + 1
txtS50.Text = Val(txtS50.Text) + 2
txtDrawB5.Text = Val(txtDrawB5.Text) + 1
Case "43"
LbNxSt43.Text = Val(LbNxSt43.Text) + 1
txtS50.Text = Val(txtS50.Text) + 3
txtDrawB5.Text = Val(txtDrawB5.Text) + 1
Case "44"
LbNxSt44.Text = Val(LbNxSt44.Text) + 1
txtS50.Text = Val(txtS50.Text) + 4
txtDrawB5.Text = Val(txtDrawB5.Text) + 1
Case "45"
LbNxSt45.Text = Val(LbNxSt45.Text) + 1
txtS50.Text = Val(txtS50.Text) + 5
txtDrawB5.Text = Val(txtDrawB5.Text) + 1
Case "46"
LbNxSt46.Text = Val(LbNxSt46.Text) + 1
txtS50.Text = Val(txtS50.Text) + 6
txtDrawB5.Text = Val(txtDrawB5.Text) + 1
Case "47"
LbNxSt47.Text = Val(LbNxSt47.Text) + 1
txtS50.Text = Val(txtS50.Text) + 7
txtDrawB5.Text = Val(txtDrawB5.Text) + 1
Case "48"
LbNxSt48.Text = Val(LbNxSt48.Text) + 1
txtS50.Text = Val(txtS50.Text) + 8
txtDrawB5.Text = Val(txtDrawB5.Text) + 1
Case "49"
LbNxSt49.Text = Val(LbNxSt49.Text) + 1
txtS50.Text = Val(txtS50.Text) + 9
txtDrawB5.Text = Val(txtDrawB5.Text) + 1
Case "50"
LbNxSt50.Text = Val(LbNxSt50.Text) + 1
txtS50.Text = Val(txtS50.Text) + 10
txtDrawB5.Text = Val(txtDrawB5.Text) + 1

If you can work out the relationship between the "number" and the control, you can get the control like this:
For ary = 1 To 30
Dim aryText = Me.Controls("TextNSt" & ary).Text.Split({","c})
For i = 0 To aryText.Length - 1
Dim s = aryText(i)
Dim n = CInt(s)
Dim LbNxStNum = n Mod 10
If LbNxStNum = 0 Then LbNxStNum = 10
Dim LbNxSt = Me.Controls("LbNxSt" & LbNxStNum)
LbNxSt.Text = (CInt(LbNxSt.Text) + 1).ToString()
Dim txtSNum = (Math.Floor(n / 10) + 1) * 10
Dim txtS = Me.Controls("txtS" & txtSNum)
txtS.Text = (CInt(txtS.Text) + LbNxStNum).ToString()
Dim txtDrawBNum = Math.Ceiling(n / 10)
Dim txtDrawB = Me.Controls("txtDrawB" & txtDrawBNum)
txtDrawB.Text = (CInt(txtDrawB.Text) + 1).ToString()
Next
Next
I may have off-by-one errors in there, so you may need to make adjustments.
If Me is not the container, change it to use the appropriate Panel/whatever container.

Related

Count Specific Column Name

I just want to know why vb not execute the October month data? Here was my code.
For col As Integer = 4 To ldtData.Columns.Count - 1
If ldtData.Columns(col).ColumnName.Substring(0, 1) = "1"
Then cnt1 = cnt1 + 1
ElseIf ldtData.Columns(col).ColumnName.Substring(0, 1) = "2"
Then cnt2 = cnt2 + 1
ElseIf ldtData.Columns(col).ColumnName.Substring(0, 1) = "3"
Then cnt3 = cnt3 + 1
ElseIf ldtData.Columns(col).ColumnName.Substring(0, 1) = "4"
Then cnt4 = cnt4 + 1
ElseIf ldtData.Columns(col).ColumnName.Substring(0, 1) = "5"
Then cnt5 = cnt5 + 1
ElseIf ldtData.Columns(col).ColumnName.Substring(0, 1) = "6"
Then cnt6 = cnt6 + 1
ElseIf ldtData.Columns(col).ColumnName.Substring(0, 1) = "7"
Then cnt7 = cnt7 + 1
ElseIf ldtData.Columns(col).ColumnName.Substring(0, 1) = "8"
Then cnt8 = cnt8 + 1
ElseIf ldtData.Columns(col).ColumnName.Substring(0, 1) = "9"
Then cnt9 = cnt9 + 1
ElseIf ldtData.Columns(col).ColumnName.Substring(0, 2) = "10"
Then cnt10 = cnt10 + 1
ElseIf ldtData.Columns(col).ColumnName.Substring(0, 2) = "11"
Then cnt11 = cnt11 + 1
ElseIf ldtData.Columns(col).ColumnName.Substring(0, 2) = "12"
Then cnt12 = cnt12 + 1
End If
Next
For month 1 until 9 will count for how many data based on individual month, but for month 10, 11, 12 . it will execute as cnt1. Why?
Problem solved by adding - in every expected output after substring method.
If ldtData.Columns(col).ColumnName.Substring(0, 1) = "1-"
.
.
.
ElseIf ldtData.Columns(col).ColumnName.Substring(0, 1) = "10-"

Loops causing my program to crash (Visual Basic)

I'm newish to VB and I'm having some issues. I'm using Repeat Until loops and my program is just crashing if I try to put anything of minor complexity in. I'm generating random numbers. I've isolated the issue to the following loop, and have removed other code that doesn't effect it.
ShowNO, Hidden, C, CA, R, RA, B, BA and all the B11 etc. values are defined as integers.
Hidden is confined to the range 11-19 in the HSB, so that aint the issue
Hidden = hsbDifficulty.Value * -1
If (Hidden = 11 Or 12) Then
RA = 5
CA = 5
BA = 5
End If
If (Hidden = 13 Or 14) Then
RA = 5
CA = 5
BA = 6
End If
If (Hidden = 15 Or 16 Or 17) Then
RA = 5
CA = 6
BA = 6
End If
If (Hidden = 18 Or 19) Then
RA = 6
CA = 6
BA = 6
End If
Randomize()
Do
Do
ShowNO = (Rnd() * 36)
Select Case ShowNO
Case Is = 0
B66 = 1
Case Is = 1
B11 = 1
Case Is = 2
B12 = 1
Case Is = 3
B13 = 1
Case Is = 4
B14 = 1
Case Is = 5
B15 = 1
Case Is = 6
B16 = 1
Case Is = 7
B21 = 1
Case Is = 8
B22 = 1
Case Is = 9
B23 = 1
Case Is = 10
B24 = 1
Case Is = 11
B25 = 1
Case Is = 12
B26 = 1
Case Is = 13
B31 = 1
Case Is = 14
B32 = 1
Case Is = 15
B33 = 1
Case Is = 16
B34 = 1
Case Is = 17
B35 = 1
Case Is = 18
B36 = 1
Case Is = 19
B41 = 1
Case Is = 20
B42 = 1
Case Is = 21
B43 = 1
Case Is = 22
B44 = 1
Case Is = 23
B45 = 1
Case Is = 24
B46 = 1
Case Is = 25
B51 = 1
Case Is = 26
B52 = 1
Case Is = 27
B53 = 1
Case Is = 28
B54 = 1
Case Is = 29
B55 = 1
Case Is = 30
B56 = 1
Case Is = 31
B61 = 1
Case Is = 32
B62 = 1
Case Is = 33
B63 = 1
Case Is = 34
B64 = 1
Case Is = 35
B65 = 1
Case Is = 36
B66 = 1
End Select
Loop Until (B11 + B12 + B13 + B14 + B15 + B16 + B21 + B22 + B23 + B24 + B25 + B26 + B31 + B32 + B33 + B34 + B35 + B36 + B41 + B42 + B43 + B44 + B45 + B46 + B51 + B52 + B53 + B54 + B55 + B56 + B61 + B62 + B63 + B64 + B65 + B66 = Hidden)
If (B11 + B12 + B13 + B14 + B15 + B16 > 0.9) Then
R = R + 1
End If
If (B21 + B22 + B23 + B24 + B25 + B26 > 0.9) Then
R = R + 1
End If
If (B31 + B32 + B33 + B34 + B35 + B36 > 0.9) Then
R = R + 1
End If
If (B41 + B42 + B43 + B44 + B45 + B46 > 0.9) Then
R = R + 1
End If
If (B51 + B52 + B53 + B54 + B55 + B56 > 0.9) Then
R = R + 1
End If
If (B61 + B62 + B63 + B64 + B65 + B66 > 0.9) Then
R = R + 1
End If
If (B11 + B21 + B31 + B41 + B51 + B61 > 0.9) Then
C = C + 1
End If
If (B12 + B22 + B32 + B42 + B52 + B62 > 0.9) Then
C = C + 1
End If
If (B13 + B23 + B33 + B43 + B53 + B63 > 0.9) Then
C = C + 1
End If
If (B14 + B24 + B34 + B44 + B54 + B64 > 0.9) Then
C = C + 1
End If
If (B15 + B25 + B35 + B45 + B55 + B65 > 0.9) Then
C = C + 1
End If
If (B16 + B26 + B36 + B46 + B56 + B66 > 0.9) Then
C = C + 1
End If
If (B11 + B12 + B13 + B21 + B22 + B23 > 0.9) Then
B = B + 1
End If
If (B31 + B32 + B33 + B41 + B42 + B43 > 0.9) Then
B = B + 1
End If
If (B51 + B52 + B53 + B61 + B62 + B63 > 0.9) Then
B = B + 1
End If
If (B14 + B15 + B16 + B24 + B25 + B26 > 0.9) Then
B = B + 1
End If
If (B34 + B35 + B36 + B44 + B45 + B46 > 0.9) Then
B = B + 1
End If
If (B54 + B55 + B56 + B64 + B65 + B66 > 0.9) Then
B = B + 1
End If
Loop Until (B > (BA - 1) And C > (CA - 1) And R > (RA - 1))
If you read the Documentation for Select Case in VB .Net, you will find out that you are doint it wrong :
Select Case ShowNo
'Use Case 0 Instead of Case Is = 0
Case 0
'Do some Stuff
End Select
Also, as mentionned by nabuchodonossor, the If at the begginning are not good :
'Proper way to write this :
If Hidden = 11 Or Hidden = 12 Then
Also, if you can give us the Exception that you encouter, we might be able to help you better.
Re-jigged the whole thing to an array which fixed the problem. Likely the huge amount of variables that made it crash. Thanks for the assistance.

Change specific row and column in Datagridview

I want to change specific row and column in Datagridview.
when I enter specific data in column x it will return any specific value in column Y. Otherwise data in column x will return value 7777 in column Y
But when I run this code, there were nothing change in column Y :
Private Sub Table1DataGridView_CellEndEdit(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Table1DataGridView.CellEndEdit
'For e.ColumnIndex + 1
If e.ColumnIndex = 2 Then
If Val(Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value) = 5 Then
Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 5
ElseIf Val(Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value) = 7 Then
Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 7
ElseIf Val(Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value) = 1 Then
Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 6
'1
ElseIf Val(Table1DataGridView.Rows(1).Cells(2).Value) = 1.5 Then
Table1DataGridView.Rows(1).Cells(e.ColumnIndex + 1).Value = 7.326
'2
ElseIf Val(Table1DataGridView.Rows(2).Cells(2).Value) = 2 Then
Table1DataGridView.Rows(2).Cells(e.ColumnIndex + 1).Value = 6.374
'3
ElseIf Val(Table1DataGridView.Rows(3).Cells(2).Value) = 2.5 Then
Table1DataGridView.Rows(3).Cells(e.ColumnIndex + 1).Value = 4.702
'4
ElseIf Val(Table1DataGridView.Rows(4).Cells(2).Value) = 2 Then
Table1DataGridView.Rows(4).Cells(e.ColumnIndex + 1).Value = 1.321
'5
ElseIf Val(Table1DataGridView.Rows(5).Cells(2).Value) = 2.5 Then
Table1DataGridView.Rows(5).Cells(e.ColumnIndex + 1).Value = 0.035
'6
ElseIf Val(Table1DataGridView.Rows(6).Cells(2).Value) = 4 Then
Table1DataGridView.Rows(6).Cells(e.ColumnIndex + 1).Value = 0.205
'7
ElseIf Val(Table1DataGridView.Rows(7).Cells(2).Value) = 2.5 Then
Table1DataGridView.Rows(7).Cells(e.ColumnIndex + 1).Value = 4.969
'8
ElseIf Val(Table1DataGridView.Rows(16).Cells(2).Value) = 4 Then
Table1DataGridView.Rows(16).Cells(e.ColumnIndex + 1).Value = 1.588
'9
ElseIf Val(Table1DataGridView.Rows(17).Cells(2).Value) = 2.5 Then
Table1DataGridView.Rows(17).Cells(e.ColumnIndex + 1).Value = 4.696
'10
ElseIf Val(Table1DataGridView.Rows(18).Cells(2).Value) = 2.5 Then
Table1DataGridView.Rows(18).Cells(e.ColumnIndex + 1).Value = 0.866
'11
ElseIf Val(Table1DataGridView.Rows(19).Cells(2).Value) = 2 Then
Table1DataGridView.Rows(19).Cells(e.ColumnIndex + 1).Value = 1.881
'12
ElseIf Val(Table1DataGridView.Rows(20).Cells(2).Value) = 2.5 Then
Table1DataGridView.Rows(20).Cells(e.ColumnIndex + 1).Value = 4.702
'13
ElseIf Val(Table1DataGridView.Rows(21).Cells(2).Value) = 2 Then
Table1DataGridView.Rows(21).Cells(e.ColumnIndex + 1).Value = 6.515
'14
ElseIf Val(Table1DataGridView.Rows(22).Cells(2).Value) = 1.5 Then
Table1DataGridView.Rows(22).Cells(e.ColumnIndex + 1).Value = 6.92671744
Else
Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 7777
End If
End If
End Sub
Then, I try to change this code, but return some same value at column Y :
Private Sub Table1DataGridView_CellEndEdit(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Table1DataGridView.CellEndEdit
'For e.ColumnIndex + 1
If e.ColumnIndex = 2 Then
If Val(Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value) = 5 Then
Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 5
ElseIf Val(Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value) = 7 Then
Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 7
ElseIf Val(Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value) = 1 Then
Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 6
'1
ElseIf Val(Table1DataGridView.Rows(1).Cells(2).Value) = 1.5 Then
Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 7.326
'2
ElseIf Val(Table1DataGridView.Rows(2).Cells(2).Value) = 2 Then
Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 6.374
'3
ElseIf Val(Table1DataGridView.Rows(3).Cells(2).Value) = 2.5 Then
Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 4.702
'4
ElseIf Val(Table1DataGridView.Rows(4).Cells(2).Value) = 2 Then
Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 1.321
'5
ElseIf Val(Table1DataGridView.Rows(5).Cells(2).Value) = 2.5 Then
Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 0.035
'6
ElseIf Val(Table1DataGridView.Rows(6).Cells(2).Value) = 4 Then
Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 0.205
'7
ElseIf Val(Table1DataGridView.Rows(7).Cells(2).Value) = 2.5 Then
Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 4.969
'8
ElseIf Val(Table1DataGridView.Rows(16).Cells(2).Value) = 4 Then
Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 1.588
'9
ElseIf Val(Table1DataGridView.Rows(17).Cells(2).Value) = 2.5 Then
Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 4.696
'10
ElseIf Val(Table1DataGridView.Rows(18).Cells(2).Value) = 2.5 Then
Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 0.866
'11
ElseIf Val(Table1DataGridView.Rows(19).Cells(2).Value) = 2 Then
Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 1.881
'12
ElseIf Val(Table1DataGridView.Rows(20).Cells(2).Value) = 2.5 Then
Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 4.702
'13
ElseIf Val(Table1DataGridView.Rows(21).Cells(2).Value) = 2 Then
Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 6.515
'14
ElseIf Val(Table1DataGridView.Rows(22).Cells(2).Value) = 1.5 Then
Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 6.92671744
Else
Table1DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = 7777
End If
End If
End Sub
Thanks for the help!
If the DataGridView is databound, you need modify the databound object not the grid directly. You can access that object through the DataBoundItem property:
Dim obj As MyObject = Table1DataGridView.CurrentRow.DataBoundItem
obj.MyProperty = newValue
just set MyObject and MyProperty as appropriate

Excel VBA - Run-time error 1004

When I run the code below I get the annoying 1004 error.
Sub TEST_MACRO()
Dim shSource As Worksheet
Dim shDest As Worksheet
Dim DateRange As Range
Dim i As Integer, nullcounter As Integer, nullcounterov As Integer, tablelength As Integer, tablelengthov As Integer, DateRangeSize As Integer
Dim q As Integer
Set shSource = ThisWorkbook.Sheets("Sheet1")
Set shDest = ThisWorkbook.Sheets("Sheet2")
Set DateRange = shSource.Application.InputBox("Select date", Type:=8)
DateRangeSize = DateRange.Rows.Count
nullcounter = 0
nullcounterov = 0
tablelength = 3
tablelengthov = 3
For q = 0 To DateRangeSize - 1
shDest.Range("C4:I17").ClearContents
'THIS IS THE CODE FOR ABC
For i = 0 To 3
If IsEmpty(shSource.Cells(DateRange.Row + q, 2 + i)) = True Or shSource.Cells(DateRange.Row + q, 2 + i) = 0 Then
nullcounter = nullcounter + 1
Else
shDest.Cells(4 + i - nullcounter, 4) = shSource.Cells(DateRange.Row + q, 2 + i)
shDest.Cells(4 + i - nullcounter, 5) = shSource.Cells(DateRange.Row + q, 6 + i)
shDest.Cells(4 + i - nullcounter, 3) = shSource.Cells(1, 2 + i)
tablelength = tablelength + 1
End If
Next
'THIS IS THE CODE FOR XYZ
For i = 0 To 6
If IsEmpty(shSource.Cells(DateRange.Row + q, 10 + i)) = True Or shSource.Cells(DateRange.Row + q, 10 + i) = 0 Then
nullcounterov = nullcounterov + 1
Else
shDest.Cells(4 + i - nullcounterov, 8) = shSource.Cells(DateRange.Row + q, 10 + i)
shDest.Cells(4 + i - nullcounterov, 9) = shSource.Cells(DateRange.Row + q, 17 + i)
shDest.Cells(4 + i - nullcounterov, 7) = shSource.Cells(1, 10 + i)
tablelengthov = tablelengthov + 1
End If
Next
Next
End Sub
The excel sheet I run this on looks like this:
http://i.imgur.com/V7tWTKq.png
The code works for ABC but it doesn't for XYZ. I'm guessing the 0 value cells are messing it up but I don't understand why.
The goal of the code is:
User is prompted to select a range of size DateRangeSize.
For each row in the range the code copies the values of ABC, ABC-D, XYZ and XYZ-D if they aren't 0 and writes them to sheet 2.
If the number of rows in the range is 1, the code works fine. But if the number of rows is greater than 1, I get the 1004 error where this part of the code is highlight:
shDest.Cells(4 + i - nullcounterov, 8) = shSource.Cells(DateRange.Row + q, 10 + i)
I appreciate the help.
EDIT: I just want to add that the code ALWAYS works for ABC. If the number of rows is 2 then the ABC values for the second row are printed in sheet2 but the code breaks when it attempts to do the same for XYZ.
EDIT 2: I added 0 values to the ABC portion but the code still works for ABC! This is so frustrating.
I figured it out. I defined
nullcounter = 0
nullcounterov = 0
tablelength = 3
tablelengthov = 3
Outside of the loop so they kept on increasing.
shDest.Cells(4 + i - nullcounterov, 8) = shSource.Cells(DateRange.Row + q, 10 + i)
Eventually 4 + i << nullcounterov and excel tried to write to a cell that didn't exist.
The fixed code:
Sub TEST_MACRO()
Dim shSource As Worksheet
Dim shDest As Worksheet
Dim DateRange As Range
Dim i As Integer, nullcounter As Integer, nullcounterov As Integer, tablelength As Integer, tablelengthov As Integer, DateRangeSize As Integer
Dim q As Integer
Set shSource = ThisWorkbook.Sheets("Sheet1")
Set shDest = ThisWorkbook.Sheets("Sheet2")
Set DateRange = shSource.Application.InputBox("Select date", Type:=8)
DateRangeSize = DateRange.Rows.Count
For q = 0 To DateRangeSize - 1
nullcounter = 0
nullcounterov = 0
tablelength = 3
tablelengthov = 3
shDest.Range("C4:I17").ClearContents
'THIS IS THE CODE FOR ABC
For i = 0 To 3
If IsEmpty(shSource.Cells(DateRange.Row + q, 2 + i)) = True Or shSource.Cells(DateRange.Row + q, 2 + i) = 0 Then
nullcounter = nullcounter + 1
Else
shDest.Cells(4 + i - nullcounter, 4) = shSource.Cells(DateRange.Row + q, 2 + i)
shDest.Cells(4 + i - nullcounter, 5) = shSource.Cells(DateRange.Row + q, 6 + i)
shDest.Cells(4 + i - nullcounter, 3) = shSource.Cells(1, 2 + i)
tablelength = tablelength + 1
End If
Next
'THIS IS THE CODE FOR XYZ
For i = 0 To 6
If IsEmpty(shSource.Cells(DateRange.Row + q, 10 + i)) = True Or shSource.Cells(DateRange.Row + q, 10 + i) = 0 Then
nullcounterov = nullcounterov + 1
Else
shDest.Cells(4 + i - nullcounterov, 8) = shSource.Cells(DateRange.Row + q, 10 + i)
shDest.Cells(4 + i - nullcounterov, 9) = shSource.Cells(DateRange.Row + q, 17 + i)
shDest.Cells(4 + i - nullcounterov, 7) = shSource.Cells(1, 10 + i)
tablelengthov = tablelengthov + 1
End If
Next
Next
End Sub

Outputting sine waves on xy plots — stuck on certain parts

Please help I want to know exactly what is going on in this code for a questions and answers exam tomorrow.
I don't need any help with writing the code because that would be cheating. I made a tiny few mistakes please forgive me I rectified most of theses I don't need help with the mistakes just with the comments and understanding mostly how it works.
Private Sub Command1_Click()
MSComm1.Output = "83" + Chr$(13)
End Sub
Private Sub Form_Load()
MSC1.PortOpen = True
Average_val = 0
minimum_val = 255
maximum_val = 0
Screenshotofsinewave.Left = 0
Screenshotofsinewave.Channel(0).TraceVisible = True
Screenshotofsinewave.Channel(0).MarkersVisible = True
sumofall_val = 0
Screenshotofsinewave.TitleVisible = False
Screenshotofsinewave.Top = 0
Screenshotofrectifiedsinewave.TitleVisible = False
Screenshotofrectifiedsinewave.Channel(0).TraceVisible = True
Screenshotofrectifiedsinewave.Channel(0).MarkersVisible = True
Screenshotofrectifiedsinewave.Top = 0
Screenshotofrectifiedsinewave.Left = 0
Screenshotoflevelshiftedsinewave.Top = 0
Screenshotoflevelshiftedsinewave.Left = 0
Screenshotoflevelshiftedsinewave.TitleVisible = False
Screenshotoflevelshiftedsinewave.Channel(0).TraceVisible = True
Screenshotoflevelshiftedsinewave.Channel(0).MarkersVisible = True
End Sub
Private Sub MSC1_OnComm()
Dim number_val
Dim number1_val
Dim Average_val
Dim com1_val
p = 0
q = 0
r = 0
s = 0
t = 0
Dim Xarr(50) As Single
Dim Yarr(50) As Single
Dim number2_val
Dim number3_val
Dim Snapshotofsinewave
Dim string1_out As String
Dim string1_in As String
Dim counter As Single
Dim sample_rate As Integer
Select Case MSC1.CommEvent
Case comEvReceive
minimum_val = 255
string1_in = MSC1.Input
Screenshotofsinewave.Channel(0).Clear
Screenshotofrectifiedsinewave.Channel(0).Clear
Screenshotoflevelshiftedsinewave.Channel(0).Clear
counter = 0
comm_count = comm_count + 1
For sample_rate = 1 To 150 Step 3
string1_out = Mid(string1_in, sample_rate, 3)
counter = counter + 1
number_val = Val(string1_out)
Xarr(counter) = counter
Yarr(counter) = number_val
Screenshotofsinewave.Channel(0).AddXY counter, number_val
If number_val > maximum_val Then
maximum_val = number_val
MaxVoltage.Value = maximum_val
End If
If number_val < minimum_val Then
minimum_val = number_val
MinVoltage.Value = number_val
End If
sumofall_val = number_val + sumofall_val
Average_value = sumofall_val / 50
AverageVoltage.Value = Average_value
Next sample_rate
counter = 0
sumofall_val = 0
For sample_rate = 1 To 150 Step 3
string1_out = Mid(string1_in, sample_rate, 3)
counter = counter + 1
number_val = Val(string1_out)
number_val = number1_val - Average_value
number_val = numer_val
If num_val1 < 0 Then
number_val = number_val * -1
End If
Xarr(counter) = counter
Yarr(counter) = number1_val
Screenshotofrectifiedsinewave.Channel(0).AddXY counter, number1_val
Next sample_rate
counter = 0
For sample_rate = 1 To 150 Step 3
string1_out = Mid(string1_in, sample_rate, 3)
Count = Count + 1
number_val = Val(string1_out)
number2_val = number_val + Average_value
Xarr(Count) = counter
Yarr(Count) = number2_val
LevelShifted.Channel(0).AddXY Count, number_val2
sumofall_val = number_val + sumofall_val
Next sample_rate
counter = com1_val
Snapshotofsinewave.Value = com1_val
End Select
End Sub
Private Sub Frame4_DragDrop(Source As Control, X As Single, Y As Single)
End Sub
Private Sub Uploaddata_Click()
If GXSwitch1.SwitchOn = True Then
led1.LampOn = True
p = 8
Else
led1.LampOn = False
p = 0
End If
If GXSwitch2.SwitchOn = True Then
led2.LampOn = True
q = 4
Else
led2.LampOn = False
q = 0
End If
If GXSwitch3.SwitchOn = True Then
led3.LampOn = True
r = 4
Else
led3.LampOn = False
r = 0
End If
If GXSwitch4.SwitchOn = True Then
led4.LampOn = True
s = 8
Else
led4.LampOn = False
s = 0
End If
t = p + q + r + s
If t = 0 Then
MSC1.Output = "0" + Chr$(13)
End If
If t = 1 Then
MSC1.Output = "1" + Chr$(13)
End If
If t = 2 Then
MSC1.Output = "2" + Chr$(13)
End If
If t = 3 Then
MSC1.Output = "3" + Chr$(13)
End If
If t = 4 Then
MSC1.Output = "4" + Chr$(13)
End If
If t = 5 Then
MSC1.Output = "5" + Chr$(13)
End If
If t = 6 Then
MSC1.Output = "6" + Chr$(13)
End If
If t = 7 Then
MSC1.Output = "7" + Chr$(13)
End If
If t = 8 Then
MSC1.Output = "8" + Chr$(13)
End If
If t = 9 Then
MSC1.Output = "9" + Chr$(13)
End If
If t = 10 Then
MSC1.Output = "10" + Chr$(13)
End If
If t = 11 Then
MSC1.Output = "11" + Chr$(13)
End If
If t = 12 Then
MSC1.Output = "12" + Chr$(13)
End If
If t = 13 Then
MSC1.Output = "13" + Chr$(13)
End If
If t = 14 Then
MSC1.Output = "14" + Chr$(13)
End If
If t = 15 Then
MSC1.Output = "15" + Chr$(13)
End If
End Sub
Depends on several factors...
Dim average_val, x As Decimal ' decimal
Dim average_val = 3.2D ' decimal if Option Infer On
Dim average_val = 3.2D ' object with boxed decimal if Option Infer Off
Dim average_val ' Object if Option Strict Off, otherwise an error