This is my code in vb.net:
Do Until StreamReader.Peek() = -1 _
OrElse intSubscript >= EmployeeData.Length
Line = StreamReader.ReadLine()
EmployeeData = Line.Split(" ")
intSubscript += 1
Loop
I am trying to read a sequential filled such as this one:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
I want to my loop to read one line per iteration and for every line read, I want it to be split by spaces and then stored in an array (EmployeeData(50)).
My issue as stated above is that my array gets reset and replaced by the values of the next iteration instead of being added to it.
I think issue is on line.
EmployeeData = Line.Split(" ")
As of me you need to append next row value to this list. if EmployeeData is list type then you can use AddRange to add current iterate value like below:
EmployeeData.AddRange(Line.Split(" "))
Related
I'm trying to read the number of characters in column "MRP element data" in tcode MD04.
Below code is giving me 1 but I want it to give 16 or it should give 16 provided that my code is right...If there is one more row with one more Purchase Order it should give 32 etc.
j = 0
Do
session.findById("wnd[0]/usr/subINCLUDE1XX:SAPMM61R:0750/tblSAPMM61RTC_EZ/txtMDEZ-EXTRA[5," & CStr(j) & "]").caretPosition = 0
objSheet.Cells(i, 2) = Len(Cstr(j))
Exit Do
j =j + 1
Loop
You currently calculate the length of the integer variable j which contains the value 0, it's why you get 1.
The table element you show is a GuiTableControl object. You will find the method GetCell to read the content of a cell, and after that you can calculate the length of its value.
Set tableControl = session.findById("wnd[0]/usr/subINCLUDE1XX:SAPMM61R:0750/tblSAPMM61RTC_EZ")
MsgBox Len(tableControl.GetCell(1,5).Text)
(row 1 = second row; column 5 = sixth column)
I am using VBA
I have a column of integers in an array
I run the array thru a randomizer module and copy that to a second column
I run the array thru a randomizer module and copy that to a third column
I run the array thru a randomizer module and copy that to a fourth column
The issue I can't seem to over come is duplicates on the same row
IE:
2 13 27 14
27 27 13 5
14 5 2 13
13 2 14 27
5 14 5 2
*columns are much longer
I have searched the web but everyone is focused on creating random numbers without duplicate. I have found shuffling but once again it seems to be one dimensional.
'// get a random number from 1 to the last row of column 25
For N1 = 1 To LastRow
Again:
Randomize Timer
RNum = Int(Rnd * (LastRow - 1 + 1) + 1)
'// this is where the values in "NArray" get shuffled around
Temp = NArray(N1)
NArray(N1) = NArray(RNum)
NArray(RNum) = Temp
ws5.Cells(N1, Lp) = NArray(N1)
'// this looks for duplicate numbers in the row
Select Case Lp
Case 26: a = ws5.Cells(N1, Lp).Value + 0
b = ws5.Cells(N1, Lp - 1).Value + 0
If a = b Then
GoTo Again
End If
Case 27: a = ws5.Cells(N1, Lp).Value + 0
b = ws5.Cells(N1, Lp - 2).Value + 0
c = ws5.Cells(N1, Lp - 1).Value + 0
If a = b Then
GoTo Again
ElseIf a = c Then
GoTo Again
End If
...
'// the shuffled array is inserted into the cells of column 26
For N1 = 1 To LastRow
ws5.Cells(N1, Lp) = NArray(N1)
Next N1
Next Lp
no errors with the exception that when comparing cell values in the row going from column to column it misses duplicate values such as row 2 column 3 may contain "14" and row 2 column 4 contains "14" it doesn't loop back and get another value for column 4
I'm having a certain layout like this:
0
0
0 5
1 6
1
0 7
1 8
1
0
0 9
1 10
1
0 11
1 12
Above is a list with combinations from 000, 001, 010, 011 to 111. The combinations all have a certain value, from 5 to 12.
(The enters are new rows, the spaces are indentlevels of cells, the 5, 6, 7, etc. are in a new column. The 0's and 1's are all in the same column.)
Now I need to have the sum of all the values in which the first 0/1 is 1, the sum of all the values in which the second 0/1 is 1, and the same for the last value. The results in this case must be: 42, 38 and 36
I can't find out how to programm this properly. I was hoping for something like this:
While not Sheets("Sheet 1").Cells(j, 1).indentlevel(2).Value = 0
sum = sum + cells(j,2)
j = j + 1
Wend
But obviously this doesn't work. I can't program this all out without loops, because the codes can be up to 5 didgets (ex. 01101)
Have you thought of using a for loop? Also I think you are using the IndentLevel syntax incorrectly.
With ThisWorkbook.Sheets("Sheet 1")
set rng = Range(.cells(1,1), .cells(3,10)) ' Change this to match your range
End With
For each c in rng
If c.IndentLevel = 2 Then
sum = sum + c
End If
Next c
This doesn't need VBA. The real problem is an inappropriate data format. Update your convention such that each indentation level is moved to a separate column. You'll then be able to proceed with simple Excel formulas.
I have listbox has lines like this
12345678%32=5
4663578877fg
6883346899
,,,etc
How can I get the first two number of every line to be like this
12
46
68
BTW the listbox has only numbers in first of every line
Thanks!
myListBox.Items.Add("1234")
myListBox.Items.Add("567")
myListBox.Items.Add("890")
For position As Integer = 0 To myListBox.Items.Count - 1
myListBox.Items(position) = CStr(myListBox.Items(position)).Substring(0, 2)
Next
EDIT
We can use the RichTextBox.Lines property that returns a array of String. Each String in the array represents a line in the RichTextBox. If you want to store the first two digits in an array, you can try :
Dim intValues(Convert.ToInt32(RichTextBox1.Lines.Length)) As Integer 'Stores the digits
For position As Integer = 0 To Convert.ToInt32(RichTextBox1.Lines.Length) - 1
intValues(position) = Convert.ToInt32(RichTextBox1.Lines(position).Substring(0, 2))
Next
Dim i As Long
Dim rows As Long
Dim rng3 As Range
rows = rng3.rows.Count
For i = rows To 1 Step (-1)
Does anyone know how this loop works? I'm confused on the meaning of rows To 1 Step (-1).
from high number To 1 adding (-1) each iteration
Note: It's adding because + AND - in mathematical logic evaluate to a -
If rows = 10 then
for i = 10 to 1 step -2 would mean loop back from 10 to 1 subtracting 2 from the i in each loop cycle.
adding a Debug.Print i inside the loop may give you a better clue.
Note: turn ON the Immediate Window hitting CTRL+G or View => Immediate Window from the VBE menu bar
An example loop increasing by 3 on each cycle.
for i = 1 to 10 step 3
debug.print i
next i
Usage
The step-back technique is mostly used when deleting rows from a spreadsheet.
To see the logic in practice see the following
How to select and delete every 3rd column
Delete entire excel column if all cells are zeroed
Excel VBA - Scan range and delete empty rows
When deleting rows, it is often common practise to start at the end and step backwards, this is so no rows are skipped.
Dim i As Long
Dim rows As Long
Dim rng3 As Range
rows = rng3.rows.Count
For i = rows To 1 Step (-1)
'delete row if "delete" is in column 1
If rng3.cells(i,1).Value = "delete" Then
rng3.Rows(i).EntireRow.Delete
End If
next i
Dim i as Integer
For i = 1 To 14 Step 3
Debug.Print i
Next i
In above code loop will iterate from 1 to 14 increment with 3 so output will be like
1 4 7 10 13
It means it can not cross 14 that is limit.
So whatever value is provided in step it will add into the variable use for looping purpose. Here
i = i +3
But in For loop in VBA, Step value can not be changed dynamically. For example:
Dim i As Integer
For i = 1 To 10 Step i
Debug.Print i
Next i
Here, before starting iteration Step is equal to the value of i that is the default value i.e. 0. So i will increment like below:
i = i+ i => i = i+0
So i will not increment here and loop will iterate for ever.
Now for below code:
Dim i as Integer
For i = 1 To 14 Step i+1
Debug.Print i
Next i
i will increment like :
i=i+(i+1) => i= i+(0+1) =>i = i+1
so it will increment by 1 and output will be 1 2 3 .... 14
Now for below code :
Dim i As Integer
i = 3
For i = 1 To 10 Step i
Debug.Print i
Next i
here, i is equal to 3 before loop execution, so Step value will be 3, but loop will start with i = 1 and will increment with 3 through out the loop.
here,
i = i+3
so output will be 1 4 7 10.
Now for some other variable:
Dim i As Integer
Dim j As Integer
j = 2
For i = 1 To 10 Step j
Debug.Print i
j = i
Next i
in above code Step value will be 2, so i will increment by 2 for every iteration whether j is modifying inside loop or not, it will not impact Step value, so output will be
1 3 5 7 9
Please correct me if I miss anything or something is wrong in this. Also suggest if there is any way for dynamic looping using For loop in VBA.