Selecting an item from a listbox to show more items in another listbox? - listbox

Alright, so i have a problem with my programming, i have three checkedlistboxes, one called "lstShows" 2nd is called "lstSeasons" and third is called "lstEpisodes" and i have two comboboxes that have seasons and episodes in them, one combobox is called "cbSeasons" and 2nd is called "cbEpisodes. so what i'm trying to do is, when i press on an item in lstshows, i want to be able to assign to it items from lstSeasons, and when i want to click on an item in seasons i want to be able to assign items to it in lstepisodes
So for example lets say, a tv shows contains 10 seasons, so i add that tv shows and assign 10 seasons for it, then season 1, has 20 episodes, and seasons 2 has 15 episodes, i want to be able to add items to each different show, and season. i have been looking every where but i could not find anything.
Here is the layout https://www.dropbox.com/s/u6xc3sb43ksq8qr/Capture.PNG?dl=0
and i tried to do the code, i done this but it does not work.
Dim item As String = lstSeasons.SelectedItem
lstEpisodes.Items.Add(item)
I really need help with this.
Thank you.

First of all, your data must be stored somewhere.
For example, you can have one worksheet per show, have the seasons in column A and for each season have the episodes in the same row (starting at column B). If you don't want this data to be seen, just hide the sheets.
So if I understood correctly, first you want to select a show, then the seasons of this show appear in the 2nd listbox, and then when you select a season the episodes of this season appear in the 3rd listbox.
First, you need to add all you shows to the 1st listbox. Here, I'll assume that you have 10 shows, that their corresponding worksheets are Worksheets(1) to Worksheets(10), and that the name of the worksheet is the name of the show (but you can do as you wish, for example storing the show's name in a particular cell).
Dim showName as String
For i = 1 To 10
showName = Worksheets(i).Name
lstShows.AddItem showName
Next i
Then, to get the seasons listbox to change when you select a particular show, you can do this :
Private Sub lstShows_Change()
Call Me.lstSeasons.Clear
showName = Me.lstShows.Value
Dim sh as Worksheet
Set sh = ThisWorkbook.Sheets(showName)
rowCount = sh.Cells(sh.Rows.Count,"A").End(xlUp).Row
Dim i As Integer
For i = 1 To rowCount
Me.lstSeasons.AddItem sh.Cells(i,1).Value
Next i
End Sub
And to get the episodes listbox to change when you select a particular season, you can do this :
Private Sub lstSeasons_Change()
Call Me.lstEpisodes.Clear
showName = Me.lstShows.Value
Dim sh as Worksheet
Set sh = ThisWorkbook.Sheets(showName)
rowCount = sh.Cells(sh.Rows.Count,"A").End(xlUp).Row
Dim colCount As Integer
Dim i As Integer
Dim j As Integer
For i = 1 To rowCount
If Me.lstSeasons.Value = sh.Cells(i,1).Value Then
colCount = sh.Cells(i, sh.Columns.Count).End(xlToLeft).Column
For j = 2 To colCount
Me.lstEpisodes.AddItem sh.Cells(i,j).Value
Next j
Next i
End Sub
Hope this will help !

