can someone help me to include aces into my blackjack program - vb.net

I have been trying to create a simple blackjack game where the user picks cards and decides whether to hit or stick and if their cards go above 21 they lose. however i do not know how to add in aces so that the program will choose whether the ace will be a 1 or an 11.
please help
this is my code
Module Module1
Sub Main()
Dim cards(4, 13) As Integer
Dim value As Integer
Dim value2 As Integer
Dim royal As String
Dim suit As String
Dim card1 As Integer
Dim card2 As Integer
Dim card3 As Integer
Dim card4 As Integer
Dim card5 As Integer
Dim card6 As Integer
Dim card7 As Integer
Dim answer As String
Dim total As Integer
Randomize()
value = (3 * Rnd()) + 1
value2 = (11 * Rnd()) + 1
'suits: 1= hearts
'2= diamonds
'3=spades
'4=clubs
cards(1, 1) = 2
cards(2, 1) = 2
cards(3, 1) = 2
cards(4, 1) = 2
cards(1, 2) = 3
cards(2, 2) = 3
cards(3, 2) = 3
cards(4, 2) = 3
cards(1, 3) = 4
cards(2, 3) = 4
cards(3, 3) = 4
cards(4, 3) = 4
cards(1, 4) = 5
cards(2, 4) = 5
cards(3, 4) = 5
cards(4, 4) = 5
cards(1, 5) = 6
cards(2, 5) = 6
cards(3, 5) = 6
cards(4, 5) = 6
cards(1, 6) = 7
cards(2, 6) = 7
cards(3, 6) = 7
cards(4, 6) = 7
cards(1, 7) = 8
cards(2, 7) = 8
cards(3, 7) = 8
cards(4, 7) = 8
cards(1, 8) = 9
cards(2, 8) = 9
cards(3, 8) = 9
cards(4, 8) = 9
cards(1, 9) = 10
cards(2, 9) = 10
cards(3, 9) = 10
cards(4, 9) = 10
'jacks
cards(1, 10) = 10
cards(2, 10) = 10
cards(3, 10) = 10
cards(4, 10) = 10
'queens
cards(1, 11) = 10
cards(2, 11) = 10
cards(3, 11) = 10
cards(4, 11) = 10
'kings
cards(1, 12) = 10
cards(2, 12) = 10
cards(3, 12) = 10
cards(4, 12) = 10
If value2 = 1 Then
royal = "2"
ElseIf value2 = 2 Then
royal = "3"
ElseIf value2 = 3 Then
royal = "4"
ElseIf value2 = 4 Then
royal = "5"
ElseIf value2 = 5 Then
royal = "6"
ElseIf value2 = 6 Then
royal = "7"
ElseIf value2 = 7 Then
royal = "8"
ElseIf value2 = 8 Then
royal = "9"
ElseIf value2 = 9 Then
royal = "10"
ElseIf value2 = 10 Then
royal = "jack"
ElseIf value2 = 11 Then
royal = "queen"
ElseIf value2 = 12 Then
royal = "king"
End If
If value = 1 Then
suit = "hearts"
ElseIf value = 2 Then
suit = "diamonds"
ElseIf value = 3 Then
suit = "spades"
ElseIf value = 4 Then
suit = "clubs"
End If
Console.WriteLine("your first card is the {0} of {1}", royal, suit)
card1 = cards(value, value2)
value = (3 * Rnd()) + 1
value2 = (11 * Rnd()) + 1
If value2 = 1 Then
royal = "2"
ElseIf value2 = 2 Then
royal = "3"
ElseIf value2 = 3 Then
royal = "4"
ElseIf value2 = 4 Then
royal = "5"
ElseIf value2 = 5 Then
royal = "6"
ElseIf value2 = 6 Then
royal = "7"
ElseIf value2 = 7 Then
royal = "8"
ElseIf value2 = 8 Then
royal = "9"
ElseIf value2 = 9 Then
royal = "10"
ElseIf value2 = 10 Then
royal = "jack"
ElseIf value2 = 11 Then
royal = "queen"
ElseIf value2 = 12 Then
royal = "king"
End If
If value = 1 Then
suit = "hearts"
ElseIf value = 2 Then
suit = "diamonds"
ElseIf value = 3 Then
suit = "spades"
ElseIf value = 4 Then
suit = "clubs"
End If
Console.WriteLine("your second card is the {0} of {1}", royal, suit)
card2 = cards(value, value2)
Console.ReadKey()
total = card1 + card2
If total > 21 Then
Console.WriteLine("You lose")
Console.ReadLine()
Else
Console.WriteLine("Do you want to hit or stick?")
answer = Console.ReadLine
If answer = "hit" Then
value = (3 * Rnd()) + 1
value2 = (11 * Rnd()) + 1
If value2 = 1 Then
royal = "2"
ElseIf value2 = 2 Then
royal = "3"
ElseIf value2 = 3 Then
royal = "4"
ElseIf value2 = 4 Then
royal = "5"
ElseIf value2 = 5 Then
royal = "6"
ElseIf value2 = 6 Then
royal = "7"
ElseIf value2 = 7 Then
royal = "8"
ElseIf value2 = 8 Then
royal = "9"
ElseIf value2 = 9 Then
royal = "10"
ElseIf value2 = 10 Then
royal = "jack"
ElseIf value2 = 11 Then
royal = "queen"
ElseIf value2 = 12 Then
royal = "king"
End If
If value = 1 Then
suit = "hearts"
ElseIf value = 2 Then
suit = "diamonds"
ElseIf value = 3 Then
suit = "spades"
ElseIf value = 4 Then
suit = "clubs"
End If
Console.WriteLine("your card is the {0} of {1}", royal, suit)
card3 = cards(value, value2)
Console.ReadKey()
total = card1 + card2 + card3
If total > 21 Then
Console.WriteLine("You lose")
Console.ReadLine()
Else
Console.WriteLine("Do you want to hit or stick?")
answer = Console.ReadLine
If answer = "hit" Then
value = (3 * Rnd()) + 1
value2 = (11 * Rnd()) + 1
If value2 = 1 Then
royal = "2"
ElseIf value2 = 2 Then
royal = "3"
ElseIf value2 = 3 Then
royal = "4"
ElseIf value2 = 4 Then
royal = "5"
ElseIf value2 = 5 Then
royal = "6"
ElseIf value2 = 6 Then
royal = "7"
ElseIf value2 = 7 Then
royal = "8"
ElseIf value2 = 8 Then
royal = "9"
ElseIf value2 = 9 Then
royal = "10"
ElseIf value2 = 10 Then
royal = "jack"
ElseIf value2 = 11 Then
royal = "queen"
ElseIf value2 = 12 Then
royal = "king"
End If
If value = 1 Then
suit = "hearts"
ElseIf value = 2 Then
suit = "diamonds"
ElseIf value = 3 Then
suit = "spades"
ElseIf value = 4 Then
suit = "clubs"
End If
Console.WriteLine("your card is the {0} of {1}", royal, suit)
card4 = cards(value, value2)
Console.ReadKey()
total = card1 + card2 + card3 + card4
If total > 21 Then
Console.WriteLine("You lose")
Console.ReadLine()
Else
Console.WriteLine("Do you want to hit or stick?")
answer = Console.ReadLine
If answer = "hit" Then
value = (3 * Rnd()) + 1
value2 = (11 * Rnd()) + 1
If value2 = 1 Then
royal = "2"
ElseIf value2 = 2 Then
royal = "3"
ElseIf value2 = 3 Then
royal = "4"
ElseIf value2 = 4 Then
royal = "5"
ElseIf value2 = 5 Then
royal = "6"
ElseIf value2 = 6 Then
royal = "7"
ElseIf value2 = 7 Then
royal = "8"
ElseIf value2 = 8 Then
royal = "9"
ElseIf value2 = 9 Then
royal = "10"
ElseIf value2 = 10 Then
royal = "jack"
ElseIf value2 = 11 Then
royal = "queen"
ElseIf value2 = 12 Then
royal = "king"
End If
If value = 1 Then
suit = "hearts"
ElseIf value = 2 Then
suit = "diamonds"
ElseIf value = 3 Then
suit = "spades"
ElseIf value = 4 Then
suit = "clubs"
End If
Console.WriteLine("your card is the {0} of {1}", royal, suit)
card5 = cards(value, value2)
Console.ReadKey()
total = card1 + card2 + card3 + card4 + card5
If total > 21 Then
Console.WriteLine("You lose")
Console.ReadLine()
Else
Console.WriteLine("Do you want to hit or stick?")
answer = Console.ReadLine
If answer = "hit" Then
value = (3 * Rnd()) + 1
value2 = (11 * Rnd()) + 1
If value2 = 1 Then
royal = "2"
ElseIf value2 = 2 Then
royal = "3"
ElseIf value2 = 3 Then
royal = "4"
ElseIf value2 = 4 Then
royal = "5"
ElseIf value2 = 5 Then
royal = "6"
ElseIf value2 = 6 Then
royal = "7"
ElseIf value2 = 7 Then
royal = "8"
ElseIf value2 = 8 Then
royal = "9"
ElseIf value2 = 9 Then
royal = "10"
ElseIf value2 = 10 Then
royal = "jack"
ElseIf value2 = 11 Then
royal = "queen"
ElseIf value2 = 12 Then
royal = "king"
End If
If value = 1 Then
suit = "hearts"
ElseIf value = 2 Then
suit = "diamonds"
ElseIf value = 3 Then
suit = "spades"
ElseIf value = 4 Then
suit = "clubs"
End If
Console.WriteLine("your card is the {0} of {1}", royal, suit)
card6 = cards(value, value2)
Console.ReadKey()
total = card1 + card2 + card3 + card4 + card5 + card6
If total > 21 Then
Console.WriteLine("You lose")
Console.ReadLine()
Else
Console.WriteLine("Do you want to hit or stick?")
answer = Console.ReadLine
If answer = "hit" Then
value = (3 * Rnd()) + 1
value2 = (11 * Rnd()) + 1
If value2 = 1 Then
royal = "2"
ElseIf value2 = 2 Then
royal = "3"
ElseIf value2 = 3 Then
royal = "4"
ElseIf value2 = 4 Then
royal = "5"
ElseIf value2 = 5 Then
royal = "6"
ElseIf value2 = 6 Then
royal = "7"
ElseIf value2 = 7 Then
royal = "8"
ElseIf value2 = 8 Then
royal = "9"
ElseIf value2 = 9 Then
royal = "10"
ElseIf value2 = 10 Then
royal = "jack"
ElseIf value2 = 11 Then
royal = "queen"
ElseIf value2 = 12 Then
royal = "king"
End If
If value = 1 Then
suit = "hearts"
ElseIf value = 2 Then
suit = "diamonds"
ElseIf value = 3 Then
suit = "spades"
ElseIf value = 4 Then
suit = "clubs"
End If
Console.WriteLine("your card is the {0} of {1}", royal, suit)
card7 = cards(value, value2)
Console.ReadKey()
total = card1 + card2 + card3 + card4 + card5 + card6 + card7
ElseIf answer = "stick" Then
Console.WriteLine("your final score is {0}", total)
Console.ReadLine()
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End Sub
End Module

