Refresh cell before printing - excel

I'm trying to find a way to refresh the calculation of a specific cell before the printing because sometimes the value is incorrect until the sheet is recalculated.
I tried with:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
For Each wk in Worksheets
wk.Calculate
Next
End Sub
But I think this doesn't work anymore on the latest Excel versions or maybe I don't how to use it.

Related

How To: Let users paste cells from another excel instance (Ctrl+V,...) but paste only values

EDITED WITH PROGRESS
Dear Stackoverflow community,
I am working on a big excel file that does some calculations for me and my colleagues. Because the calculation data is a lot and is entered in Ranges (like "A1:H8"), not single cells (like "A1","C1",...), I want the users to be able to copy data from the same or another excel instance to my file.
The problem (edited):
The problem is, that just pasting cells formats the target cells (even if they are protected against formatting) and this has to be avoided. I searched through a lot of online discussions and finally made my own code, that allows me to copy and paste between two excel files in the same excel instance. Sadly, it does not work, if I copied the cells from another instance.
The code:
This is the code I use in "ThisWorkbook":
Sub PasteValuesOnly()
'if cells are pasted in named worksheets, only values are pasted
'is linked to Ctrl+V in options of macro menu
On Error GoTo err_handler
Dim Target As Range
Set Target = Selection
If Target.Parent.Name <> "Table1" Then
Selection.PasteSpecial
Else
Selection.PasteSpecial Paste:=xlPasteValues
End If
err_handler:
Exit Sub
End Sub
The system:
Windows 7
Excel 2010
What I tried besides my code (new progress):
As mentioned in the comments, I know Siddharth Rout's solution for only letting the users paste values, but I can't get it to work (not even in a fresh file when copying and pasting inside one Excel instance). I tried it for the whole workbook and for one single sheet.
What would help (edited):
It would be very helpful, if you could tell me how to optimize my code, so it works for two instances as well. If you know what is to do when I have an error with UndoList = Application.CommandBars(“Standard”).Controls(“&Undo”).List(1) in Siddharth Rout's solution with Excel 2010, this would be helpful, too.
Otherwise I would like every solution, that let's my users paste like they ever do, but prevent them from formatting the cells while pasting.
Thank you in advance
RaspiManu
After long hours of searching the internet, I found the solution of Donna Landy (Bella_Donna) in the microsoft forum. Her code is simple and works for CTRL+C / CTRL+V, Copy and Paste over Right Click Menu, Drag'n'Drop and even with two excel instances.
Because it starts on every single cell change and goes back to the cell or range that was changed, I slightly optimized it for my needs. Now, users that enter a list manually won't have to press "Enter" two times every time, they want to get to the next line below.
Assuming, the standard user will normally copy and paste, if there is a range of data, he or she does not want to retype, I changed the code, so the module sub only gets activated, if more than one cell has been changed (see below).
The solution:
In every worksheet, that has to be protected against formatting (modified):
Private Sub Worksheet_Change(ByVal Target As Range)
'activates format protection when changing a range
If Target.Cells.Count > 1 Then 'If more than one cell has been changed...
Call Worksheet_Change_Protected(Target) '...activating protection
End If
End Sub
In a module (unmodified):
Sub Worksheet_Change_Protected(ByVal Target As Range)
'Prevents user blithely obliterating in-cell formatting by undoing their paste and pasting the value
'Donna Landy 26.11.2018
'May be freely copied - hat tip appreciated :)
Dim SavedVal As Variant
On Error GoTo ErrHan
'Save the pasted value for later
SavedVal = Target.Value
'Switch off events to prevent infinite loop
Application.EnableEvents = False
'Undo the user's paste
Application.Undo
'Set target value
Target.Value = SavedVal
ErrExit:
'Remember to re-enable events
Application.EnableEvents = True
Exit Sub
ErrHan:
Resume ErrExit
End Sub
Thank you very much, Donna Landy!

Clicking on a dynamic hyperlink with VBA to jump to a new position within the same sheet

