I'm trying to write a macro and I have the 2nd half done (tile windows showing specific sheets) but not sure how to do the first half.
There are some sheets that when displayed I would like to look at two other sheets at the same time (multiple window tiling). This is easy to set up manually, but I'd like it to happen automatically when I click on any of the 3 sheet tabs that are in the spreadsheet. There are other sheets that should be handled normally and have only 1 window open.
I'm thinking that I need to set up some sort of onClick event, but I haven't ran into it before in Excel and didn't see anything initially in Google.
Google figured it out once I rephrased my question. In VBA, select the Sheet object (not a module) and use this code:
Private Sub Worksheet_Activate()
'Your code goes here
End Sub
The code will be executed when the sheet is activated.
My reference: http://www.mrexcel.com/forum/showthread.php?t=71663
Related
I'm using Excel 2016 on my Mac (v10.11) and currently working on a table for the accounting of my company. It contains two sheets: On the first sheet I list up some costs and give them a projectnr. These costs get copied and pasted on the second sheet. The projectnrs get copied in a protected column, where I need to delete all duplicates. I want to automate this process. if you click on a button on the first page, the rest of the game will be done by itself.
For this, I wrote a VBA function like this:
Option Explicit
Sub Copy()
ThisWorkbook.Worksheets("FirstTable").Range("R21:R84").Copy
ThisWorkbook.Worksheets("SecondTable").Cells(3, 26).PasteSpecial xlPasteValues
ThisWorkbook.Worksheets("SecondTable").Activate
ThisWorkbook.Worksheets("SecondTable").Range("Z:Z").RemoveDuplicates Columns:=1, Header:=xlYes
ThisWorkbook.Worksheets("FirstTable").Activate
ActiveWorkbook.Save
Call Print
End Sub
The problem is that the method RemoveDuplicates does not work on mac! I've tested it on windows and everything works well. Does anyone know what the reason for this could be?
If I mark the column on my own and click on remove duplicates in the data tab, it works, but not with this macro.
I found the reason why it doesn't work:
On mac it pops up a dialog, which you need to confirm. If you don't hit the confirm button, the duplicates won't be removed. After I call the RemoveDuplicates function, I switch to the FirstTable by calling ThisWorkbook.Worksheets("FirstTable").Activate. The dialog disappears and the duplicates are not deleted.
On windows there is no dialog, thats why it works.
I want to do an automation using Excel VBA. First of all I am not sure if its possible so I need to explain the problem first.
I would launch an application which has a list of reports , that one can run by right clicking on any of these and selecting an entry from the popup menu caled say "Run this report". The problem is the report names are displayed in a listbox.
There are so many entries I only need to run a few of them based on their names.
To achieve this I thought about placing the mouse cursor on the text displaying the appropriate report name and then trigger the right click event. These can be done using Windows APIs.
The challenge I am facing is how to hover my mouse on any particular list item based on its display text.
I can enumerate all the windows controls based on the handle of the application's window, but is it possible to get the location of any item on the screen based on the text displayed on list item.
I have a spreadsheet of about 20,000 rows. This spreadsheet is a hierarchy that displays part lists for pieces of equipment. I have created code that creates buttons (actually the shape rectangle) that allow the user to expand / contract these groups through hiding / unhiding rows.
I believe the code is running slow because there are several thousand buttons on the spreadsheet that I believe are refreshing each time rows are hidden. When I click the button the code runs, the correct rows hide / unhide, then all the buttons dissapear for a few seconds then reapear. The disapearing occurs after the rows hide or unhide so the code has already finnished running.
Is there any way to stop Excel from refreshing every button? Is there a different issue?
It sounds like you could make use of the Pivot Table feature in Excel. I'm sure it's much faster than the macros/buttons the spreadsheet is using:
PIVOT TABLE OUTLINE
You just need to put in the effort of re-organizing your data into a decent format. Good Luck.
If you need an explanation of pivot tables you can just google "pivot table tutorial.
http://www.google.com/search?q=%22pivot+table+tutorial%22
I have used a workaround which I'll elaborate more when the code is handy,but is like so:
Have your button creating macro create hyperlinks in a cell instead with the text +/- and a tool tip of >>expand/collapse<<
ActiveSheet.Hyperlinks.Add _
Anchor:=[A1], Address:="", _
SubAddress:=[A1].Address, _
ScreenTip:=">>expand/collapse<<", _
TextToDisplay:="+/-"
In the workseet_followhyperlink event do a check to see if the tool tip is >>expand/collapse<< and if so follow you macro for sowing and hiding.
if target.screentip = ">>expand/collapse<<" then
myShowHideMacro
else
'do other stuff
end if
I have recently been asked if I could make a macro in Excel VBA that will allow a user to type in two numbers and have it automatically drop to the next row. The purpose of this is so they can type in grades for a test two numbers at a time without pressing enter since they aren't great at typing.
When I first heard this he mentioned it was Visual Basic, so I figured I'd just use a TextChanging or TextChanged event in the cell range and have it work off that. However, I haven't been able to find any such event or anything resembling it in the documentation thus far. The first thing that I came across was Workbook_Change, but that only changes after you press enter which is useless to me. Someone else mentioned there is such an event, but couldn't name it directly and I haven't been able to find what they were talking about.
If anyone has any information on if such an event exists or is possible I'd love to know.
The Excel version is 2007 as far as I'm aware.
This, in my opinion, requires a non-programming solution. I absolutely sympathize - it is tough to watch people get old - but you have to draw the line somewhere - for their sake and yours. The enter key is the most basic part of a computer. You could probably write a macro that would automatically hit enter on every even(or odd depending) keystroke in excel - but you're going to run into other problems like not being able to use delete normally. And what if you do want to put a string of text in a cell(like the student's name)? Perhaps it is time to find a non-programming solution. By that I mean someone should have a candid conversation with him about how he wants to solve the problem. Personally, I would offer to type the numbers in for him, as I am accustomed to the number pad - but it is probably better to be more direct and start to discuss retirement.
See this discussion about the limitations of cell edit mode in excel:
http://www.mrexcel.com/forum/excel-questions/524860-call-macro-every-keystroke.html
If you're really heart-set on a programming solution, I would recommend some kind of keystroke logging add-in.
Good Luck.
You could use the Worksheet_SelectionChange event. It is triggered without enter, but it would be triggered a lot.
You could however also create a special user-form for typing in the data, but this might be more work than necessary.
The main problem with using my suggested event is, you will need it as trigger and trigger it yourself, when selecting the next row, so disable event handling before changing the selection.
Edit:
This is a quick solution (paste this into the vba-code of the desired worksheet):
Private Const clngColumnRightToLastGrade = 5
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Column = clngColumnRightToLastGrade Then
Application.EnableEvents = False
'offset selection, one row down, two cols to left
Target.Offset(1, -2).Select
Application.EnableEvents = True
End If
End Sub
This will set you one row down and to column C, everytime your selection changes to column E (=5).
You don't have to use a constant of course, you could specify the column to sense in the workbook, so your user might modify it easier by himself.
To make this as an optional feature, you could extend it to autogenerated code. What I have in mind is like a Ribbon-Button, which opens a setupForm to configure, and a Ribbon-Button to activate the configuration, which would place this code in the configured sheet. But this might be a bit over the top.
In Excel 2003, (may be different in Excel2007 ?!) the WorkSheet_Change event is triggered every time the value of a cell is changed wether it is by pressing enter, delete, selecting an other cell after modifying a cell or even when a vba script changes the value of a cell.
I would do something like that:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim RefRange As Range
Set RefRange = Intersect(ActiveSheet.Columns("??????????"), ActiveSheet.UsedRange)
If Not Intersect(Target, RefRange) Is Nothing Then
Target.Offset(0, 1).EntireColumn.Range("A1").Select
'Target.Offset(0, 1).EntireColumn.Range("A65536").End(xlUp).Offset(1,0).Select
End If
End Sub
I'd like to add buttons to specific cells in Google docs spreadsheet. The apps script UI documentation talks about how to add a new panel, but it's not clear how UI in that panel could be attached to specific rows or cells.
Is it possible to add UI to particular cells, or are we limited to adding new panels?
The apps UI only works for panels.
The best you can do is to draw a button yourself and put that into your spreadsheet. Than you can add a macro to it.
Go into "Insert > Drawing...", Draw a button and add it to the spreadsheet.
Than click it and click "assign Macro...", then insert the name of the function you wish to execute there. The function must be defined in a script in the spreadsheet.
Alternatively you can also draw the button somewhere else and insert it as an image.
More info: https://developers.google.com/apps-script/guides/menus
Status 2018:
There seems to be no way to place buttons (drawings, images) within cells in a way that would allow them to be linked to Apps Script functions.
This being said, there are some things that you can indeed do:
You can...
You can place images within cells using IMAGE(URL), but they cannot be linked to Apps Script functions.
You can place images within cells and link them to URLs using:
=HYPERLINK("http://example.com"; IMAGE("http://example.com/myimage.png"; 1))
You can create drawings as described in the answer of #Eduardo and they can be linked to Apps Script functions, but they will be stand-alone items that float freely "above" the spreadsheet and cannot be positioned in cells. They cannot be copied from cell to cell and they do not have a row or col position that the script function could read.
Use checkboxes(say, in F1) instead of buttons/Images. This is a intermediate to a full ui inside cells. Then hook your function, that is supposed to run on button click to onEdit() trigger function.
Sample script:
function onEdit(e){
const rg = e.range;
if(rg.getA1Notation() === "F1" && rg.isChecked() && rg.getSheet().getName() === "Sheet1"){
callFunctionAttachedToImage();
rg.uncheck();
}
}
References:
Class Range
Event Objects
Buttons can be added to frozen rows as images. Assigning a function within the attached script to the button makes it possible to run the function. The comment which says you can not is of course a very old comment, possibly things have changed now.
There is a silly trick to do something that might help you :
You can make the drawing object as tall as your sheet (To appear to every row in the sheet).
You can make the script affects the current cell value by the following code:
SpreadsheetApp.getActiveSpreadsheet().getActiveCell().setValue(cellValue);