VBA - Show entire row on last item in drop down - vba

I am trying to unhide a row in my spreadsheet based on the value picked from a drop down list above.
I am not wanting to show this row based on a certain value, but only when anything other than the last row is selected.
So, for example, in a drop down list I have versions of Windows (with the last item being Windows 10). If the last item is selected, then the row below which would ask 'Is your version of Windows planned for an upgrade soon?' will stay hidden. Any other value will unhide this row.
This will help with future maintaining of the spreadsheet whereby when a new version of Windows is released, I can add this to the bottom of the drop down list, and the code will not need to be changed as it's looking for the last row for it's execution.
Is this possible? Or is there a better way of doing this?

To hide row depending on value in column above, in, lets say, cell A9 use event in Worksheet.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(external:=False) = "$A$9" And Target = "Windows 10" Then
Target.Offset(1).EntireRow.Hidden = True
Else
Target.Offset(1).EntireRow.Hidden = False
End If
End Sub
You probably want to have last version of Wind not hardcoded however. But possible solution for this strongly depends on how you populate dropdown list.

Related

Datagridview add row form behind don't increment CurrentRowIndex

I have found a bug on datagridview (vb.net), if I add a row from code, the value of DataGridView1.CurrentRow.Index is always 0.
If I add row manualy (with mouse) the value of DataGridView1.CurrentRow.Index is right.
I have a function that add some rows from code and I have implement DataGridView1_CellValueChanged and I check if the first cell in selected row is empty or not, but this check always first row.
I've found this bug in this link, https://developercommunity.visualstudio.com/t/datagridview-event-rowsadded-doesnt-show-the-corre/2270 and microsoft don't want to fix it.
I can use a simple variable boolean for fix this bug, but maybe someone can help me for resolve definitly this error?
Thanks
That's not a bug. Microsoft even stated as much in the report you linked to. "By design" means not a bug.
The CurrentRow is the row that contains the CurrentCell and that is the cell that contains the caret. When the user adds a row in the UI, it is assumed that they will want to edit that row, so the grid moves the caret to the first cell in that row. When you add a row in code, there's no specific expectation that the user is going to want to edit that row because it wasn't the user who added it, so the caret stays where it is.
If you want a newly added row to be the CurrentRow then you need to assign a cell in that row to the CurrentCell property of the grid, e.g.
Dim newRowIndex = myDataGridView.Rows.Add()
Dim newRow = myDataGridView.Rows(newRowIndex)
Dim firstNewCell = newRow.Cells(0)
myDataGridView.CurrentCell = firstNewCell
You would probably put code like that into a method and then call that method whenever you wanted to add a row.

Datagridview.Rows(#).Selected not updating CurrentRow.Index

VB.Net 4.6.1
Windows Forms application
I have a bound datagridview that I'm trying to sort and select. The data loads fine and I can click the headers (with some code) and the sorting works well. I'm working on reselecting a record after the sort is complete and I'm running into some trouble.
Assume that I have a datagridview with 20 records in it. On the form I also have a button that runs the following:
MessageBox.Show(MyDataGridView.CurrentRow.Index.ToString)
I also have a Sub that handles MyDataGridView.Sorted and in it I have one test line as follows:
MyDataGridView.Rows(2).Selected = True
After the data loads I click on the 11th record (selectionmode fullrowselect) and then I click the button. Box pops up and says "10" (0 based index). I click one of my column headers, the data sorts and the highlight bar jumps up to the 3rd record (index 2). However when I hit my currentrow.index button it still says 10. Additionally if I hit the down arrow key the selection bar jumps down to the 11th (index 10) record in the list.
It seems that using "Selected" doesn't update the currentrow.index value and since it is read-only I can't force it. Can anyone educate me on what's going on because, as it is, my users would be confused if the arrow up and down keys didn't work properly after a sort.
Thank you!
"Current" and "selected" do not mean the same thing. How could they, when multiple rows can be selected but there can only be one current row? The current row is the row that currently contains the caret and a selected row is one that is highlighted. If you want the row at index 2 to be the current row then you need to make a cell in that row the current cell, i.e. assign a cell in that row to the CurrentCell property.

Display row number without moving column

I have built a 12 month roster in which I have the 365 days across 365 columns and my 120 employees listed down in each row, down to row 120.
I am looking for some VBA code that will allow me to (for example) to display row 120 whilst keeping the active column in display.
I am currently using the below simple code which does highlight the row but resets the column set to show Column A. I am using toggle buttons to initiate the macros.
Sub ToggleButton3_Click()
Cells(ActiveCell.Row, "70").Activate
End Sub
Any tips, solutions or advice would be greatly appreciated!
Thank you!
You should create a procedure like this:
Public Sub MoveToRow()
ActiveSheet.Cells(70, ActiveCell.Column).Activate
End Sub
And associate it to a keyboard shortcut (i.e. CTRL + J). Open the Macro Dialog -> Select ThisWorkBook.MoveToRow() -> Click the "Options" Button -> Assign the shortcut. It works :)

