I have a hard-coded 2d array that prints to a text box in visual basic. However I am unable to split the lines between each row. This is an extract from the array:
stpeople(0, 0) = "Bob"
stpeople(1, 0) = "Last"
stpeople(0, 2) = "Jamie"
stpeople(1, 2) = "Smart"
This will show as one line like so: "Bob Last Jamie Smart"
this is the code that I am using to display the array am I using vbNewLine incorrectly? I am using visual basic.net.
Dim stoutput As String
For y As Integer = 0 To 9
For x As Integer = 0 To 4
stoutput = stoutput & stpeople(x, y) & " "
Next
stoutput = stoutput & vbNewLine
Next
ListArray.Items.Add(stoutput)
I don't know what type is ListArray but I'll try to guess. Sometime, these things don't display new lines, you would need to add each line to the Items.
Dim stoutput As String
For y As Integer = 0 To 9
stoutput = ""
For x As Integer = 0 To 4
stoutput = stoutput & stpeople(x, y) & " "
Next
ListArray.Items.Add(stoutput)
Next
Your code should work with textbox, if not, it mean you didn't set them to support multiline.
Related
I have two variables (getYear and getBranch) in my page.
getYear-1,4,11
getBranch-4,5,7
GetYearSingle = Split(getYear, ",")
I get single array value after Split() function like this:
For Each iLoop In GetYearSingle
response.write "<br>Year= " & iLoop
Next
I get result like this
year=1
year=4
year=11
but I need result like this
year=1
Branch=4
year=4
Branch=5
year=11
Branch=7
Going out on a limb I'll assume that
getYear-1,4,11
getBranch-4,5,7
was actually meant to look like this:
getYear = "1,4,11"
getBranch = "4,5,7"
If that's the case you want to split both strings at commas and use a For loop (not a For Each loop) to iterate over the elements of both arrays.
arrYear = Split(getYear, ",")
arrBranch = Split(getBranch, ",")
For i = 0 To UBound(arrYear)
response.write "<br>Year= " & arrYear(i)
response.write "<br>Branch= " & arrBranch(i)
Next
You need to loop over both arrays via the (syncronized) index:
Option Explicit
Dim y : y = Split("1,4,11", ",")
Dim b : b = Split("4,5,7", ",")
If UBound(y) = UBound(b) Then
Dim i
For i = 0 To UBound(y)
WScript.Echo y(i), b(i)
Next
End If
output:
cscript 44118915.vbs
Microsoft (R) Windows Script Host, Version 5.812
Copyright (C) Microsoft Corporation. Alle Rechte vorbehalten.
1 4
4 5
11 7
I am trying to use VBA in Excel to write all of the possible combinations of the contents of three arrays to a column to create a wordlist.
Currently, I know how to loop through the arrays and get some output that I want but I can't figure out how to build the loop to give me all possible combinations of the baseWord(n) & numberCharSet(n) & specialCharSet(n).
How do I properly loop through the baseWord array to get all combinations of each baseWord with the contents of the specialCharSet and numberCharSet arrays?
Example:
Cloud1!
Cloud1#
Cloud1#
Cloud1$
Cloud2!
etc...
Private Sub createWordlist()
Dim baseWord(1 To 2) As String
baseWord(1) = "Cloud"
baseWord(2) = "cloud"
Dim numberCharSet(1 To 4) As String
numberCharSet(1) = "1"
numberCharSet(2) = "2"
numberCharSet(3) = "3"
numberCharSet(4) = "4"
Dim specialCharSet(1 To 4) As String
specialCharSet(1) = "!"
specialCharSet(2) = "#"
specialCharSet(3) = "#"
specialCharSet(4) = "$"
x = 1
y = 1
z = 4
w = 1
For Each Item In baseWord
Range("A" & x).Value = baseWord(w) & numberCharSet(y) & specialCharSet(z)
x = x + 1
y = y + 1
z = z - 1
Next
End Sub
As #ScottCraner mentioned in the comments, all you need to do is nest 3 loops:
For Each word In baseWord
For Each num In numberCharSet
For Each special In specialCharSet
Debug.Print word & num & special
Next
Next
Next
I have a vb net program to take a binary value in the image. This syntax produces
111110
on textbox3.text. I want no spaces at textbox3.text
1 1 1 1 1 0
Dim x, y As Integer
Dim gambar As New Bitmap(PictureBox7.Image)
Dim gray, vektor, biner As Integer
'biner
'With gambar
For x = 0 To gambar.Width - 1
For y = 0 To gambar.Height - 1
gray = (CInt(gambar.GetPixel(x, y).R) + _
gambar.GetPixel(x, y).G + _
gambar.GetPixel(x, y).B) / 3
gambar.SetPixel(x, y, Color.FromArgb(gray, gray, gray))
If gray > 128 Then
biner = 255
Else
biner = 0
End If
gambar.SetPixel(x, y, Color.FromArgb(biner, biner, biner))
'ttup proses grayscale
If (biner = 0) Then
vektor = 0
End If
If (biner = 255) Then
vektor = 1
End If
'TextBox2.Text = pixel_putihblkg2
TextBox3.SelectedText = vektor.ToString
Next y
PictureBox7.Refresh()
PictureBox7.Image = gambar
Next x
PictureBox7.SizeMode = PictureBoxSizeMode.StretchImage
Catch exc As Exception
End Try
Your request is unclear, but if I interpret your example correctly, you want to insert a space between each digit in your string before assigning it to the TextBox. You can do this with a modified loop and String.Insert.
Dim spacedString As String = vektor.ToString
For i As Integer = 0 To (spacedString.Length * 2) Step 2
spacedString = spacedString.Insert(i + 1, " ")
Next
TextBox3.Text = spacedString
Here I'm copying vektor.ToString into a new variable, which will then be modified. The For loop increments from zero to twice the length of the unmodified string (because the final string will be twice as long), and steps by two (to insert after each character plus space, or two positions). For each iteration, use .Insert to insert a space. Finally, assign the modified string to the TextBox.
This will result in an extra space at the end of the string. If this is a problem, you can use String.TrimEnd to remove it.
TextBox3.Text = spacedString.TrimEnd(" "c)
Update: I failed to notice that you seem to be inserting one digit at a time to the TextBox. In this case you can simply add the spaces directly in code.
TextBox3.Text &= vektor.ToString & " "
I would like to be able to add some range of data in a dynamic multidimensional array without using a double loop that screens each element of the array. But I don't know if it is possible. By double loop, I mean such a code (this is only an example):
Dim Films(1 To 5, 1 To 2) As String
Dim i As Integer, j As Integer
For i = 1 To 5
For j = 1 To 2
Films(i, j) = Cells(i, j).Value
Next j
Next i
I am using VBA 2010. I know how many rows my array has, but the number of columns is variable.
Here is my code :
Sub DRS(Item)
'item is a name to search for in a specific range
Dim SrcRange() As Variant
Dim cell3 As Range
Dim n As Integer, m As Integer
SrcRange() = Array()
ReDim SrcRange(45, 0)
m = -1
n = 0
With Sheets("X")
For Each cell3 In .Range("I13:AG" & .Cells(1, Columns.Count).End(xlToRight).Column)
'the range ("I13:AG...") contains names, and some will match with "item"
m = m + 1
If Len(cell3.Value) > 0 And cell3 = Item Then
SrcRange(0, n) = .Range(m + 8 & "30:" & m + 8 & "75")
'the previous line **should** add a whole range of cells (which contain numbers, one by cell) in a colum of the array, but this is the line that doesn't work.
n = n + 1
ReDim Preserve SrcRange(UBound(SrcRange), n)
End If
Next cell3
End With
End Sub
I already tried those::
SrcRange(:, n) = .Range(m + 8 & "30:" & m + 8 & "75")
SrcRange(0:45, n) = .Range(m + 8 & "30:" & m + 8 & "75")
SrcRange(, n) = .Range(m + 8 & "30:" & m + 8 & "75")
but no one worked.
Is there a way or a formula that would allow me to add a full range of cells to each column of the array, or am I obliged to use a double loop to add the elements one by one?
I'm guessing that this Range...
.Range("I13:AG" & .Cells(1, Columns.Count).End(xlToRight).Column)
...should actually be xlToLeft instead of xlToRight (xlToRight will always return I13:AG16384).
I'm also not entirely sure what the m + 8 & "30:" & m + 8 & "75" is supposed to be evaluating to, because you increment the variable m each time through the loop, and it gives you ranges like 930:975. I'll take a stab in the dark and assume that the m + 8 is supposed to be the column that you found the item in.
That said, the .Value property of a Range object will just give you a 2 dimensional array. There isn't really any reason to build an array - just build a range and then worry about getting the array out of it when you're done. To consolidate the range (you only get the first area if you grab its Value), just copy and paste it to a temporary Worksheet, grab the array, then delete the new sheet.
Sub DRS(Item)
'item is a name to search for in a specific range
Dim SrcRange() As Variant
Dim found As Range
Dim cell3 As Range
With Sheets("X")
For Each cell3 In .Range("I13:AG" & .Cells(1, Columns.Count).End(xlToLeft).Column)
'the range ("I13:AG...") contains names, and some will match with "item"
If Len(cell3.Value) > 0 And cell3.Value = Item Then
If Not found Is Nothing Then
Set found = Union(.Range(.Cells(30, cell3.Column), .Cells(75, cell3.Column)), found)
Else
Set found = .Range(.Cells(30, cell3.Column), .Cells(75, cell3.Column))
End If
End If
Next cell3
End With
If Not found Is Nothing Then
Dim temp_sheet As Worksheet
Set temp_sheet = ActiveWorkbook.Sheets.Add
found.Copy
temp_sheet.Paste
SrcRange = temp_sheet.UsedRange.Value
Application.DisplayAlerts = False
temp_sheet.Delete
Application.DisplayAlerts = True
End If
End Sub
I'm trying to make a simple 12x12 times table for my console application.
However when I input my double for loops, as soon as it reaches a two digit value, it leaves a space behind.
https://lh6.googleusercontent.com/-f9aeH2PaXdc/VNbETAJkKxI/AAAAAAAAAIs/dNm3stirQoM/w675-h341-no/help.png
(Link to what it looks like)
I need to find a way to straighten the table out to this
https://lh3.googleusercontent.com/-2iO0PAwElZA/VNbEPv8gtnI/AAAAAAAAAIk/66ofURW5Yq8/w676-h345-no/Picture1.png
Here is also the following code:
Module Module1
Sub Main()
Dim table(12, 12) As Integer
For i = 1 To 12
Console.Write("" & i & " ")
Next
Console.WriteLine("")
For i = 1 To 12
For j = 1 To 12
table(i, j) = i * j
Next
Next
For i = 1 To 12
Console.Write(i & "")
Console.Write(" ")
For j = 1 To 12
Console.Write(" |" & table(i, j) & "")
Next
Console.WriteLine()
Next
Console.ReadKey()
End Sub
End Module
If anybody could find a solution to this I would be grateful