Excel hiding columns [duplicate] - excel

Is there a way, to trigger an event (call a sub) in Excel VBA, when i manually hide a row/column?
I want the same row to be hidden in all following sheets, when it is hidden in a particular sheet.
Is that possible?
Thanks in advance

There is no direct event trigger to capture hiding or unhiding columns. There are clumsy workarounds, using formulae in cells but those feel like a kludge when using and not really flexible.
However, there is an indirect way to capture this event if you use Excel 2007 or newer. This is neat and extremely flexible.
Modify the Ribbon XML (if it's present): You need to be able to modify the Ribbon's customUI14.xml (for Excel 2010) or customUI.xml (for Excel 2007).
Create the custom Ribbon UI XML file (if not present): You may use Ron De Bruin's excellent Custom UI Editor from http://www.rondebruin.nl/win/s2/win001.htm (which is also endorsed in quite a few official Microsoft examples).
XML changes for capturing events: Open the Excel file in the Custom UI Editor and add the XML code as shown below. That will enable a column hide or unhide event to trigger a macro in your code.
VBA code to execute your actions: Add the code listed below to take action once the event is captured.
Reference: This excellent solution was provided by Andy Pope here (MSDN link).
Custom XML code:
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" >
<commands >
<command
idMso="ColumnsHide"
onAction="Column_Hide_Macro"/>
<command
idMso="ColumnsUnhide"
onAction="Column_UnHide_Macro"/>
</commands >
</customUI>
Custom UI Editor screenshot:
VBA code:
Sub Column_Hide_Macro(control As IRibbonControl, ByRef CancelDefault)
MsgBox ("You have hidden a column")
' You may put your code here
' to check if your monitored row is hidden
CancelDefault = False ' This enables the default action to continue
End Sub
Sub Column_UnHide_Macro(control As IRibbonControl, ByRef CancelDefault)
MsgBox ("You have unhidden a column")
' You may put your code here
' to check if your monitored row is unhidden
CancelDefault = False ' This enables the default action to continue
End Sub

Related

How to you quick toggle cell Notes in excel?

I work with excel files that use a lot of "Notes".
I know I can right-click on a cell to toggle note visibility, and I also know the shortcut ALT+R+T+O. However these are cumbersome when I look to toggle notes constantly.
I've added a macro, on Ctrl+Q that reads:
Sub ToggleNote()
'
' ToggleNote Macro
'
' Keyboard Shortcut: Ctrl+Q
'
ActiveCell.Comment.Visible = Not ActiveCell.Comment.Visible
End Sub
But there are a few limitations with this:
I receive a new excel every day (by email) and It's cumbersome to convert it to .xlsm & add the macro.
Some of the tool we use in the company blocks .xlsm files making them hard to share.
The macro is a little glitchy if I accidentally use it on a cell with no comment.
Is there a good way to do this that I'm not aware of?
PS. The excel files are written using Python & XlsxWriter - it is feasible to modify the code that creates the excels.
There are two easy commands for this:
' Show all comments/notes
Application.DisplayCommentIndicator = xlCommentAndIndicator
' Hide all comments/notes (but show the indicator)
Application.DisplayCommentIndicator = xlCommentIndicatorOnly
There also is:
' Hide all comments/notes, even the indicator:
Application.DisplayCommentIndicator = xlNoIndicator
But this makes it difficult to see which cells have comments/notes and which not.
You could use an Add In which would have the macro enable for each use of Excel. You can modify this to just happen with certain workbooks etc.
In the workbook_open of the Addin you could have
Private Sub Workbook_Open()
Application.OnKey "^q","mdlFunctions.DisplayNotes"
End Sub
Then in a module called mdlFunctions in the AddIn, have your toggling code, mine's DisplayNotes(). You may want to check for a comment being present within that code too.
You can just have this add in installed, then workboks won't become macro enabled, but you will have the macro function from the addin.
The excel files are written using Python & XlsxWriter - it is feasible to modify the code that creates the excels.
Yes. It is possible in XlsxWriter to generate the notes/comments as initially visible or hidden, either individually or for all comments in a worksheet. See

I want to create an add button

I am newbie in excel and I really need a button that if pressed adds a certain value to the cells I selected, is that possible?
You can add buttons to Excel by enabling developer mode:
On the File tab, go to Options > Customize Ribbon.
Under Customize the Ribbon and under Main Tabs, select the Developer check box.
Then on the developer ribbon you can click insert, then select a button to draw onto the sheet.
You will then be given the option to assign a macro to the button (Also later accessible by right clicking the button).
As for the VBA code of the macro, you would need to be more specific about what functionality you require, is the 'certain value' to be added to the selected cells always the same value?
Someone answered me in two minutes on another site with this perfect code for what I wanted, thanks a lot sir. It works like magic.
Sub Add_to_Selection()
If Not IsNumeric([E4]) Then
Exit Sub
Else
Dim cell As Range
For Each cell In Selection
cell.Value = cell.Value + [E4].Value
Next cell
End If
End Sub

How to trigger a macro with change of ribbon tab?

I have an Excel Workbook with a couple of custom ribbon tabs. I would like to activate a specified Sheet when clicking of particular custom ribbon tab.
Details:
The Workbook composed of a 5 Sheets has following additional ribbon tabs: "Parameters" and "Data Analysis". Each Tab has a few groups of controls. I need to add a trigger (macro?), which would automatically change a Sheet to Sheet3 (to run ActiveWorkbook.Sheets("Sheet3").Activate) only when a user picks the "Data Analysis" Tab.
I would appreciate any help.
It is possible to use a getVisible callback from a ribbon to achieve what you are looking for.
Here is an example xml for a tab (it should be nested inCustomUI and ribbon tags)
<tabs>
<tab id="ExampleTab" label="Example Tab">
<group id="ExampleGroup"
label="Example Group"
getVisible="GetVisibility"
tag="1"
>
</group>
</tab>
</tabs>
When someone clicks on the tab this xml will call GetVisibility sub in the workbook it belongs to. You can use tag property to pass a variable to the `Sub'. For example every tab can have its own tag, or it can be used for something else.
Now here is an example code for GetVisibility:
Public Sub GetVisibility(Control As IRibbonControl, ByRef Visible)
'your code to activate the worksheet can go here
Visible = True 'if you want to make a control visible
Select Case Control.Tag 'you can use Control.Tag to get a variable from ribbon
Case "1"
Case Else
End Select
End Sub
The only issue is that this code will normally only be called the first time you click on a tab. To get the code to rerun I think you need to invalidate your ribbon control. For that you would have to store your ribbon in a global scope variable at initialization. The last time I checked you may need a workaround for that, such as storing ribbon address in a worksheet. There is a number of question on Stack about that, for example: this one.

Microsoft Excel - define a "string" to get specific output

A friend of mine works at Verizon and asked me If excel has built in functions for whenever he types "open" into a cell it will return "8:30-5:00" into that cell.
I hounded google for an hour. I cant seem to find what I am looking for.
Thank you.
You can use Custom functions in Excel. Custom functions, like macros, use the Visual Basic for Applications (VBA) programming language.
https://support.office.com/en-us/article/Create-Custom-Functions-in-Excel-2007-2f06c10b-3622-40d6-a1b2-b6748ae8231f
You need a Worksheet Event Macro. This is a small routine that will constaintly monitor cells on a worksheet and take action if data is typed into them.
Say we want to monitor cell B9. Include the following in the worksheet code area:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim B9 As Range
Set B9 = Range("B9")
If Intersect(Target, B9) Is Nothing Then Exit Sub
If B9.Value <> "Open" Then Exit Sub
Application.EnableEvents = False
B9.Value = "8:30-5:00"
Application.EnableEvents = True
End Sub
Because it is worksheet code, it is very easy to install and automatic to use:
right-click the tab name near the bottom of the Excel window
select View Code - this brings up a VBE window
paste the stuff in and close the VBE window
If you have any concerns, first try it on a trial worksheet.
If you save the workbook, the macro will be saved with it.
If you are using a version of Excel later then 2003, you must save
the file as .xlsm rather than .xlsx
To remove the macro:
bring up the VBE windows as above
clear the code out
close the VBE window
To learn more about macros in general, see:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
and
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
To learn more about Event Macros (worksheet code), see:
http://www.mvps.org/dmcritchie/excel/event.htm
Macros must be enabled for this to work!
A non-VBA solution is possible using Excel's built-in Autocorrect facility.
On the File tab in Excel 2013 click Options -> Proofing -> Autocorrect and enter "open" in the Replace: box and "8:30-5:00" in the With: box (without the quotes).
This is not case sensitive so will work for both "open" and "Open".
If you are ever likely to want "open" or "Open" to appear in a string you could add an escape character such as backslash to your replace string "\open".

Trigger Event in Excel VBA when Row or Column is hidden

Is there a way, to trigger an event (call a sub) in Excel VBA, when i manually hide a row/column?
I want the same row to be hidden in all following sheets, when it is hidden in a particular sheet.
Is that possible?
Thanks in advance
There is no direct event trigger to capture hiding or unhiding columns. There are clumsy workarounds, using formulae in cells but those feel like a kludge when using and not really flexible.
However, there is an indirect way to capture this event if you use Excel 2007 or newer. This is neat and extremely flexible.
Modify the Ribbon XML (if it's present): You need to be able to modify the Ribbon's customUI14.xml (for Excel 2010) or customUI.xml (for Excel 2007).
Create the custom Ribbon UI XML file (if not present): You may use Ron De Bruin's excellent Custom UI Editor from http://www.rondebruin.nl/win/s2/win001.htm (which is also endorsed in quite a few official Microsoft examples).
XML changes for capturing events: Open the Excel file in the Custom UI Editor and add the XML code as shown below. That will enable a column hide or unhide event to trigger a macro in your code.
VBA code to execute your actions: Add the code listed below to take action once the event is captured.
Reference: This excellent solution was provided by Andy Pope here (MSDN link).
Custom XML code:
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" >
<commands >
<command
idMso="ColumnsHide"
onAction="Column_Hide_Macro"/>
<command
idMso="ColumnsUnhide"
onAction="Column_UnHide_Macro"/>
</commands >
</customUI>
Custom UI Editor screenshot:
VBA code:
Sub Column_Hide_Macro(control As IRibbonControl, ByRef CancelDefault)
MsgBox ("You have hidden a column")
' You may put your code here
' to check if your monitored row is hidden
CancelDefault = False ' This enables the default action to continue
End Sub
Sub Column_UnHide_Macro(control As IRibbonControl, ByRef CancelDefault)
MsgBox ("You have unhidden a column")
' You may put your code here
' to check if your monitored row is unhidden
CancelDefault = False ' This enables the default action to continue
End Sub

Resources