Protected Excel workbook broken after pasting data from a "newer" workbook - vba

Given an excel workbook with an unlocked cell in a protected worksheet.
If I copy a cell from another workbook which was opened after target workbook, and paste it to the unlocked cell, it becomes locked and I can't do anything with it except undo the paste action.
On the other hand, if source workbook was opened before the target, copy-paste works as expected - target cell remains editable.
I've reproduced this on excel 2007 and 2010.
What am I asking is to reproduce the problem and advise how to handle this issue with VBA to avoid locking cells by users.

Following #Jeeped advise, I wrote this script and it works:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Sh.Unprotect Password:="pwd"
Target.Locked = False
Sh.Protect Password:="pwd"
End Sub
But there's a side effect. Undo cache will be cleared each time worksheet changes.

Related

Activating a different worksheet using VBA does not change the focus on it permanently

I want to select and modify different worksheets programmatically every time the workbook is saved. At the end however, I want to set the focus on a particular worksheet so that the workbook is saved with that particular worksheet in focus. What I'm noticing is that whenever the code executes it activates the worksheets, modifies them but at the end it goes back to the worksheet that I had selected before running the code.
Here's my code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Sheets(1).Activate
Debug.Print Sheets(1).Name
End Sub
The code above is executed in an empty, local workbook with 2 empty worksheets
Sheet1 and Sheet2. Whenever I save the workbook with Sheet2 selected, I see that it is indeed activated because the console log prints Sheet1, in the workbook however, the selected worksheet remains Sheet2.I'm using SAP's BusinessObjects Analysis but as noted above, the workbook is a local macro-enabled workbook that is not saved on the SAP NetWeaver platform.
Is it possible for me to permanetly set the focus to a different worksheet so that it's visible in the workbook?
Thanks
EDIT:
Oh no!!! I have the annoying problem of inconsistent behavior with the different save buttons once again and that is yet to be resolved! I just realized that if I save through the workbook save button the sheet permanently changes, however when I save through the code editor it doesn't. The previous problem I had experienced was on workbooks saved on SAP NetWeaver where VBA code is not executed through the workbook save button but is, through the code editor save button. I guess I will have to log an Oss with SAP for this inconsistency.
What you are saying is only possible, if someone has written:
Private Sub Worksheet_Activate()
Sheets(2).Activate
End Sub
At Worksheets(1).
Otherwise, the code you are using:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Sheets(1).Activate
Debug.Print Sheets(1).Name
End Sub
should activate the first Sheet and it should not be changed later.
I have no idea where Sheet2 is compared to Sheet1.
You say
The code (above) is executed in an empty, local workbook with 2 empty
worksheets Sheet1 and Sheet2. Whenever I save the workbook with
Sheet2 selected, I see that it is indeed activated because the console
log prints Sheet1, in the workbook however, the selected worksheet
remains Sheet2
Your code doesn't do anything to a sheet called Sheet2. It only looks at the first sheet in the tab order - the sheet could be called anything.
It activates the first sheet and then puts the name of the first sheet in the immediate window.
This code will select the sheet with the tab name Sheet2, it will then put the name of the activesheet (Sheet2) in cell A1 of the sheet with the tab name Sheet1.
Finally it selects the sheet with the codename Sheet3 (The codename is the name not in brackets in the Project Explorer).
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
With ThisWorkbook
.Worksheets("Sheet2").Select
.Worksheets("Sheet1").Range("A1") = ActiveSheet.Name
End With
Sheet3.Select
End Sub
Just use following code:
Sub activateSheet(sheetname As String)
'activates sheet of specific name you want.
Worksheets(sheetname).Activate
End Sub
then for select another sheet:
Sub activateSheet(sheetname As String)
'selects sheet of specific name you want.
Sheets(sheetname).Select
End Sub
Regards
Xsi

How do I lock cells containing formulas but still allow macros to work?

