excel checkboxes to hide and unhide rows disappear when i save and reopen file - vba

I have 2 sheets in excel where if you tick the checkboxes on the first sheet it hides or unhides the rows on sheet2. I used active x controls and set checkbox properties to 'move and size with cells' but when i unhide the rows, the checkboxes disappear but are still in the document as their height changes and remains at 0.(the rows appear just fine). Please help!!

ActiveX checkboxes are reknown for problems like these. FOrms checkboxes are a bit better, but I would advise to just use cells with data validation set to list and have it accept 0/1 or True/False. It is relatively easy to set up an event macro that toggles the value of those cells after a double-click for instance.

Related

Excel VBA: Deselect a cell

Is there a way to deselect a cell with Excel VBA? My situation is I have a worksheet double-click event, so the cell must be selected for the macro to run. But that leaves a flashing cursor in the cell that's selected.
How do I deselect the cell once the macro is done? I've searched Google for a few methods and tried some, but none quite work properly. I've tried moving the selection left one cell and then right, back to the original cell. But for some reason, the cursor is still flashing in the selected cell. Any help?
The Worksheet_BeforeDoubleClick event has an argument called Cancel. Have a line in your double click event code that sets Cancel = True and that will cancel the cell edit mode and just have the cell selected instead of the blinking cursor within the cell

Programmatically add a button with a macro to an Excel file

I'm creating formated worksheets with an Excel VBA application and I would like to add programmatically add a button to each worksheet when created. This button must have a macro attached to it, which display the average of the values in a certain range in a special cell, and change the background color of this cell depending on the value of the average. Is it possible to this, and if yes, how can I do it ?
You can add the Button like this or example:
ActiveSheet.Buttons.Add(52.5, 7.5, 173.25, 41.25).Select 'Arguments are coordinates
Selection.OnAction = "Button_Click"
Now make sure the Button_Click sub has the logic you want.

Excel ActiveX ListBox Shrinks with each update

I have a set of linked subs which work like this:
A user types into an ActiveX TextBox
A Change Event in that TextBox calls a sub in a Module
That Module sub drives updating a named range in a sheet
The range value drives updating a table of Excel cells that uses lookup functions based on the range value
The table values are copied and pasted to a another range (to eliminate links to formulas)
That pasted range is put into a ListBox using this (props to Rory for his patience):
ActiveSheet.ListBox1.List = Sheets("Search Criteria Control").Range("G1:G21").Value
The result is that for every character the user types in the TextBox the ListBox is updated.
The problem I have is that the ListBox shrinks a bit with every keystroke in the TextBox referred to in #1 above. Is this normal behavior and I'm misusing ListBoxes, am I doing something wrong or do I need to respecify the dimensions of the ListBox every time it is updated with something like this?
ActiveSheet.OLEObjects("ListBox1").Top = 35
ActiveSheet.OLEObjects("ListBox1").Left = 650
ActiveSheet.OLEObjects("ListBox1").Width = 550
ActiveSheet.OLEObjects("ListBox1").Height = 610
Thanks in advance for any thoughts on this.
I was having trouble with the same thing. My ActiveX listbox would move around on the sheet and change size for no reason that I could see.
While I did go ahead and develop some code to reset size and coordinates, that wasn't satisfactory since there had to be a mechanism to trigger that code - something I didn't want to burden end-users with.
I found a better answer in another user forum. There's a Listbox Property called IntegralHeight whose default property is True - something to do with screen resolution and optimal display of listbox contents. Simply set that to False. I did that with some ActiveX boxes that were giving me fits, and I was able to disable the "adjustment" code and, so far, so good!

Make Excel select cell when clicked if previously selected cell had validation dropdown list displayed

If the user is on a cell in Excel, and that cell has a data validation dropdown list, and the user has the list displayed:
If the user then clicks on another cell, that other cell is not selected. Instead, all that happens is that the first cell's data validation dropdown list is dismissed. The first cell is still selected, until the user clicks on the second cell again.
Is there any way in VBA to detect when that happens, and select the second cell so the user doesn't have to click a second time?
I'm on Excel 2007.
You could get the behavior you want by using a ComboBox fit into the cell and use that instead of the built-in data validation. You just lock the cells and have a ComboBox appear in the cells in that column that they select. If they choose another cell the combobox simply goes invisible. This also solves the problem of using a custom font (if you have a font with special symbols, for example.)

Unlock/lock form objects with vba

I have an excel worksheet with 2 comboboxes, and 3 scrollbars. I want to protect all the sheet except this form objects (and one cell). When i try to protect the sheet, i can't use the scrollbars and comboboxes. How can i unlock them, keepin the protection for the rest of the sheet with vba ? I tried to unlock the cells linked to the form objects but it still doesn't work.
Thank u
AB
Why not put the cells that need to be modified on a very hidden worksheet.
Create a named range e.g. "InputCell" in a separate worksheet e.g. "SheetWithInputCell". Set the cell link of the combobox to the named range using =InputCell.
Then set the worksheet to VeryHidden. VeryHidden means that users can't right-click on the sheet tabs and unhide it.
To set the sheet to very hidden, go to the VBA IDE (Alt+F11) and look at the Properties window. If you can't see the Properties window, select View > Properties Window.
In the Project Explorer window (View > Project Explorer), select the worksheet to hide and set the Visible property to xlSheetVeryHidden.
Then you can leave the hidden sheet unprotected and lock the sheet with the controls.
What about Menu option Data --> Allow Users to Edit Ranges?