Maro to always copy and paste as formulas by default in Excel - vba

I need help to check this macro that intends to copy and paste without formatting. It doesn't work fine.
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
On Error Resume Next
Target.PasteSpecial xlPasteFormulas
Application.CutCopyMode = True
End Sub
How do I make Ctrl-V to paste without any format? I want to keep the excel clean and tidy, so users can copy and paste values and formulas without formatting.
Thanks!

If you want to make your own, custom Ctrl-V, well, you can achieve it this way:
' Code Module ThisWorkbook
Private Sub Workbook_Open()
Application.OnKey "^v", "ThisWorkbook.PasteWithoutFormat"
End Sub
Private Sub PasteWithoutFormat()
If Application.CutCopyMode Then Application.Selection.PasteSpecial xlPasteFormulas
End Sub

Try this: Assigning Macros to Short-Cut Keys in Excel
You'll need to work out how users have access to the macro - there are a few ways to do this and I'm not so sure what will work in your environment, nor your access level to make these options work.
I suppose I'd build a very simple ADD-IN in which I'd embed the macro and there also, assign the macro shortcut to override CTRL + V. Then deploy the ADD-IN according to your company policies.
However, we need more details from you to fully implement the solution.

I went for the easiest way. I set up Ctr-v (in macro's options) to run the following code:
Sub Pstfrmul()
Selection.PasteSpecial xlPasteFormulas
End Sub
Many many thanks for all your answers and comments. Best,
GerĂ³nimo

Related

Excel VBA protect returning error

I've seen many posts on here and elsewhere about using the following code in the ThisWorkbook Object in order to protect your workbook but still allow macros to run:
Private Sub Workbook_Open()
ActiveWorkbook.Protect UserInterfaceOnly:=True
End Sub
However, the protect function tooltip that pops up only offers three arguments Protect([password], [structure], [windows]). Therefore, when I open the document I get the following error
"Compile Error: Named argument not found"
in regard to UserInterfaceOnly:=True.
Am I using the wrong protect function, or is there something else I'm missing? I want to allow users to input data into the workbook using forms/macros, but I don't want them to be able to manually change any cells.
It almost seems as if the protect function that I'm running has different arguments than many of the other protect solutions I've found. Could this be a version issue or something else?
The suggestion provided by Tim Williams worked. Protect doesn't work the same way at the workbook level, therefore I needed to write individual code for each sheet I wanted to protect. I used the following code to protect the sheets but allow forms to populate:
Private Sub Workbook_Open()
Worksheets("Sheet1").Protect UserInterfaceOnly:=True
Worksheets("Sheet2").Protect UserInterfaceOnly:=True
Worksheets("Sheet3").Protect UserInterfaceOnly:=True
End Sub

Disable delete/insert option of rows/columns/cells in a workbook

I'm currently trying to figure out how to disable the delete/insert options of cells/rows/columns in a workbook. Currently, I'm working with the following VBA code to disable the delete option for rows and columns. However, this still allows me to delete cells (not clear content, but the "delete" where other cells need to shift). Also, I want to also disable the insert option for rows/columns/cells. Here's the code I'm currently using:
Private Sub Workbook_open()
Dim dis As CommandBarControl
For Each dis In Application.CommandBars.FindControls(ID:=293)
dis.Enabled = False
Next
For Each dis In Application.CommandBars.FindControls(ID:=294)
dis.Enabled = False
Next
End Sub
I was trying to search for the "ID" of the delete cell and insert options so I could just replicate the above code, but I can't seem to find it. Any help will be greatly appreciated, thanks! :)
You can use the sheet protection with VBA code like this :
Private Sub Workbook_Open()
Dim sh As Variant
For Each sh In Sheets
sh.Protect AllowInsertingColumns:=False, AllowInsertingRows:=False, UserInterfaceOnly:=True
Next sh
End Sub
The classical answer for disable delete/insert option of rows/columns/cells in a workbook is simply to protect the worksheet with a password and to define what can be done in the parameters.
This is an example, not allowing inserting columns and rows:
Worksheets(1).Protect AllowInsertingColumns:=True, AllowInsertingRows:=True

VBA Center Across Selection Hotkey

I am trying to create a hotkey shortcut for the "Center Across Selection" functionality. The "Merge Cells" functionality looks nice and all, but is horrid to use in practice because of the problems it creates with Macros and such. So, naturally, center across selection is the go-to tool. I use the keyboard to navigate in Excel, so it would be very nice to have a custom shortcut to make this selection easier and faster, since it is used often.
I have some code for this, which I believe should work, but for some reason, it is not:
In PERSONAL.XLSB - Module 1
Private Sub WorkBook_Open()
Application.OnKey "^q", "center_across_selection"
End Sub
In PERSONAL.XLSB - Module 3
Sub center_across_selection()
With Selection
'converts centered text to regular format
If .HorizontalAlignment = xlCenterAcrossSelection Then
.HorizontalAlignment = xlGeneral
'converts regular text to centered across selection
Else
Selection.HorizontalAlignment = xlCenterAcrossSelection
End If
End With
End Sub
Then, once I save this, exit out of the workbook (and all other workbooks as well), reopen the workbook, and try to use the Ctrl+Q shortcut, nothing happens to my cells! Please help me find where I am going wrong.
Move Private Sub WorkBook_Open() from MODULE 1 to the workbook code area.
You've implemented the macro in a standard module; Workbook_Open means to handle the Workbook object's Open event, but a standard module doesn't do that (the ThisWorkbook class/Excel object module does, as shown in Gary's (well, his student's anyway) answer).
So either you move the macro to ThisWorkbook's code-behind, or you rename the macro to Excel4-compatible Auto_Open:
Private Sub Auto_Open()
Application.OnKey "^q", "center_across_selection"
End Sub
Note that this Auto_Open macro will run regardless of the state of Application.EnableEvants when the workbook is opened.

Excel functions don't work with macro

So I got this macro to load stuff from another worksheet:
Private Sub Workbook_Open()
Workbooks.Open Filename:= _
"http://DOMAIN/csdav/nodes/5865160/Projektoffertliste.xls"
ThisWorkbook.RefreshAll
ActiveWorkbook.Close
End Sub
and if I try using simple fuctions like =VALUE or =IF it just won't work and it says #VALUE as error code.
=DATEVALUE('http://DOMAIN/csdav/nodes/5865160/[Projektoffertliste.xls]Projektoffertliste'!B2)
Thats one usage example, i tried this too with =VALE because I first thought it was a DATEVALUE problem. the http://.... data getting does work but i can't use it with functions.

VBA. What is incorrect in this code?

Searching found code like this
Sub Workbook_Activate()
Application.OnKey "+^{RIGHT}", "YourMacroName"
End Sub
However, when I tried, I got
How to create procedure?
I did this
Sub YourMacroName()
Selection.Copy
Sheets("V").Select
End Sub
Sub Workbook_Activate()
Application.OnKey "+^{RIGHT}", "YourMacroName"
End Sub
Got the same error
What would be correct code? Or where would be tutorial for dummies? Found some examples, but they does not work
I see my tags were modified to excel and excel-vba. But I do not use excel. Use Kingsoft Office
Changed Application.OnKey "+^{RIGHT}", "YourMacroName" to .OnKey Key:="^+M", Procedure:="YourMacroName"
and got
Then changed to OnKey Key:="^+M", Procedure:="YourMacroName" (removed .) and got error Named argument not found. And get selected Key:=
In "ThisWorkbook", you will run code that is triggered by an event. I suggest you also put it in Workbook_Open instead of Workbook_Activate as you only need to store the shortcut once.
So, in the VB Editor, open the "Project Explorer" if it is not (CTRL+R) and find "ThisWorkbook" in the folder "Microsoft Excel Objects".
Here the code should look like this
Private Sub Workbook_Open()
' CTRL + SHIFT + RIGHT
Application.OnKey "+^{RIGHT}", "YourMacroName"
End Sub
Since, inside a Module (In the Project Explorer, right click on the folder "Modules" and select Insert > Module), put the macro "YourMacroName"
Sub YourMacroName()
Selection.Copy
Sheets("V").Select
End Sub
Oh, and you should probably rename your procedure "YourMacroName" for something more obvious...