VBA to make it just to the next line and include the if statement above

I have an item number scanned into excel B4 and based on the number, it returns either "Yes" or "No" into C4. If it says "Yes" I would like the item number in B4 to be added to another sheet. If it says "No", nothing needs to happen with this item number. I would like the items numbers to from a list in the separate sheet.
=IF(ISNUMBER(SEARCH("Yes",Sheet7!C4)),Sheet7!B4,)
My if statement is working fine but the issue now lies with a code to make jump to the next row after data has been changed so this can be a continuous process. Right now, after something has been scanned, it provides me with that value i need but as soon as another item is scanned it changes the value to the newest. i want a list of item numbers in the end
I won't write the code for you but look at events.
To get to these open the VBA window [Alt] + [F11] then go onto the sheet you want the event to action on.
Change the top left Drop Down to "Worksheet" and the top right will then let you select different events. for this I would recommend the 'Change' event, this causes the code to trigger whenever a cell is changed.

Is there a way to allow only unique values in a column of an Infragistics UltraWinGrid?

I'm using an Infragistics UltraWinGrid with a column of drop-down boxes. I don't want the user to be able to select the same value for multiple rows in that column. Is there a simple (or heck, I'd settle for advanced) way to do this?
I'm using VB.NET
-EDIT-
I tried setting a filter on the data source for the drop-down box. But when I did that, the values in the other boxes in that column started disappearing (not the values themselves, but the descriptions that they were supposed to represent, so instead of reading "Information", it just said "1"). All of the cells in a column refer to the same combo box, so if you filter out a value from the data source for one of them, you filter it out for all of them.
I'm currently trying to catch a CellChange event and check against all other currently-used values. If it was already used, I would put up a message saying as much and revert back to the old value. However, the value comes back as the previously-saved one, so... not helpful. About to see if I can use the "text" property.
Since you're using Infragistics, you could use an UltraDropDown which is bound to a DataTable (or something similiar) which you can add a "Selected" column in addition to a column holding the values you want to show.
As each value is selected (via AfterCellUpdate or AfterCellListCloseUp for instance), you could update the "Selected" column in that data source and use a column filter to only show items which haven't been marked as selected. That way as items are selected or removed, the contents of the drop-down would be automatically updated.
To clear the selected flag from the old value, you can use the BeforeCellUpdate event to access the cell's current value then perform a lookup on the data source bound to the UltraDropDown using that value to clear the flag.
Solved it:
The trick was to use BeforeCellUpdate whose BeforeCellUpdateEventArgs has a "NewValue" and a "Cancel" member. I just look through all of the items in the column to see if any of them match the new value. If one does, I notify the user and cancel the operation.
And that's it. Here's the code:
Private Sub myUltraWinGrid_BeforeCellUpdate(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.BeforeCellUpdateEventArgs) Handles myUltraWinGrid.BeforeCellUpdate
If e.Cell.Column.Key = "myColumn" Then
Dim newValue As Integer = CInt(e.NewValue)
For Each row As Infragistics.Win.UltraWinGrid.UltraGridRow In myUltraWinGrid.Rows
If row.Cells("myColumn") IsNot e.Cell _ 'So I'm not checking against the current cell
AndAlso CInt(row.Cells("myColumn").Value) = newValue Then
MsgBox("That value has already been used in this column")
e.Cancel = True
End If
Next
End If
End Sub