Copying an Object Across a Workbook - vba

I have a workbook which displays a little coloured box based on some input metrics from another worksheet within the workbook. I want to display these little coloured boxes in an additional worksheet in the workbook. Is there a way to copy a shape across worksheets so that the colour will still update with the metrics rather than using the code again for a separate worksheet?
I essentially want to display this textbox with the coloured boxes/arrows in another worksheet as well.

A pretty dirty way to do something like this would be the Indirect-Picture-Copy-Solution.
Asume the art is at Sheet1 B2:D8 then just input a picture in Sheet2 (the picture doesn't matter, just pick the first you can find)
While the Picture is selected input in the formula bar =Sheet1!B2:D8.
Hope that helps ;)
EDIT
For making it dynamically put in any module:
Public Function testing() As Range
Set testing = Range(Sheet1.Shapes("Dia 1").TopLeftCell, Sheet1.Shapes("Dia 1").BottomRightCell)
End Function
(Make sure to change the names to fit your workbook/sheet/shapes....-names)
Then define a name (I'll pick TETE for this example)
Refers to: =testing()
Then the picture-formula is: =TETE
Whenever the size or position changes, your picture fits to it... still not a good way to solve your problem (to my eye)
Funny fact: making the picture-formula directly to =testing() will just pop an error

Related

Excel copy all values from one main sheet to various other sheets if they are a certain colour

Would really appreciate a solution to the below:
I am looking to have 8 sheets.
Main sheet that has all jobs, these are all currently sorted into the following colours :
Red - live
Green - invoiced/complete
Blue - quoted
Black - enquiry
Grey - dead/ lost
Purple - work in progress
Yellow - Retention
what i would like to do is keep the main sheet and have a sheet for each of the above. when the text becomes red for example i would like it to be transfered to the live sheet and vica versa for the rest. this should be in a macro
can anyone help?
Many thanks,
You say "when the text becomes red " so possibly this is a conditional format?
In any case, what you need to do is to attach code to the main sheet's Calculate event . This code should do the following
look at the activecell's color element that you mean (font, background, conditional formating, etc)
Based on that color, copy the entire row to the appropriate sheet
(Can you assume the sheets already exist?)
You will probably need a Select Case Statement. I would declare a worksheet variable and then SET it to the appropriate sheet in the select
and then
Activecell.entirerow.copy ws.cells(ws.rows.count,1).end(xlup).offset(1,0)
will copy the row to the desired sheet at the bottom of any existing rows.
Have a try and come back with code if you get stuck
EDIT: Sorry I missed the "click a button" part. You can ignore the bit about the sheets calculate event - just attach your code to the button. The only thing to worry about then is that you will need to run through all the used rows of the sheet, since there might be more than one coloured row when you click on the button.

Deciphering an Excel macro; Range.Select, then Range.Activate, then Selection.ClearContents?

This is the code I have in a [very involved] spreadsheet someone made at work:
Sub ClearSheet()
'
' Macro5 Macro
'
'
Range("E9,E2:F7,C14:I39,Q41:Q55,N14:N39,N41:N55").Select
Range("Q14").Activate
Range("E9,E2:F7,C14:I39,C41:I55,Q41:Q55,N14:N39,N41:N55,L41:L55").Select
Range("Q41").Activate
Selection.ClearContents
I have never so much as glanced at an excel macro before, so I had to look some things up. I get that the first range is selected and then Q14 becomes the active cell. Then that is done again, with some overlapping sections, and Q41 is made into the active cell. All to have the selections just be cleared out. I'm sure this is a simple question but I don't understand what the point is of the .Activates, or why someone would separate the sections that need to be cleared into two separate segments? From my very limited understanding, I thought Activate was something like focus, where that is now that cell that has focus for ease of use on the users side. But what good is that if the focus changes from the first cell to the second cell in a millisecond?
All I know is that I need these cells:
E9,E2:F7,C14:I39,N14:N39,C41:I55,L41:L55,N41:N55,Q41:Q55
to clear out when this code is run, and if this code is doing something in addition to that, what is it?
Is this just poorly written or am I too ignorant to understand? ~the novel~
Use
Range("E9,E2:F7,C14:I39,N14:N39,C41:I55,L41:L55,N41:N55,Q41:Q55").ClearContents
Better still specify the workbook and worksheet to do this in e.g.
ThisWorkbook.Worksheets("Sheet1").Range("E9,E2:F7,C14:I39,N14:N39,C41:I55,L41:L55,N41:N55,Q41:Q55").ClearContents
Using sheet 1 as an example. You want to be sure to be in the right sheet before clearing stuff out. If you don't specify, and leave as just range, then the currently Active sheet is used.
In the code you talked about the each selection was shifting focus from the prior making the prior selections redundant.
Using Select, in particular, is not generally a good thing, it means 'touching' the sheet which incurs potentially unnecessary performance overhead.
As mentioned in comments, and indicated by ' Macro5 Macro, this is, at least in part, likely all, macro generated code. Macro meaning "many". Many instructions in this case. The macro recorder is verbose to say the least. It records everything your are doing including scrolling, mistakes in range selections etc. It is a good learning tool, and can often give useful insights into some objects and methods. The valuable skill is learning which elements to keep and how to turn this verbose code into structured programming.
The way you interpret Select and Activate is correct, one is for the actual selection and the other is somewhat to focus.
Select as the method name suggest selects the object. This method is not limited to Range Objects alone but is shared by most of the objects in Excel. Some of the examples:
Range("A1").Select '/* selecting a Range Object */
Worksheets("Sheet1").Select '/* selecting a Sheet Object */
Activate on the other hand works when you already selected an object.
Activates a single cell, which must be inside the current selection. To select a range of cells, use the Select method.
So what happens when you activate a cell not in the current selection?
It becomes the selected cell and as you've said, Excel executes the Select first and then the Activate in mili or nano or pico seconds (God knows how fast) interval.
In Range Objects the use of Select and Activate is almost interchangeable. But you have to take note that there will be difference always with Selection and ActiveCell. For example:
Range("A1:B10").Select
Range("B5").Activate
Debug.Print Selection.Address
Debug.Print ActiveCell.Address
This means that you can actually do stuff (e.g. format, clear, add formula, add text etc.) on all cells you activate within the current selection but still preserves what Selection object points to.
There are cases that activating the object is vital. For example you want to select multiple worksheets like below and then select Range("A1") of Sheet3.
Worksheets(Array("Sheet1", "Sheet3", "Sheet5")).Select
Worksheets("Sheet3").Activate '/* vital */
Worksheets("Sheet3").Range("A1").Select
Above is the correct select command for multiple worksheet selection and selecting a range within 1 of the worksheets selected. But without the Activate part, there is a chance that it will return:
Run-time error '1004': Select method of Range class failed
because the first sheet in the array will always be the activated sheet object after the select. Now, how to avoid this troubles? Simple, avoid using select and activate. ~the novel sequel~

Excel stops scrolling after a Copy command

I have a set of data entries in the 2nd Worksheet of my Workbook, which are part of a list using Filters. Using VBA I have created a button that when clicked, opens a Userform which lets the user enter their desired data selection to be copied from the 2nd Worksheet to the 1st Worksheet. This is done by having the user checking 1 out of 8 possible OptionButtons and then having the user select 1 of 5 possible options from a ComboBox. All is well and every possible combination in the Userform inputs leads to the correct data being copied from the 2nd Worksheet in to the 1st Worksheet. However, after a successful copy, I am unable to scroll using my scroll wheel in my 1st Worksheet. The "square" moves in the scrollbar when scrolling with the scroll wheel, however the worksheet does not move.
The main differences between the two Worksheets are that in the 1st Worksheet, no filters are present and that the copied selection is always less data then the source data, however this isn't to big of a difference (source data in 2nd worksheet is about 300 rows, smallest copy possibility is around 12 rows). I've searched and tried the following solutions, with the accompanying results.
Unfreeze Panes; One solution suggested was to unfreeze panes as Excel would possibly freeze all rows after the copy. However, when checked there was no "Unfreeze" option, indicating that I had no frozen panes to begin with. Also, when I clicked any of the three "Freeze" options, thinking I would try to manually "Freeze" and then "Unfreeze" to resolve the issue, Excel would stop working, giving me the "Not Responding" in the title bar and needed to be shut down/restarted.
Select Objects; Another suggestion was that Excel would have the "Select Objects" checked in the "Find & Select" portion of the "Home" tab. When un-checking this, the issue should be resolved. However, it wasn't checked to begin with. Checking it and then un-checking it did not resolve the problem.
Switch Worksheets; When I manually click the 2nd Worksheet after a copy command and then click back to the 1st Worksheet, the problem is resolved and I can use the scroll wheel again. However, I intend to use this Workbook a lot and having to constantly click back and forth is going to be a pain. So this isn't really a solution, more of a work around.
Dragging; Another work around I have found is that clicking and then dragging the "square" in the scrollbar does move the worksheet around. However, this is again just a temporarily fix.
One more thing that may be useful to mention. In both Worksheet 1 & 2 I am using collapsible columns. Worksheet 2 also contains collapsible rows, where Worksheet 1 does not.
Okay, so on a whim I tried to enter the following 2 lines of code to the end of the sub
ThisWorkbook.Sheets("Overview").Activate 'Activates the 2nd Worksheet
ThisWorkbook.Sheets("Selection").Activate 'Activates the 1st Worksheet
This mimics the manually triggering of switching Worksheets. And it resolved the problem. Still don't understand why the problem occurred but it's solved anyway.
I had a similar issue and my initial workaround was to make the userForm modeless:
Sub addButton()
builderForm.Show (0)
End Sub
I wasn't a huge fan of leaving it modeless, so I ended up using your sheet toggling method like this:
Sub addButton()
builderForm.Show
Sheets("Cover").Activate
Sheets(Worksheets.count - 10).Activate
End Sub
I still don't fully understand why it is happening; it's almost like windows/excel loses track of which sheet is the active one while the userForm is running.
Thanks for your submission/answer!
This also happens when a worksheet has too many copied Conditional Formatting rules. Once I cleaned up my extra/duplicated rules, I could scroll again after copying a cell.
Found good info about copying/pasting with or without the Conditional Formats here https://superuser.com/questions/419287/how-to-copy-paste-without-conditional-formatting

How do I save cells just pasted over?

I am working on a change management. I have good, robust code for single cell changes with old and new target values. for pasting a range, however, it is more complicated. for instance, if i copy range A1:A5 and paste to B1, how do i save the previous values in B1:B5, saving B1 is easy, the rest is beyond me.
I did something similar, the method was fairly simple, when something is pasted it is selected, save the selection.address into a range variable.
Then save each cell's contents to string variables.
then undo the paste.
then copy out the now restored contents of the range variable to wherever you want for backup purposes.
then write back the variables with the originally pasted values over the top.
Set the code to run on a worksheet change event and you are all good. Obviously you need to put in some code to detect that only certain areas are in the target so the event doesn't go bananas and don't forget to turn the events off whilst you do you stuff then back on after.

Is it possible to have a cell that has a formula and accepts entry at same time in excel?

Example:
A B
1 =vlookup(XX)
2
3
in cell A1 there is a Vlookup formula, Is it possible to enable user entry in this cell and override the formula then later restore the formula automatically when sheet is open again?
Even through VBA
Short, boring answer: nope.
A cell only ever has a keyed-in value, or a calculated formula. Can't have both.
Longer answer: maybe.
Shift everything 1 row down, and use row 1 to store your "original" formula - then hide that row (and pray the user isn't going to mess with it).
When the sheet is opened again sounds like you're confusing "workbook" and "worksheet" - you need to handle Workbook_Open if you want to run code when a workbook opens. Workbooks contain worksheets - it's the workbook that opens, not the sheets (sheets activate, but I doubt you would want to put that logic in there).
So, in the handler for Workbook_Open, write code that takes the formula in the hidden row and overwrites whatever is under it.
Another solution can be to hard-code the formula in the VBA code.
One possibility would be to store your Workbook as a template. Normally when a user opens the workbook by double-clicking, it will open whole new workbook based on the template, and they can modify it to their heart's content, save it, mail it to Grandma, etc.
The next person who comes along will double-click the template file and get the formula again, just as you designed it.
Short answer: Kind of, sort of
Long answer:
Save your workbook as a template. Every time someone will use it you'll see the orignal with formula, then if someone write over the formula, when using save your original will be kept intact.
What You need to do is:
press Alt + F11
select ThisWorkbook and paste this code:
Private Sub Workbook_Open()
Worksheets("Sheet1").Range("A11").Value = "asdf"
End Sub
Every time the workbook is opened, this script will run.
Instead of "Sheet1" you can write the name of the sheet you want to apply the script.
Inside the Range, You can define the cells you want to modify, You can use even multiple cells. Check this for more information about this.
After Value You can write what You want to be written inside the cell. You can write "=vlookup(XX)" and it will work.