I have a worksheet that I make my employees fill out, and I have calculated cells that I want to lock so they cannot change them. I have selected the cells and selected properties and ensured that the "lock" checkbox is checked. When I protected the worksheet/workbook the "export to csv" macro button stopped working. In order to enable the macro to be completed I inserted this VB code into the Workbook:
Private Sub Workbook_Open()
Dim wSheet As Worksheet
For Each wSheet In Worksheets
wSheet.Protect Password:="password", _
UserInterFaceOnly:=True
Next wSheet
End Sub
This worked but had the unintended side effect of allowing my locked formulas to be able to be edited even though they were locked. Only cells containing non-formula values remained locked. What is the proper way to allow macros but still lock formula cells?
My solution was to lock the entire workbook and worksheets, then code into the VB button the disabling of the lock then the re-enabling the macro. Like so:
Sub MyMacro()
Sheet1.Unprotect Password:="password"
'insert code here
Sheet1.Protect Password:="password"
End Sub
I then deleted my Workbook_Open code.

vba to transfer code from module vba project to excel sheet vba project

I've developed an excel addin that imports sheets into the existing one. i would like to be able to double click anywhere and be able to jump back to the first sheet.
Private Sub Worksheet_BeforeDoubleClick(ByVal target As Range, cancel As Boolean)
Sheets(1).Select
cancel = True
End Sub
Another program writes a summary excel file. I've developed a add-in macro that when opened and run, Imports the full solution sheets and do some data manipulation. is there a way to transfer this code into the excel sheet vba project? thanks in advance
use workbook events
so type in ThisWorkbook code pane the following:
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
Sheets(1).Select
Cancel = True
End Sub
Press Alt+F11 to go to VBA development tab and then in the tab on the left there should be displayed your Excel worksheets.
Simply double click on the worksheets you want to apply that feature and paste the code :)
See this image for more details about where to click:
https://www.extendoffice.com/images/stories/doc-excel/doc-drop-down-list-prevent-paste/doc-drop-down-list-prevent-paste-1.png

Excel VBA DoubleClick

I'm working on a work project where I have an excel sheet with values that turn red when they are out of spec. What I'd like to do is be able to double click on a cell and have the sheet in my workbook pop up that has trending data on it. I have already created the sheet with the graph on it. Long story short, I'd like to be able to double click on a specific cell and have it bring up the corresponding sheet.
I have tried this code, and it will not work. Is anyone able to maybe write code from scratch or alter the code so I could use it? The cell I'm trying to click on is N9, and the sheet I want it to open is called "Alpha Final Rinse"
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, cancel As Boolean)
Sheets("Alpha Final Rinse").Select
End Sub
I'm doing this in Excel 2013. Thank you!
If you only want N9 to be able to switch focus to another worksheet, isolate Target with the Intersect method.
In the data worksheet's code sheet:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, cancel As Boolean)
If Not Intersect(Target, Range("N9")) Is Nothing Then
cancel = True
Worksheets("Alpha Final Rinse").Activate
End If
End Sub
Note that cancel = True is necessary to stop the user entering in-cell edit mode (assuming that has been enabled in Options).
Your code will work if:
it is installed in the worksheet code area of your data sheet (the sheet whose cells you are double-clicking)
macros are enabled
the file type is .xlsm rather than .xlsx

Macro from on open event for worksheet

I have a macro that I want to run as part of open event. I recorded the macro to copy and paste some columns on a worksheet. This macro will only work when I am on that worksheet.
Since I want to run this macro as one of many applications when workbook is open and it doesn't always open on this specific worksheet I would like the code to target the worksheet and run the macro.
Here is my code but for some reason it doesn't work :
Application.Run "'Workbook.xlsb'!Sheet5.Copy_Paste2"
If I'm understanding you, you need to call Copy_Paste2 from ThisWorkbook Open event.
Private Sub Workbook_Open()
Sheet5.Copy_Paste2
End Sub