Excel 2010 command button disappears - excel

I'm developing an Excel 2010 workbook, in a manual formulas calculation mode.
(file -> options -> formulas -> Workbook calculation -> manual)
I have some command buttons in the sheet (ActiveX controls), and I set them to move and size with cells (right click on the button -> format control -> Properties -> move and size with text).
This is since I have some rows filtered out under some conditions, and I want the buttons placed in these rows to appear and disappear as well, according to the display mode of their hosting rows.
It all goes perfectly fine, till I save he worksheet when some of the rows (hence buttons) are filtered out (i.e. not displayed).
When I re-open the file again, and expand the filtered rows, the buttons don't show. When checking their properties I see that their visible property is True, but their height is 0, and this doesn't change when I un-filter their hosting rows.
I want to emphasize again that before saving the file - both filtering and un-filtering the buttons worked well.
Would much appreciate any help here.

OK so I get the same results either with ActiveX or Form Controls. For whatever reason, it seems the control's original height does not persist beyond the save & close.
Another option would be to simply clear the AutoFilter on the Workbook's Close and Save events. However, this probably is not what you want if you like to leave some filter(s) on when you save and re-open the file. It's probably possible to save the filter parameters in a hidden sheet or by direct manipulation of the VBE/VBA, but that seems like a LOT more trouble than it's worth. Then you could re-apply the filter(s) when you re-open the workbook.
Here is what code I suggest
NOTE: I relied on the worksheet's _Calculate event with a hidden CountA formula (setting, changing, or clearing the AutoFilter will trigger this event). I put the formula in E1 just so you can see what it looks like:
Since your application relies on Calculation = xlManual then this approach will not work exactly for you but in any case, the subroutine UpdateButtons could be re-used. You would need to tie it in to another event(s) or functions in your application, as needed.
Here is the code
Option Explicit
Private Sub UpdateButtons()
'## Assumes one button/shape in each row
' buttons are named/indexed correctly and
' the first button appears in A2
Dim rng As Range
Dim shp As Shape
Dim i As Long
Application.EnableEvents = False
'## use this to define the range of your filtered table
Set rng = Range("A1:A6")
'## Iterate the cells, I figure maybe do this backwards but not sure
' if that would really make a difference.
For i = rng.Rows.Count To 2 Step -1
Set shp = Nothing
On Error Resume Next
Set shp = Me.Shapes(i - 1)
On Error GoTo 0
If Not shp Is Nothing Then
DisplayButton Me.Shapes(i - 1), Range("A" & i)
End If
Next
Application.EnableEvents = True
End Sub
Private Sub DisplayButton(shp As Shape, r As Range)
'# This subroutine manipulates the shape's size & location
shp.Top = r.Top
shp.TopLeftCell = r.Address
shp.Height = r.Height
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
MsgBox "_Change"
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
''## Assumes one button/shape in each row
'' buttons are named/indexed correctly and
'' the first button appears in A2
'Dim rng As Range
'Dim shp As Shape
'Dim i As Long
'
''## Uncomment this line if you want an annoying message every time
''MsgBox "Refreshing Command Buttons!"
'
'Application.EnableEvents = False
''## use this to define the range of your filtered table
'Set rng = Range("A1:A6")
'
''## Iterate the cells, I figure maybe do this backwards but not sure
'' if that would really make a difference.
'For i = rng.Rows.Count To 2 Step -1
' Set shp = Nothing
' On Error Resume Next
' Set shp = Me.Shapes(i - 1)
' On Error GoTo 0
'
' If Not shp Is Nothing Then
' DisplayButton Me.Shapes(i - 1), Range("A" & i)
' End If
'Next
'
'Application.EnableEvents = True
End Sub
For Another option See this article. You can re-purpose existing commands with RibbonXML customization. While this article is geared towards C# and Visual Studio it's possible to do it with the CustomUI Editor.