First thing I believe you need to do is to put a multi dimensional array to store your information.
More details about jagged arrays: https://msdn.microsoft.com/en-us/library/hkhhsz9t(v=vs.90).aspx
so you can use it this way for example:
Dim shows(50)(50) As string
This will give you 50 "shows" with 50 episodes each. now you can change those using the program as needed.
Now into the next part which is inserting those in. You can modify them by assigning a value by
shows(1)(12) = "ep12nameofshow1"
Where you can assign the string as a variable if you want to be able to manually change the name during runtime
Now you want to add the items to your listbox! So lets go over that with a great little for-loop
For Each episode As String In shows(1) 'show number here
lstEpisodes.Items.Add(episode)
Next
please note that I didn't test this since I don't have access to most of your code so please inform me if there are any problems you are facing.
Update This code should be working:
1- Add this at top of your page (below class declaration)
Dim showEpisodes(99)(99)() As String
think of it as showEpisodes(Show#,Season#,Ep#)
2- Add values to your array. How you do that is up to you (use a file, database or just predetermined values. You can even put them in run time. But that's another thing for another question!)
3- Add this part to your season's code
For Each element As String In showEpisodes(lstShows.SelectedIndex)(lstSeasons.SelectedIndex)
lstEpisodes.Items.Add(element)
Next

Related

how to get index of word from column containing array in all its cells

This is fake data for question purpose
sorry for using images rather than markdown table but it would have been very hard to explain if i used markdown table
This is my DATA named Work Sheet where Y and Z is always going to be array and both have corresponding value. EX: for row2:
WBC = 33
this is my stats Sheet where i need to get value of Parameter[x] based on Day & Disease
This is where i took B$151 to B166 and now need value of it from DATA!Z2:Z (Lab values) based on name of parameter,Disease , Day .
where selected cell (B169) has formula:
B166 = RBC
A169 = DAY 1
B1 = Gastroenteritis
=MODE(INDEX(SPLIT(query(DATA!$E$2:$Z, "Select Z where Y contains '"&B$166&"' and E contains '"&$A169&"' and X contains '"&B$1&"' limit 10"),",",1),0,B$167))
Now problem is in DATA sheet user can input item at any index. right now i am using fixed index B$167, but it will break entirely and give wrong results if user changes index of that term. is there any way where i can replace B$167 with dynamic like searching that parameter name in that E,X (Day, Disease). and get index of it and change B$167 with it
i have tried using match search index and pretty everything in my knowledge but problem was Match function only takes single cell and column, so i can't give entire DATA!Y2:Y and split them and then get index. but did not work.
this was the limit of solution i was able to reach.
where A5 = WBC or any in regards
given Y2 becuase was getting error on Y2:Y
=MODE(INDEX(SPLIT(FLATTEN(FILTER(DATA!Z2:Z,DATA!X2:X=B4, DATA!E2:E=C4)),","),0,MATCH(A5,SPLIT(FLATTEN(DATA!Y2),","),)))
but using this will only give index if it is in y2 and also there is no garranty
that parameter is going to be in same index in all cells all the time
i have tried to add 4th condition to Filter on above formula such as search, regexmatch and all but i was not able to solve.
CURRENT SOLUTION: EDIT2
[NEW SHEET][1]
[1]: https://i.stack.imgur.com/rNDh8.png
right now i have did kind of like this and it will be ok if i can combine those 2 (Disease, value) into single cell like this.
RBC: 11
WBC: 33
so i don't need to manually add disease name and Value column each time. i googled but found that i can't skip columns in formula output
so i moved to this.
EDIT2:
i solved this way, now everything is automatic.
where 1 = n
=INDEX(UNIQUE({A2:A}),1)

Google Sheets random name generator

I was wondering if anyone had a script or way to randomly select a name based on a list. So heres the problem, i have one person who entered the raffle one time. The next person entered lets say 5 times and so on. The fist person should only get one chance to win while the next person should get 5 chances to win
Assuming the top row for labels and names in ColumnA, 'times' in ColumnB then:
In C2 and copied down to suit:
=sum(B$2:B2)
In D1:
=RANDBETWEEN(1,Sum(B:B))
Then:
=iferror(index(A:A,match(D1,C:C,0)),index(A:A,1+match(D1,C:C,1)))
Alternatively, list the "next person" five times as often as the other two, select the complete list, apply Data > Randomise range and pick the one at the top of the list, say.

Automatically fill out textbox doble clicking on an item in a listbox

Another beginners question, but this time about MS Access Controls.
I have a form where I have a list box full with many items.
What I need to do is when I double click on one of this item stored in the list box should all the textbox filled with an item stored on a different field from the same row where from my table.
I checked many tutorials already but didn't found a good solution.
Thanks for any help!
EDIT:
I have a table with several fields. The list box have the items from the first field(ID). The rest of the fields(Tile, Delivery Date, etc...) represented by text boxes in my form.
When I double Click on one item from the list box will populate the text boxes.
If I select the 5th item from my list box which represents the 5th row from my table every text box should have their item from the same row from the fields represented in the table.
I only want to edit the data stored in the table with this form.
Ok I finally get a solution for this one.
I using the DLOOKUP to get the data pulled to my form after the double-click event.
Dim ctrloop
For Each ctrloop In Me.lbReportID.ItemsSelected
strReportID = Me.lbReportID.ItemData(ctrloop)
Next ctrloop
Me.txtTitle = DLookup("[Title]", "tblreports", "[ID No] = '" & strReportID & "'")
The loop stores all report ID I have in my table. The DLOOKUP using this as a filter. It's like the WHERE in SQL.
When the program run will fill out my Title textbox looking through the title field in my tblreports table filtered with the strReportID.
It's working similarly like this SQL query which I using in a different textbox:
SELECT tblReports.[ID No]
FROM tblReports
WHERE (((tblReports.WorkPacage) = 'CDS'))
AND (((tblReports.State) <> 'Complete'));

How do I update the content selected from a dropdown cell if the source subsequently changes? (Insert cell reference instead of literal value.)

I have two sheets used to track a construction project.
On the first sheet, a list of tasks is incorporated into a timeline with cost projections, etc. The tasks are something like the following:
Cut Concrete
Pour new pad
Frame
Roof
The second sheet is for tracking individual purchases, each of which is associated with a task from the first sheet (e.g., Cut Concrete). It looks something like the following:
DATE PAYEE ITEM CATEGORY COST
----- ---------- ---------- -------- ------
10/25 Home Depot (10) 2x4's Frame ▽ $54.00
Using Data Validation, the Category dropdown in the second sheet references the list of items from the first sheet. This is working perfectly. Here's the problem...
If I change the item on the first sheet (for instance, "Frame" to "Framing"), although the dropdown is updated, any previously entered rows (such as the one shown above), just show a validation error (i.e., a red indicator in the right corner of the cell).
Since a construction project can easily have hundreds of items purchased, rather manually looking for data validation errors, is there a way to have the second sheet's values updated? For instance...
Add a script that watches for content changes in the first sheet. When the user starts editing a "task" cell, its original value is noted; and upon exiting the cell, if the value has changed, the script looks through the second spreadsheet for the original value and replaces it with the new one. (Seems like a lot of hassle.)
Find some way for the dropdown to insert a cell reference to the first sheet instead of the actual value. That way, the dropdown cell is always referencing the source item (i.e., the "task" cell's current content).
A more obvious feature I don't know about.
As per my favored possibility noted in my question, I figured out how to write a script that would run after a value was selected from the dropdown, thus overwriting the literal value with a cell reference.
The following script runs after the user makes a selection in the dropdown menu:
function onEdit(event){
var activeSheet = event.source.getActiveSheet();
var activeSheetName = activeSheet.getName();
var activeCell = activeSheet.getActiveCell();
var activeColumn = activeCell.getColumn();
if (activeSheetName == "Envelope (Spent)" && activeColumn === 4) {
var destinationCell = activeCell;
var destinationContent = destinationCell.getValue();
var sourceSheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
var sourceSheetName = sourceSheet.getName();
var sourceRange = sourceSheet.getRange("D:D");
var sourceValues = sourceRange.getValues();
for (var i = 0; i < sourceValues.length; i++) {
if (sourceValues[i] == destinationContent) {
var sourceRow = sourceSheet.getRange("D" + i + ":D" + i).getRow() + 1;
destinationCell
.setValue("='" + sourceSheetName + "'!D" + sourceRow)
.setNote(destinationContent)
;
}
}
}
}
To allow for an easy recovery in case the two sheets somehow get out of sync, the originally selected value, derived from the dropdown's data validation, is inserted as a note. I figured it was easier to clear all the notes in the future than to find myself with a bunch of entries that don't correspond with the source list of tasks.
The data validation in Google sheets is always inserting the Literals you type or pick so you can't link it to the original cell. You have to solve the whole thing through scripts
Get a list of all data validation items through onOpen()
Create an onEdit() function that runs if the data validation range is edited, checks which field is changed and then goes through the purchases, checks which purchases have the old value and replaces them with the new one.

Apply 3-color scale to an entire row in Excel 2010.

I have an table in an MS Excel 2010. The table has two columns. The first column is a name of a person (Col A), the second column is the marks that the person secured in an exam (Col B).
I am applying conditional formatting. If I choose the following wizard
Home > Conditional Formatting > Format all cells based on their values
I can color the Col B on a 3-color scale. This is exactly what I want. However, I want it for the entire row and not only the cell in Col B. I want the name also to be formatted in the same color as the marks.
Anyone knows how to do this?
I have already looked around a bit. The following came close to did not solve the particular problem that I am trying to.
http://www.howtogeek.com/howto/45670/how-to-highlight-a-row-in-excel-using-conditional-formatting/
Conditional Formatting Rows Based on Date
You're probably going to have to use VBA code for this.
Right click the worksheet label and select 'View Code'
Inside the code window, paste in the following code:
Sub RunMe()
Dim xRng As Range, xCell As Range
With Me
Set xRng = .Range(.Cells(2, 2), .Cells(.Rows.Count, 2).End(xlUp))
' Change the first '2' above to reflect the starting row of your data
For Each xCell In xRng
xCell.Offset(0, -1).Interior.Color = xCell.DisplayFormat.Interior.Color
Next xCell
End With
End Sub
Now every time you run the macro (Alt-F8, select macro), column A will be formatted with the conditional formatting assigned to column B.
If you want this process to be automatic, change:
Sub RunMe()
to something like:
Private Sub Worksheet_Activate()
' This will run the macro whenever the worksheet is selected
or you could assign the code to a keyboard shortcut or a command button etc.
If you would like the code to run every time the file is opened, saved closed etc, add the code instead to the ThisWorkbook code window (although you'd have to alter the code slightly as 'Me' is referencing the particular worksheet in which the code is placed).

Resources