I have a large chunk of VBA code that generates a result sheet. It's fairly large, and in order to better, faster dig through it, I've added a dynamic hyperlink at the top of the sheet, with a drop-down menu next to it. Drop down the item, click the hyperlink, and you get whooshed over to the part of the spreadsheet you want to get to.
I've been asked to make it even easier, and when you select an item from the drop down menu, to auto-whoosh you to the correct location. So an on-trigger macro to click a dynamic hyperlink.
Ok, so far, so good. Do some googling, and I end up with the following:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("HyperlinkType")) Is Nothing Then ClickHyperlink
End Sub
Private Sub ClickHyperlink()
ThisWorkbook.Names("HyperLinkTotal").RefersToRange.Hyperlinks(1).Follow
End Sub
Unfortunately, this results in a subscript out of range, which apparently can happen with dynamic hyperlinks.
The hyperlink formula for reference:
=IFERROR(HYPERLINK("#Totals!B"&MATCH(HyperlinkType,B:B,0),"Jump to "&HyperlinkType),"Please enter a valid type")
1) How do I fix the subscript out of range issue?
2) Is there a better way than hyperlink(1)? It almost looks to me like it's indexing the hyperlink, and I'm not sure that's exactly what I'm looking for - I'm looking for the hyperlink in the cell, not the first in the workbook. I may be misunderstanding.
Previous instances of this, and similar question on stack overflow:
Excel Macro executing Hyperlink shows 'Subscript out of range error' - no answer
Hyperlinks.Follow error: Run-time error '9': Subscript out of range - completely different method used to solve that particular issue (XY problem)
Hyperlink code shows Subscript out of range error vba excel - used a reserved word as a variable
VBA to open Excel hyperlink does not work when hyperlink generated with a formula - Seems to be promising, I think this might solve it.
Thanks to #Forward Ed, I was able to get it working with select.
Forgive the lazy lack of variables:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("HyperlinkType")) Is Nothing Then ClickHyperlink (Me.Range("HyperlinkType").Value)
End Sub
Private Sub ClickHyperlink(ActuarialString As String)
Dim ResultRow As Long
ResultRow = Me.Range("B:B").Find(ActuarialString).Row
Me.Cells(ResultRow, 2).Select
End Sub
To put it another way: If you want to click on a dynamic hyperlink, you're probably running into the XY problem. Step back, figure out exactly what you're trying to accomplish, and use one of VBA's other tools to do it.

VBA - Worksheet Event

I receive every week a file that has always the same structure and format and what I want to do is to get a message when double clicking in some the cells of one of the columns.
I have the code and it works but I have to paste everytime in the worksheet once I open it. I want make this code "generic" so I can use it automatically everytime I open one of these workbooks without having to copy every time or do anything rather than the double clicking to get the message.
Private Sub Worksheet_BeforeDoubleClick(ByVal target As Range, cancel As Boolean)
If Not Application.Intersect(target, myrange) Is Nothing Then
cancel = True
MsgBox "some message"
End If
End Sub
Some comments.. the sheet i need to work with it's always called the same and the column I need to work with is always the same too. The only change is the number of rows it contains.
After some research I don't know if I should do this in a class module or as an addi-in. I'm a begginer in VBA so this is out of my scope...yet.
Thanks!

Double click in excel 2016

I am using excel 2016 with all protection set off or as low as possible. I want to make a double click on a cell.
I have pasted the code in "module 1", directly into the worksheet.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
MsgBox ("WHAT?")
End Sub
Put the code in Sheet1 or whatever one you want. These events are not fired in modules...
Worksheet_SomeEvent(...) are Worksheet functions so the code for such must be placed in worksheets in order to fire.

Excel VBA - prevent cell from changing upon edit

I would like to prevent a cell from being edited as I need to preserve the formula in it. The problem is, I want to copy paste a large range of values from another workbook over into my sheet, and the target range for pasting includes some cells which must not change. Thus, a simple lock option wouldn't work, as this would prevent me from doing this operation in one go.
I tried using a code wherein the cell changes back to its intended formula upon change of the cell, however, this yields an infinite loop. See simplified example below:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("F28")) Is Nothing Then
Range("F28").Formula = "=if(E28=0,0,G28/E28)"
End If
End Sub
Many thanks for your help, much appreciated.
Edit:
An automated solution that would transfer from this other workbook wouldn't work as I receive these files in various formats and workbook names.
If your problem is infinite loop than here goes quite common solution for the problem. You just need to switch off events for a while and than switch them on back. Your code could looks like:
Private Sub Worksheet_Change(ByVal Target As Range)
'turns off events
Application.EnableEvents = False
If Not Intersect(Target, Range("F28")) Is Nothing Then
Range("F28").Formula = "=if(E28=0,0,G28/E28)"
End If
'turns on events back
Application.EnableEvents = True
End Sub
You have to make a Sub that enters the "resident" formulas. Just make a call to this sub after pasting the new values.

Resources