I have the following number strings:
Textbox1.text / or Textbox1.Line= 1,2,3,19,29,78,48,39,40,51,53,54,69,70,71,73
Textbox2.text= / or Textbox2.Line= 1,9,3,31,29,78,45,39,40,51,59,54,69,70,71,73
textbox3.text= / or TextBox3.Line= 11,4,3,31,29,78,45,39,40,53,59,54,6974,75,76
and Others ...
How can I make a Count that shows how many numbers from 1 to 10 are in the Textbox, how many numbers from 11-20, how many numbers from 31-40, and so on. Example: On line 1 - we will have 3 small numbers from 1 to 10 (1,2,3).
To do this You will have to split the string into an array or list then compare the values in the array or list to the numbers you want. like this
Dim arr1 As New List (Of String)
arr1.AddRange(Split (TextBox1.Text, ","))
Dim final As String
Dim count As Integer = 0
For Each item As String In arr1
If CInt(item) >= 1 And CInt(item) <= 10 Then
count+=1
'replace 10 with the maximum number you want and 1 with the minimum number.
final&=item & " "
End If
Next
Msgbox("There are " & count & "numbers" & final)
You can easily convert a string containing a comma-separated list of integers into a string array like this
Dim s = "1,2,3,19,29,78,48,39,40,51,53,54,69,70,71,73"
Dim parts = s.Split(","c)
Then convert the string array into a list of integers
Dim numbers = New List(Of Integer)
For Each p As String In parts
Dim i As Integer
If Integer.TryParse(p, i) Then
numbers.Add(i)
End If
Next
Now comes the counting part. With LINQ you can write
Dim tens = From n In numbers
Group n By Key = (n - 1) \ 10 Into Group
Order By Key
Select Text = $"{ 10 * Key + 1} - {10 * Key + 10}", Count = Group.Count()
This
For Each x In tens
Console.WriteLine($"{x.Text} --> {x.Count}")
Next
Prints
1 - 10 --> 3
11 - 20 --> 1
21 - 30 --> 1
31 - 40 --> 2
41 - 50 --> 1
51 - 60 --> 3
61 - 70 --> 2
71 - 80 --> 3
Related
In here get value into the arraylist, I'm using MySql database and Tables.
my 1st array list number of items can be 1 or 2 or 3 or 4 or 5,
my 2nd array contain number of 5 items.
for example;
arraylist1 = (10,50,50)
arraylist2 = (2.6,2.4,1.7,1.4,1.1)
If when multiplying arraylist together,
total = (10 * 2.6) + (50 * 2.4) + (50 * 1.7) places can't be changed.
How to do that?
firstly I try with define the arralist2 and it worked.
arraylist2 = (2.6,2.4,1.7,1.4,1.1)
Code;
Dim a As Integer = 0
For Each i In arraylist1
Ptotal += arraylist2(a) * i
a += 1
Next
Dim a As Integer = 0
For Each i In distance_range
Ptotal += price_rage(a) * i
a += 1
Next
I have this crazy array.
ReDim arrayDeCeldas(filas - 1, columnas - 1)
For i = 0 To filas - 1
For j = 0 To columnas - 1
arrayDeCeldas(i, j) = i & j
Debug.Write(arrayDeCeldas(i, j) & " ")
Next j
Debug.WriteLine("")
Next i
And I'm trying to link it to a number of conditions and is not well to do
I'm trying to try to get 6 index from array
The number can't repeat and it are pick horizontally or vertically randomly
0 1 2 3 4 5 6 7
10 11 12 13 14 15 16 17
20 21 22 23 24 25 26 27
30 31 32 33 34 35 36 37
40 41 42 43 44 45 46 47
This example about i'm try.
It can be seen, there are a total of 6 items with different sizes
Actually my code, it's getting huge
Private Sub setBarco()
Dim numeroRandom As New System.Random()
Dim indiceA, indiceB As Integer
For b = 1 To barcos
Randomize()
Dim value As Integer = CInt(Int((2 * Rnd() + 1)))
indiceA = numeroRandom.Next(0, filas)
indiceB = numeroRandom.Next(0, columnas)
Select Case b
Case 1
If arrayDeCeldas(indiceA, indiceB) = 0 Then
arrayDeCeldas(indiceA, indiceB) = b & 1
End If
Case 2
Select Case value
Case 1
For c = 0 To 1
If indiceB + c < columnas Then
If arrayDeCeldas(indiceA, indiceB + c) = 0 Then
arrayDeCeldas(indiceA, indiceB + c) = b & c
End If
Else
If arrayDeCeldas(indiceA, indiceB - c) = 0 Then
arrayDeCeldas(indiceA, indiceB - c) = b & c
End If
End If
Next
Case 2
For c = 0 To 1
If indiceA + c < filas Then
If arrayDeCeldas(indiceA + c, indiceB) = 0 Then
arrayDeCeldas(indiceA + c, indiceB) = b & c
Else
End If
Else
End If
Next
End Select
Case 3
Case 4
Case 5
Case 6
End Select
Next b
End Sub
Based on your comment-reply, I understand the program must:
Pick at random any six elements from a 2D-array, and each element or value must be selected only once.
That's fairly straightforward: here's a pseudo-code implementation:
Perform some initial computation:
Get the bounds of the 2D array
Seed a random number generator:
Create a HashSet instance
Get six pairs of coordinates:
Loop continuously until HashSet has 6 elements in it:
Generate a coordinate pair within the bounds of the array found in Step 1.
Get the value from the coordinate pair
Check to see if we've seen the value before (by looking at the HashSet). If we've not seen it before, then add it to the HashSet and continue, otherwise ignore it and try again.
Return the contents of the HashSet
In C# this would be:
Int32[,] numbers = new Int32[] { ... };
Int32 maxY = numbers.GetUpperBound(0); // y-axis in dimension 0
Int32 maxX = numbers.GetUpperBound(1); // x-axis in dimension 1
Random rng = new Random();
HashSet<Int32> values = new HashSet<Int32>();
while( values.Count < 6 ) {
Int32 x = rng.Next( maxX + 1 ); // Random.Next is upperbound exclusive, hence +1
Int32 y = rng.Next( maxY + 1 );
Int32 value = numbers[ y, x ];
if( !values.Contains( value ) ) values.Add( value );
}
return values.ToArray();
Note that by keeping track of observed values, instead of coordinates, we can simultaneously avoid duplicate values and duplicate coordinates: as duplicate coordinates will give you duplicate values anyway.
I'm working on a scorecard at work which has columns representing 4 possible outcomes, for example:
Successful,
unsuccessful,
Exceptional,
Other
Each staff member is assessed 5 times in the month against those ratings. So 1 person might have 3 successful, 2 exceptional, 0 unsuccessful, 0 other.
So the max instances of each outcome is 5 but the total sum of instances can't be more than 5.
I could try to type out all the combinations (and get them wrong) - is there any function/formula/VBA that anyone knows of that will list out all of the possible combinations of outcomes for me?
e.g. 5000,4100,4010,4001,3200,3020,3002,3110,3011, etc...
Since your numbers can range from 0005 to 5000, you could just write a simple loop that tests each number to see if the digits total 5:
Sub GetPermutations()
Dim i As Long
For i = 5 To 5000
If SumDigits(i) = 5 Then Debug.Print Format$(i, "0000")
Next
End Sub
Function SumDigits(s As Variant) As Long
Dim i As Long
For i = 1 To Len(s)
SumDigits = SumDigits + CLng(Mid$(s, i, 1))
Next
End Function
Alternatively:
Dim w As Long, x As Long, y As Long, z As Long
For w = 0 To 5
For x = 0 To 5
For y = 0 To 5
For z = 0 To 5
If w + x + y + z = 5 Then Debug.Print w & x & y & z
Next
Next
Next
Next
c#:
// numbering 1 to 4 - i.e.
// 1 sucessful, 2 unsucessful, 3 exception, 4 other
for(int i=1;i<4;i++)
{
for(int n=1;n<4;n++)
{
for(int d=1;d<4;d++)
{
for(int q=1;q<4;q++)
{
if(i+d+n+q<=5)
Console.WriteLine(i+n+d+q+", ");
}
}
}
}
EDIT: just saw you asked for vba:
For number1 As Integer = 1 To 4 Step 1
For number2 As Integer = 1 To 4 Step 1
For number3 As Integer = 1 To 4 Step 1
For number4 As Integer = 1 To 4 Step 1
if(number1+number2+number3+number4<=5) Then
Debug.WriteLine(number1.ToString & number2.toString &number3.toString &number4.ToString & ",");
End If
Next number4
Next number3
Next number2
Next number1
The most simple solution I could think of, there's probably better though.
Thanks for your help everyone - i managed to use a combination of suggestions by starting with the number 1, auto-filling down to 5000, then "text to columns", fixed width to separate the digits, sum to 5, filter and hey presto! Seems to have produced the right number of possible combinations adding up to 5.
Thanks again for your help!
I want to count the number of spaces and characters inputted in a textbox field.
Example
textbox1.text - " 1 2 3 4 5 6 7 a b c"
I want to count the number of spaces and the characters
Then the result must be..
Dim spaces as integer, char as integer
spaces = 10 , char = 10
Dim spaceCount, lettercount As Integer
spaceCount= 0
lettercount = 0
Dim s As String = " 1 2 3 4 5 6 7 a b c"
For Each c As Char In s
If c = " " Then
spaceCount+= 1
Else
lettercount += 1
End If
Next
MsgBox(charcount)
MsgBox(lettercount)
For Each will iterate each character within the string then it will check whether c is a space or not. if it is a space then increment the spaceCount else increment the lettrtCount
You can do as you want in a sort way.
Try it
Dim count As Integer = 0
textbox1.text = " 1 2 3 4 5 6 7 a b c"
count = textbox1.text.Split(" ").Length -1
Dim longstring As String = "aab c d e"
Dim TotalLength As Integer = longstring.Length
Dim TotalSpaces() As String = Split(longstring, " "
Dim TotalChars As Integer = TotalLength - (TotalSpaces.Length - 1)
We have that old software (made by one of the first employees many years ago) in company that uses Microsoft Access to run. Boss asked me to add a random string generation in the specific text box on click but i have no idea how to do that. I dont have any Microsoft Access programming experience, thats why i am askin you to help.
I managed to create button and text field so far. Thats where it stops. I also managed to access the code for the button action:
Private Sub command133_Click()
End Sub
This is one way, will work in Access VBA (which is an older basic than vb.net). It will generate a string with letters and numbers.
Sub test()
Dim s As String * 8 'fixed length string with 8 characters
Dim n As Integer
Dim ch As Integer 'the character
For n = 1 To Len(s) 'don't hardcode the length twice
Do
ch = Rnd() * 127 'This could be more efficient.
'48 is '0', 57 is '9', 65 is 'A', 90 is 'Z', 97 is 'a', 122 is 'z'.
Loop While ch < 48 Or ch > 57 And ch < 65 Or ch > 90 And ch < 97 Or ch > 122
Mid(s, n, 1) = Chr(ch) 'bit more efficient than concatenation
Next
Debug.Print s
End Sub
Try this function:
Public Function GetRandomString(ByVal iLength As Integer) As String
Dim sResult As String = ""
Dim rdm As New Random()
For i As Integer = 1 To iLength
sResult &= ChrW(rdm.Next(32, 126))
Next
Return sResult
End Function
Workin on #Bathsheba code, I did this. It will generate a random string with the number of characters you'd like.
Code :
Public Function GenerateUniqueSequence(numberOfCharacters As Integer) As String
Dim random As String ' * 8 'fixed length string with 8 characters
Dim j As Integer
Dim ch As Integer ' each character
random = ""
For j = 1 To numberOfCharacters
random = random & GenerateRandomAlphaNumericCharacter
Next
GenerateUniqueSequence = random
End Function
Public Function GenerateRandomAlphaNumericCharacter() As String
'Numbers : 48 is '0', 57 is '9'
'LETTERS : 65 is 'A', 90 is 'Z'
'letters : 97 is 'a', 122 is 'z'
GenerateRandomAlphaNumericCharacter = ""
Dim i As Integer
Randomize
i = (Rnd() * 2) + 1 'One chance out of 3 to choose one of 3 catégories
Randomize
Select Case i
Case 1 'Numbers
GenerateRandomAlphaNumericCharacter = Chr(Rnd() * 9 + 48)
Case 2 'LETTERS
GenerateRandomAlphaNumericCharacter = Chr(Rnd() * 25 + 65)
Case 3 'letters
GenerateRandomAlphaNumericCharacter = Chr(Rnd() * 25 + 97)
End Select
End Function
I use it with random number of characters, like this :
'Generates random Session ID between 15 and 30 alphanumeric characters
SessionID = GenerateUniqueSequence(Rnd * 15 + 15)
Result :
s8a8qWOmoDvC4jKRjPr5hOY12u 26
TB24qZ4cNfr6EdyY0J 18
6LZRQ9P5WHLNd71LIdqJ 20
KPN0RmlhhJKnVzPTkW 18
R2pNOKWJMKl9KpSoIV2egUNTEb1QC2 30
X8jHuupP6SvEI8Dt2wJi 20
NOTE: This is still not completely random. It will give a higher count of numbers than normal as approx 1/3 of all chars generated will be numbers.
Normally distribution will look like:
10 numbers plus 26 lowercase plus 26 uppercase = 62 possible chars.
Numbers will normally be 10/62 parts of the string or 1/6.2
With the code
i = (Rnd() * 2) + 1 'One chance out of 3 to choose one of 3 catégories
the count of numbers is pushed up to 1/3 (on average)
Probably not too much of a worry - unless you are trying to beat the NSA and then you have decreased your range significantly.