I had a similar problem with buttons disapearing (moving on upper left corner) when removing filters.
A solution I found was to add a row above the columns headers so that buttons were still appearing at the top of the columns but were not touching the row where filters were placed.
Adding / removing filters stop interfering with buttons' positions.

I had a similar problem where form buttons appear to work fine, but then disappear after saving and reopening the workbook. Specifically this happened when the form button where part of hidden rows (done using vba code).
Seems like a real bug, although I don't know where the link is.
By changing the form buttons to ActiveX buttons, the buttons stopped disappearing, but started moving/bunching to the top of the screen when the rows were hidden. I just added some vba to re-position the buttons (e.g. CommandButton1.Top = Range(A12:A12).Top --> moves the ActiveX command button to the 12th row).

Related

Printing from dynamic Excel list box

I have a workbook that is a 'quick print' sheet for my workplace.
I am not very skilled with VBA, but I have found various bits from web searches that have given me mostly desired results.
This is the desired look of the sheet:
The column C and D is a List Box with the VBA code:
Private Sub Worksheet_Activate()
Dim Sh
Me.ListBoxSh.Clear
For Each Sh In ThisWorkbook.Sheets
Me.ListBoxSh.AddItem Sh.Name
Next Sh
End Sub
The print icon in D2 is linked to macro:
Sub Print_Sheets()
Dim i As Long, c As Long
Dim SheetArray() As String
With ActiveSheet.ListBoxSh
For i = 0 To .ListCount - 1
If .Selected(i) Then
ReDim Preserve SheetArray(c)
SheetArray(c) = .List(i)
c = c + 1
End If
Next i
Application.Dialogs(xlDialogPrinterSetup).Show
Worksheets(SheetArray()).PrintOut Copies:=1
End With
End Sub
and finally the sheet has following in ThisWorkbook in order to refresh the List Box and put the user on the correct page:
Private Sub Workbook_Open()
Sheets(2).Select
Sheets(1).Select
End Sub
The final bit of VBA seems to work as intended, but I included it anyway in case it could interfere with something (I don't think it can, but just to be sure).
The List Box populates as intended, but I can't get it to stay the width of the columns C:D, and the height of the populated cells in B, every time the workbook is opened the box will increase in size both to the right and down.
The print button works mostly as intended; when clicked it will prompt with available printers, and then OK to print, but cancelling still causes the document to print. It will also allow the printing of the "Please Select..." page - the name of the sheet this form is on - which I have tried to stop from happening but can't get it to work.
On the point of having the list box resize correctly, I have followed this answer but I can't figure out how to keep the size as the range, rather than an integer.
Edit: I have now figured out that I can use Me.ListBoxSh.Width = Range("C:D").Width to set the width the same as the desired columns, as well as Me.ListBoxSh.Height = Range("3:7").Height for the height in the image shown. However, I will be adding to this list and am not sure how to have the VBA code work as "Start in row 3 and continue until an unpopulated row".
On not being able to print the "Please Select..." sheet, I followed this guide and it didn't seem to do anything at all. I had changed the WsName = "Sheet1" line to read WsName = "Please Select..." also.
Any help would be appreciated.

Excel | Disable button at start

I am working on an excel file that will work as a calendar with specifications.
I want to have a button at each day. Since I want this to be reusable for other years, I will have buttons on columns with no days (for example, if January starts on a tewsday, Monday will have a button, but nothing on the day, since it is from December).
I know it is possible to set a button enable = False, but I don't know where to put that code. I don't want it to be disabled when another button is clicked but at the opening of the file.
I am new to vba, I'm sorry if this is something really simple.
My approach needs those cells with days from previous month to be empty or "", if theres any value inside it wont work (instead you change the logic to treat cells values like numbers instead of strings).
I noticed that days in your calendar are in string format or so (i.e: "01") that's why I use Len() to evaluate length of string.
This code will set buttons visibility based on TopLeftCell value. Visible = True to days with some value, and Visible = False to empty values.
There is a way to make a button "Enable" but that property is for buttons inside an UserForm.
Tell me if it works for your case, since Sheet.CurrentRegion may cause some issues if your cells are way to much separate from each other, plus it could also hide some other buttons you have. If any of those scenarios do happen let me know, I'll continue helping you anyways!
Sub Set_Buttons_Visibility()
Dim Sheet As Worksheet
Dim Calendar_DataBodyRange As Range
Dim Shape As Shape
'Set Calendar range
Set Sheet = ActiveSheet 'Set Sheet
Set Calendar_DataBodyRange = Sheet.Cells(1, 1).CurrentRegion 'Set current region
Calendar_DataBodyRange.Select '<- comment this after you tested everything[']
'Hide buttons from previous month
For Each Shape In Sheet.Shapes
'If Shape.Visible Then Shape.Select
'Get variables
'Get Button day, as string
strTemp = CStr(Shape.TopLeftCell)
'Get range occupied by button
Set rngTemp = Sheet.Range(Shape.TopLeftCell, Shape.BottomRightCell)
'rngTemp.Select
'Test conditions
'Test rngTemp is part of Calendar_DataBodyRange
bInRange = Not Intersect(Calendar_DataBodyRange, rngTemp) Is Nothing
'Test TopLeftCell has some string
bString = (Len(strTemp) > 0)
'Test bInRange and bShow (True and True)
bCondition = (bString = False) And bInRange
'Perform action
'Set shape visibility
Shape.Visible = Not (bCondition)
'Delete shape (only if you have another procedure to rebuild all buttons)
''''Shape.delete
Next
End Sub
Run code when workbooks opens
To start this function when workbook opens, go to VBA Project Explorer > ThisWorkbook then inside the module you can bind your code to Workbook_Open event. Later on (depending in where you've have stored your code) use the following Run function.
Important:
According to your case you might need to store your code 1) inside the sheet you are working on, in other cases you store your code 2) in a single sheet usually called PERSONAL.XLSB that is always open when Excel itself Opens (Know more about this) so your functions can be accesible for all sheets that you work on.
Pros and Cons:
On the first case is perfect for sharing your work with your boss or colleagues since your code is locally stored in the sheet (but is harder to update, and hard to back up) and the second case is optimal for your own use since all your functions are in the same workbook so you can call it like "[Workbook.Name]![FunctionName],[FunctionParameters]" (allows you to do better updating and an easier backup just by copy-pasting). In any case you can addapt to your necessities.
Private Sub Workbook_Open()
'Run sintax needs Workbook [extension] and string [!]
'Function is stored in current workbook (case 1)
Run ThisWorkbook.Name & "!Set_Buttons_Visibility"
'Function is stored in PERSONAL (case 2)
Run "PERSONAL.XLSB!Set_Buttons_Visibility"
End Sub

OnClick event to select and display a chart by clicking on a cell containing text

I want an Excel worksheet that displays the progress of Performance Indicators in the form of a dynamic report.
The structure of the report will be Grouped Lists of Performance Indicator Descriptions under their respective Headings and for a chart, based on that description to be displayed detailing the relevant month-by-month performance.
The main build has been done, however, it's the dynamic part of calling charts based on clicking on a cell that is stumping me.
I want the end-user to click on one of the listed descriptions and the relevant chart based on that description to be displayed on the right-hand side.
Is there a way to do this via VBA? The charts and tables will be stored on a separate hidden worksheet in the same document.
You can use hyperlinks to trigger some specific action by using the Worksheet_FollowHyperlink event.
In the worksheet code module for the sheet where the user will be clicking cells:
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
Debug.Print Target.TextToDisplay 'Text of the clicked cell
Debug.Print Target.Range.Address 'the actual cell which was clicked
'Process either of the above to figure out what action to take...
End Sub
And in a regular module:
'Sets up your links: select one or more cells and run this to turn the cell
' contents into hyperlinks which will trigger the event handler.
Sub MakeLinks()
Dim c As Range
For Each c In Selection.Cells
c.Hyperlinks.Delete 'remove any existing link
c.Worksheet.Hyperlinks.Add Anchor:=c, Address:="#LinkTarget()", _
TextToDisplay:=c.Text
'you can re-style the cells if you don't want the blue/underlined font
Next c
End Sub
'This serves as a "dummy" destination for the hyperlinks
' Just returns the clicked-on cell, so the selection doesn't jump around
Function LinkTarget() As Range
Set LinkTarget = Selection
End Function
Once this is set up, clicking any of the links will trigger the event handler in the sheet module, and you can decide what action to take based on the text of the clicked link.
This is a useful alternative to having a bunch of buttons in a table-like structure, since you don't have to worry about tying buttons to specific rows (eg. when sorting)
Notes
If you need to respond to clicks on multiple sheets then there's also the Workbook_SheetFollowHyperlink event (in the ThisWorkbook code module)
This doesn't work with links created using the HYPERLINK() worksheet formula, since those type of links don't trigger the event handler.
If your cell text is short then clicking the cell may "miss" the link - in that case you can pad the text with spaces on either side so it fits the cell better
You can run a macro when a certain cell is selected by using the Worksheet.SelectionChange event.
I would start by putting the event handler into the sheet in question and test what the selected range is compared to the range you care about.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.row = row_of_kpi_desc And Target.Column = col_of_kpi_desc Then
Call Other_Macro
End If
End Sub
Other_Macro could copy the chart from this other hidden sheet and paste it to a specific location. There are ample examples of how to do this. Best of luck.
Assuming the charts are on a worksheet names Charts, and each chart object has been named with a useful name (my code uses "KPI 1", "KPI 2", "KPI 3", but feel free to use something descriptive).
This code goes in the code module behind the sheet the user will click on. To access it easily, right-click on the sheet tab and select View Code.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Cells.Count = 1 Then
Select Case True
Case Not Intersect(Target, Me.Range("A2")) Is Nothing
Dim ChartName As String
ChartName = "KPI 1"
Case Not Intersect(Target, Me.Range("A3")) Is Nothing
ChartName = "KPI 2"
Case Not Intersect(Target, Me.Range("A4")) Is Nothing
ChartName = "KPI 3"
End Select
If Len(ChartName) > 0 Then
Application.ScreenUpdating = False
Dim shp As Shape
For Each shp In Me.Shapes
If shp.Name = "KPI Chart" Then
shp.Delete
End If
Next
Worksheets("Charts").ChartObjects(ChartName).Copy
Me.Paste
With Me.Shapes(Me.Shapes.Count)
.Top = Target.Top
.Left = Target.Left + Target.Width
.Name = "KPI Chart"
End With
ActiveCell.Select
Application.ScreenUpdating = True
End If
End If
End Sub

How to start macro with a left click NOT on a cell in excel?

I am trying since 2 days to find how to do the following without finding anything that suits the aim:
Steps by order :
user open excel file
he chose between folowing :
Paste an image directly in the worksheet (may be an limited area)
activate some video in the workbook (may be a webcam for start)
he select with a button to activate his clicks detection
he clicks anywhere on the picture and i get the coordinates of clicked points
So far i've seen ppl using (and tested myself) :
mouse event ==> this does not work as i need to know the name of what he is clicking on and it may be a brand new picture he just pasted
BeforeDoubleClick (same, i'd prefer avoid doubleclick but even then it doesnt work when i click on something else but cells)
Selectionchange ==> doesnt work if im not clicking on cells
Place hidden button over the area i want : i cant click a button if its not visible, and it becomes visible when i click it if i put as transparent
If anyone has ideas about this...
(nb: im not a pro of vba so i may have missed something)
Just forgot : my issue is not getting the coordinates of mouse, its just triggering the macro when i want, for now im jsut trying to get a simple msgbox to see if trigger works.
Thanks if anyone has any ideas
BR
Not sure if this fits your need (for example couldn't test it with a video).
a) Place a "button" of any kind on your sheet. I usually use a square shape for that and format it a little bit (color, shade, text). Assign the subroutine SetEvents to it.
b) Declare a global variable that remembers that click-activation is active
Option Explicit
Global EventCatchingActive As Boolean
c) In the event routine of your button, set the OnAction-method for all shapes of the sheet - see the routine setEvents. This ensures that even newly added images handle the click event.
Sub setEvents()
' This routine is called from the magic button.
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets(1) ' Set this to whatever sheet you need
Dim button As Shape
Set button = ws.Shapes(Application.Caller)
If EventCatchingActive Then
EventCatchingActive = False
button.TextFrame2.TextRange.Characters.Text = "Start Clicking"
Else
Debug.Print "Setting EventHandler"
Dim sh As Shape
For Each sh In ThisWorkbook.Sheets(1).Shapes
' Debug.Print sh.Name, sh.Type
If sh.Name <> button.Name Then sh.OnAction = "ClickedMe"
Next
EventCatchingActive = True
button.TextFrame2.TextRange.Characters.Text = "Stop Clicking"
End If
End Sub
d) Declare a routine that is called if any of the shapes is clicked. With Application.Caller you can check what was clicked.
Sub ClickedMe(Optional target As Range = Nothing)
If Not EventCatchingActive Then Exit Sub
If target Is Nothing Then
Debug.Print "I clicked on " & Application.Caller
Else
Debug.Print "I clicked on cell " & target.Address
End If
End Sub
(note that code of steps b) to d) goes into a regular module)
e) If you also want to handle clicks on a cell, add the following into the sheet-module of the worksheet you are dealing with.
Private Sub Worksheet_SelectionChange(ByVal target As Range)
ClickedMe target
End Sub

Disable a specific form control button from running its assigned macro even if it is clicked

I would like to be able to prevent a specific button ("Button 4925") from running its assigned macro even when it is clicked. Basically, when you click it, it would either do nothing or show a message that says " This is an essential item that cannot be deleted"
In other words, I would like to be able to exit the sub only if the clicked button is in cell A12. Otherwise, run the code as normal. I don't know how to do that considering that I am a very beginner in VBA.
Some information:
The button is a form control button. Not an Active X one. It gets copied and pasted by another macro on the sheet. The assigned macro is written under a Standard Module.
The assigned macro function is to delete a relative range of rows. Here is the code:
Sub Delete_Button()
' Delete_Button Macro
' Step 1: Select the cell under the clicked button
Dim r As Range
Dim s As Object
Set r = ActiveSheet.Buttons(Application.Caller).TopLeftCell
r.Select
' Step 2: delete all buttons relative to the selected cell from step 1
StartCell = ActiveCell.Offset(-5, 0).Address
EndCell = ActiveCell.Offset(0, 0).Address
For Each s In ActiveSheet.DrawingObjects
If Not Intersect(Range(StartCell, EndCell), s.TopLeftCell) Is Nothing Then
s.Delete
End If
Next s
' Step 3: delete the rows relative to the selected cell from step 1
ActiveCell.Offset(-7, 0).Rows("1:9").EntireRow.Select
Selection.Delete Shift:=xlUp
ActiveCell.Offset(-4, 0).Range("A1").Select
End Sub
You must 'tell' to the code, in a way, that it must not delete the range.
So, I would suggest you to create a Private variable on top of the module keeping the button code (in the declarations area):
Private stopButCode As Boolean
Than, you must make this variable True. Use a Check box, or a piece of code in another control to make it True.
The Button code must be adapted in a way like following:
If Not stopButCode Then
'delete whatever is to be deleted
Else
MsgBox "Deletion not allowed..."
Exit Sub
End If
Edited:
If you want the code not working only if the button will be on cell "A12", you can use adapt your code as following:
Dim r As Range
Dim s As Object
Set r = ActiveSheet.Buttons(Application.Caller).TopLeftCell
If r.Address = "$A$12" then Exit Sub
'here follows your existing code...

Resources