Use a rule like: Aces are worth 11, unless the total exceeds 21, then they are worth 1. You have to recalculate the total for each ace in the hand until the total is less then or equal to 21. This might not be a truly optimal strategy, but good enough for learning how to code.

The thing to remember is that THERE IS NO "CHOICE" to count an ace as 1 or 11. It is strictly determined by the hand. Count all aces as 1. Then, if the hand contains one or more aces, and totals less than 12, add 10. That's it. Only one ace can ever be counted as 11, and only when it MUST be.
The really tricky part of programming blackjack is to remember that you need to keep track of the hand total and an extra bit of info to indicate if the total is "soft" or not (if you added the 10 above, it's soft).

Related

Sorting a range of values correctly

The code for the question "Sorting a range of values correctly in VBA" is below:
'Legend
Worksheets(1).Range("U1").Value = "Legend"
Worksheets(1).Range("U1:V1").Merge
'Classified Cases in Ranges
Worksheets(1).Range("U2:V2").Merge
Worksheets(1).Range("U2").Value = "Classified Cases in Ranges"
'Format the Legend
Worksheets(1).Range("U3:U40").NumberFormat = "#"
Worksheets(1).Range("V3:V40").NumberFormat = "#"
'Legend values
Worksheets(1).Range("U3").Value = 0
Worksheets(1).Range("U4").Value = 1
Worksheets(1).Range("U5").Value = 2
Worksheets(1).Range("U6").Value = 3
Worksheets(1).Range("U7").Value = 4
Worksheets(1).Range("U8").Value = 5
Worksheets(1).Range("U9").Value = 6
Worksheets(1).Range("U10").Value = 7
Worksheets(1).Range("U11").Value = 8
Worksheets(1).Range("U12").Value = 9
Worksheets(1).Range("U13").Value = 10
Worksheets(1).Range("U14").Value = 11
Worksheets(1).Range("U15").Value = 12
Worksheets(1).Range("U16").Value = 13
Worksheets(1).Range("U17").Value = 14
Worksheets(1).Range("U18").Value = 15
Worksheets(1).Range("U19").Value = 16
Worksheets(1).Range("U20").Value = 17
Worksheets(1).Range("U21").Value = 18
Worksheets(1).Range("U22").Value = 19
Worksheets(1).Range("U23").Value = 20
Worksheets(1).Range("U24").Value = 21
Worksheets(1).Range("U25").Value = 22
Worksheets(1).Range("U26").Value = 23
Worksheets(1).Range("U27").Value = 24
Worksheets(1).Range("U28").Value = 25
Worksheets(1).Range("U29").Value = 26
Worksheets(1).Range("U30").Value = 27
Worksheets(1).Range("U31").Value = 28
Worksheets(1).Range("U32").Value = 29
Worksheets(1).Range("U33").Value = 30
Worksheets(1).Range("U34").Value = 31
Worksheets(1).Range("U35").Value = 32
Worksheets(1).Range("U36").Value = 33
Worksheets(1).Range("U37").Value = 34
Worksheets(1).Range("U38").Value = 35
Worksheets(1).Range("U39").Value = 36
Worksheets(1).Range("U40").Value = "(blank)"
'Classified Cases in Ranges values
Worksheets(1).Range("V3").Value = "0-9"
Worksheets(1).Range("V4").Value = "10-19"
Worksheets(1).Range("V5").Value = "20-29"
Worksheets(1).Range("V6").Value = "30-39"
Worksheets(1).Range("V7").Value = "40-49"
Worksheets(1).Range("V8").Value = "50-59"
Worksheets(1).Range("V9").Value = "60-69"
Worksheets(1).Range("V10").Value = "70-79"
Worksheets(1).Range("V11").Value = "80-89"
Worksheets(1).Range("V12").Value = "90-99"
Worksheets(1).Range("V13").Value = "100-109"
Worksheets(1).Range("V14").Value = "110-119"
Worksheets(1).Range("V15").Value = "120-129"
Worksheets(1).Range("V16").Value = "130-139"
Worksheets(1).Range("V17").Value = "140-149"
Worksheets(1).Range("V18").Value = "150-159"
Worksheets(1).Range("V19").Value = "160-169"
Worksheets(1).Range("V20").Value = "170-179"
Worksheets(1).Range("V21").Value = "180-189"
Worksheets(1).Range("V22").Value = "190-199"
Worksheets(1).Range("V23").Value = "200-209"
Worksheets(1).Range("V24").Value = "210-219"
Worksheets(1).Range("V25").Value = "220-229"
Worksheets(1).Range("V26").Value = "230-239"
Worksheets(1).Range("V27").Value = "240-249"
Worksheets(1).Range("V28").Value = "250-259"
Worksheets(1).Range("V29").Value = "260-269"
Worksheets(1).Range("V30").Value = "270-279"
Worksheets(1).Range("V31").Value = "280-289"
Worksheets(1).Range("V32").Value = "290-299"
Worksheets(1).Range("V33").Value = "300-309"
Worksheets(1).Range("V34").Value = "310-319"
Worksheets(1).Range("V35").Value = "320-329"
Worksheets(1).Range("V36").Value = "330-339"
Worksheets(1).Range("V37").Value = "340-349"
Worksheets(1).Range("V38").Value = "350-359"
Worksheets(1).Range("V39").Value = ">=360"
Worksheets(1).Range("V40").Value = "(blank)"
'***************************************************
'***************************************************
'***************************************************
'Move to US MACRO worksheet
Worksheets(2).Activate
'Pivot Table 1
'Pivot Table 1
'Pivot Table 1
LastRow = Cells(Rows.Count, 2).End(xlUp).Row
'Use vlookup() function and perform conditional formatting
For x = 4 To (LastRow - 1)
Cells(x, 1).FormulaR1C1 = "=VLOOKUP(RC[1],'US Master Macro'!R[-1]C[20]:R[36]C[21],2,FALSE)"
If Cells(x, 2) >= 8 Then
Cells(x, 1).Interior.ColorIndex = 3
Cells(x, 1).Font.ThemeColor = xlThemeColorDark1
Cells(x, 1).Font.Bold = True
Cells(x, 2).Interior.ColorIndex = 3
Cells(x, 2).Font.ThemeColor = xlThemeColorDark1
Cells(x, 2).Font.Bold = True
Cells(x, 3).Interior.ColorIndex = 3
Cells(x, 3).Font.ThemeColor = xlThemeColorDark1
Cells(x, 3).Font.Bold = True
Else
Cells(x, 1).Interior.ColorIndex = 2
End If
Next x
"I have the following data in a pivot table. To the left, is described a range and to the right the number of cells within that range. I would like to learn a code that correctly arranges the range in the left and color codes anything after "80-89". I have used the sort function but it doesn't seem to work. Please help."
Days Count of PR ID
"10-19" 656
"0-9" 480
"20-29" 190
"30-39" 115
"40-49" 65
"50-59" 47
"70-79" 28
"60-69" 23
"80-89" 12
"110-119" 3
"120-129" 2
"130-139" 1
"100-109" 1
"180-189" 1

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-"

Excel VBA UDF Count Ifs with data conversion

I am attempting to write a UDF using Countifs. I want the user to be able to select criteria 1 (held as X1) and the month (held as month_X). The month in the lookup table is in numbers so I want the function to convert the month name to the month number.
This works - but is there a better way to convert the data? I.e. less lines of code?
Function CountIF_Custom(X1 As String, Month_X As String)
Dim MonthArray(1 To 12, 1 To 2) As Variant
MonthArray(1, 1) = "January"
MonthArray(2, 1) = "February"
MonthArray(3, 1) = "March"
MonthArray(4, 1) = "Apr"
MonthArray(5, 1) = "May"
MonthArray(6, 1) = "June"
MonthArray(7, 1) = "July"
MonthArray(8, 1) = "August"
MonthArray(9, 1) = "September"
MonthArray(10, 1) = "October"
MonthArray(11, 1) = "November"
MonthArray(12, 1) = "December"
MonthArray(1, 2) = "1"
MonthArray(2, 2) = "2"
MonthArray(3, 2) = "3"
MonthArray(4, 2) = "4"
MonthArray(5, 2) = "5"
MonthArray(6, 2) = "6"
MonthArray(7, 2) = "7"
MonthArray(8, 2) = "8"
MonthArray(9, 2) = "9"
MonthArray(10, 2) = "10"
MonthArray(11, 2) = "11"
MonthArray(12, 2) = "12"
For x = 1 To 12
If Month_X = MonthArray(x, 1) Then
Month_X = MonthArray(x, 2)
End If
Next x
CountIF_Custom = Application.CountIfs(Sheets("SomeSheet").Range("SomeRange"), X1, Sheets("SomeSheet").Range("SomeRange"), Month_X)
End Function
No need for the array, you can use a for loop with the MonthName function:
Function CountIF_Custom(X1 As String, Month_X As String)
Dim x As Integer
For x = 1 To 12
If Month_X = MonthName(x) Then
Month_X = CStr(x)
Exit For
End If
Next x
CountIF_Custom = Application.CountIfs(Sheets("SomeSheet").Range("SomeRange"), X1, Sheets("SomeSheet").Range("SomeRange"), Month_X)
End Function

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

Making automated ID in VBA

I am new in and is trying to give an automated ID in which i used the following code:
y = 0
If txtdsgnation = "Plumbing" Then
x = "P"
Do Until Cells(i, 3) = ""
If Cells(i, 3) = "Plumbing" Then
y = y + 1
End If
i = i + 1
Loop
End If
If txtdsgnation = "Electricity" Then
x = "E"
Do Until Cells(i, 3) = ""
If Cells(i, 3) = "Electricity" Then
y = y + 1
End If
i = i + 1
Loop
End If
If txtdsgnation = "Fittings" Then
x = "F"
Do Until Cells(i, 3) = ""
If Cells(i, 3) = "Fittings" Then
y = y + 1
End If
i = i + 1
Loop
End If
If txtdsgnation = "Lift maintenance" Then
x = "L"
Do Until Cells(i, 3) = ""
If Cells(i, 3) = "Lift maintenance" Then
y = y + 1
End If
i = i + 1
Loop
End If
z = 100 + y
txtID = x + "-" + z
The thing i am trying to do is that if i add a new employee, for example a plumber and I already have 2 plumbers txtID will automatically become "P-102"
Thanks in advance
In the loop, the y is always getting initialized with 0. So, y has no chance to become more than 1. Please try to put the y out of the loop.