craeting a dynamic listindex - vba

First timer. Learning VBA on the go and need some help. I get an invalid property value on this line of code Me.Tech2.ListIndex = startnr
The startnr changes according to value of combobox "Tech2".
Dim jobnr As String
Dim startnr As Integer
Dim endnr As Integer
'Tech2 combobox populates according to the name in Tech1 combobox where the name is a named range
jobnr = Tech1.Value
Sheets("Lists").Activate 'named range scope are sheets("Lists")'
Me.Tech2.RowSource = jobnr
endnr = 10000
If Me.Tech2 = "" Then
For startnr = 1 To endnr
Next startnr
End If
Me.Tech2.ListIndex = startnr

startnr is 10001 so Me.Tech2.ListIndex = startnr tries to make the selected item the 10,002nd item in Tech2, if that item does not exist the error is raised.
Valid values for ListIndex are 0 for the first item through Me.Tech2.ListCount - 1 for the last item.

Related

Transferring items from one listBox to another list

i have ListBox1 and ListBox2 and textbox
I want the following when setting a value in a TextBox. If the value in ListBox is1, then items are moved from the current value to the last items in not the first. to listbox2
Dim foo As String
Dim index As Integer = 0
Dim good As Integer = 0
foo = TextBox1.Text
ListBox2.Items.Clear()
For i = 0 To ListBox1.Items.Count - 1
If ListBox1.Items(i).ToString = foo Then
index = i
End If
If i >= index Then
ListBox2.Items.Add(ListBox1.Items(good).ToString)
End If
Next
As Lars has noted in a comment, the index is 0 at the beginning so the condition i >= index wil be always true. So you need to intialize it with Int32.MaxValue.
You add always the first item, because good is always 0. You should use the loop variable i:
ListBox2.Items.Add(ListBox1.Items(i).ToString())
Here is a LINQ version which simplifies the whole code, you don't need more:
ListBox2.Items.Clear()
Dim allAfter = ListBox1.Items.Cast(Of Object).SkipWhile(Function(item) Not TextBox1.Text.Equals(item))
ListBox2.Items.AddRange(allAfter.ToArray())

VBA Powerpoint - Modify category names

I'm trying to modify category names of an existing chart in VBA powerpoint with the below code, but instead the category names are removed from the chart.
Dim mCatArray() As String
lngSeries = 0
Dim testvar As Variant
'Get the existing category names in an Array
'oSh = Shape element
For Each testvar In oSh.Chart.Axes(xlCategory).CategoryNames
strIn = ProperCaps(testvar & "", objRegex)
ReDim Preserve mCatArray(lngSeries)
'Do my changes and insert the category name in the Array
mCatArray(lngSeries) = strIn
lngSeries = lngSeries + 1
Next
'Assign the Changed Category array back - METHOD 1
For lngSeries = 1 To .Chart.SeriesCollection.Count
oSh.Chart.SeriesCollection(lngSeries).XValues = mCatArray
Next
'Assign the Changed Category array back - METHOD 2
oSh.Chart.Axes(xlCategory).CategoryNames = mCatArray
Both the method's don't seem to help. Any help is appreciated.

VB.Net Must be non-negative and less than the size of the collection

Index was out of range. I was trying to put the selected rows in frm_Guest to frm_Main:
Private Sub dgview_GPIGuest_CellContentClick_1(sender As Object, e As
DataGridViewCellEventArgs) Handles dgview_GPIGuest.CellContentClick
Dim ID As String = dgview_GPIGuest.SelectedRows(0).Cells(0).Value.ToString()
Dim FName As String = dgview_GPIGuest.SelectedRows(0).Cells(1).Value.ToString()
Dim LName As String = dgview_GPIGuest.SelectedRows(0).Cells(2).Value.ToString()
Dim Gender As String = dgview_GPIGuest.SelectedRows(0).Cells(3).Value.ToString()
Dim Address As String = dgview_GPIGuest.SelectedRows(0).Cells(4).Value.ToString()
Dim IDType As String = dgview_GPIGuest.SelectedRows(0).Cells(5).Value.ToString()
Dim IDNumber As String = dgview_GPIGuest.SelectedRows(0).Cells(6).Value.ToString()
frm_Main.txt_GPIId.Text = ID
frm_Main.txt_GPIFirstName.Text = FName
frm_Main.txt_GPILastName.Text = LName
frm_Main.txt_GPIAddress.Text = Address
frm_Main.txt_GPIIDNumber.Text = IDNumber
frm_Main.txt_GPIIdType.Text = IDType
If Gender = "Male" Then
frm_Main.rb_GPIMale.Checked = True
Else
frm_Main.rb_GPIFemale.Checked = True
End If
End Sub
I think the confusion is between selected row and cell click. A row is selected by clicking in the far left column of the grid. The entire row will highlight. When a single cell is clicked there are no selected rows; therefore index 0 is out of range. If you want to use this event then use the DataGridViewCellEventArgs as #jmcilhinney suggested.
Debug.Print($"Row {e.RowIndex.ToString}, Column {e.ColumnIndex}")
Firstly, not sure if you already did it but you must set the SelectionMode property to FullRowSelect either in design time or from code behind :
Dgvw1.SelectionMode = DataGridviewSelectionMode.FullRowSelect
The reason for this is to select the entire row when you click on a cell.
Moving on, the usage of SelectedRows is quite unnecessary here(assuming MultiSelect is not set). What i understand so far is that you want to get the column value of the selected row, right ? Here's a quick code :
Dim row = dataGridView1.CurrentCell.RowIndex; ''This gets the row index(or count) of the selected row
Dim col = dataGridView1.CurrentCell.ColumnIndex; ''This gets the column index(or count) of the selected row
Dim ID As String = dataGridView1.Rows(row).Cells(col).Value.ToString()) ''Now note that here i am getting the selected column/cell's value, you can change it with any required index :)
Hope this helps.
The reason you received the error
It's pretty simple to understand. It is somewhat like you are trying to subtract 1 from 0 which will result in -1 but in programming(in cases of collections) , it is an invalid value. I assume when you select a cell, your entire row isn't selected(default behavior)(read the beginning of the answer to fix). That's why SelectedRows(), which is supposed to return all selected rows, returns null. And finally you get the error

Randomly select an item from a list based on a class, repeat number of times based on different numbers

I am not familiar with using macro's, but I think that what I would like excel to perform is best handled with a macro. So I can use all the input you may have!
I have these headers;
ID Tag Pen Sex Weight Class Inside range
With 450 rows of data. Based on the distribution of the weight data, I have in two other columns (class and number) the number of rows I want to select within each class. The selected rows must have the value "Yes" in the column "Inside range".
I want to randomly select the rows, based on the number needed for each class, and copy these rows to a new sheet. It sums up to 30 rows in the new sheet.
I hope you have a suggestion how to complete this action!
can you try the following, you will need to add a reference to Microsoft Scripting Runtime library:
Const rowCount = 450
Public Sub copyRows()
Dim i As Integer
Dim j As Integer
Dim classes As Scripting.Dictionary
Dim source As Worksheet
Dim colNumber As Integer
Dim colClassName as Integer
Dim colInsideRange As Integer
Dim allSelected As Boolean
Dim randomRow as Integer
Dim sumRemaining as Integer
allSelected = False
Set source = Worksheets("YourWorksheetName")
colClassName = 6 'this is the column number where class names are entered. I am assuming 6
colNumber = 7 'this is the column number where number of rows to be selected are entered. I am assuming 7
colInsideRange = 8 'this is the column number where "Inside Range" values are entered. I am assuming 9
For i = 2 to rowCount + 1 'assuming you have a header row
classes(CStr(source.Cells(i, colClassName))) = CInt(source.cells(i, colNumber)
Next i
Do until allSelected
Randomize
randomRow = Int ((Rnd * 450) + 2) 'assuming you have a header row, + 1 if you don't
If classes(CStr(source.Cells(randomRow, colClassName))) = 0 Then
With classes
sumRemaining = 0
For j = 1 to .Count - 1
sumRemaining = sumRemaining + .Items(j)
If sumRemaining > 0 Then Exit For
Next j
allSelected = (sumRemaining = 0)
End With
Else
source.Cells(randomRow, colInsideRange) = "Yes"
classes(CStr(source.Cells(randomRow, colClassName))) = classes(CStr(source.Cells(randomRow, colClassName))) - 1
End If
Loop
'Enter your code to copy rows with "Inside Range" = "Yes"
End Sub
Sorry if there are some errors or typos, I wrote from my mobile phone.

How to assign a word(number) from a listbox to a textbox

I have an application in VB.Net that displays the results of a math operation to a listbox. For example I have the 1 + 1 = 2.
What I'm trying to do is to have the first 2 numbers (1 and 1) copied to 2 different textboxes when listbox item is selected.
Any help would be greatly appreciated.
Thank you.
My VB.Net is a bit rusty, but something like this should do it:
In the SelectedIndexChanged event put this:
'Make sure that we have a selected item before continuing
If listBox1.SelectedIndex = -1 Then
Return
End If
'Get the text of the selected item
Dim selectedtext As String = listBox1.Items(listBox1.SelectedIndex).ToString()
'Split the item by the + and the = into an array of strings
Dim parts As String() = selectedtext.Split("+"C, "="C)
'If we have more than 2 parts
If parts.Length > 2 Then
'Define a variable for each part
Dim part1 As String = parts(0).Trim()
Dim part2 As String = parts(1).Trim()
'Make text boxes set to part1 and part2. part1 = 1, part2 = 1
End If