VBA code to lock only user selected (Highlighted) cells in excel - excel

I was wondering how can i one use VBA/macros to lock certain excel cells that are selected/highlighted by the user.
The code im using right now is locking the entire sheet.
Sub Macro4()
'
' Macro4 Macro
'
'
Worksheets("Sheet1").Activate
ActiveSheet.Unprotect
Cells.Select
Selection.Locked = True
ActiveSheet.Protect
End Sub
Any ideas on what im doing wrong?
Thank you for your time.

If you want to perform any actions on the selected cell(s) every time a new selection occurs, you should rely on the code being triggered when this happens:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Selection.Locked = True
End Sub
This inside the file with the code for the given sheet; that is, if you want to consider Sheet1, the file where you have to write this code is: Microsoft Excel Objects/Sheet1 (Sheet1).
UPDATE AFTER YOUR COMMENT
Sub Button1_Click()
Selection.Locked = True
End Sub
This code locks all the cells selected when the Button1 is clicked.

Related

VBA disabling drag and drop prevents users to copy and paste to another workbook

I've tried to disable drag and drop on a specific Excel workbook.
I wrote the following code on VBA editor on 'My Workbook' section.
Private Sub Workbook_Activate()
Application.CellDragAndDrop = False
End Sub
Private Sub Workbook_Deactivate()
Application.CellDragAndDrop = True
End Sub
Problem is: after that people cannot copy-paste any cell from this workbook to another.
How's so?
Thank you for any suggestion

How to assign macro to button without using "active sheet" in VBA?

I am trying to change the macro assigned to a button from a different sheet than where the button is. This is how far I've gotten:
Sub Macro1()
ActiveSheet.Shapes.Range(Array("Button 1")).Select
Selection.OnAction = "Macro2"
End Sub
However, the above code requres me to be on the active sheet. I have tried the following:
Sub Macro1()
Sheet1.Shapes.Range(Array("Button 1")).Select
Selection.OnAction = "Macro2"
End Sub
However, this will give me an "Object doesn't support this property or method" error.
Why doesn't it work when "ActiveSheet" is replaced with "Sheet1"?
And why can't I collect the two lines of code into one line?:
Sub Macro1()
Sheet1.Shapes.Range(Array("Button 1")).OnAction = "Macro2"
End Sub
Any help would be appreciated!
Please, simple try:
Sheet1.Shapes("Button 1").OnAction = "Macro2"
Of course, a macro named "Macro2" should exist in a standard module. If in a sheet code module, the sheet CodeName is necessary in front of the macro name ("Sheet1.Macro2")...

Run popup message in protected excel sheet when the value of two cells are equal

I want to confirm with a popup message and sound effect in a protected Excel 2010 worksheet when the values of two cells are the same. I have tried this formula in data validation:
IF(D4=D5,beepnow(),"")
but it does not run. Can anyone assist with the formula or a VBA code replacement instead? Thanks!
Here's a program that will run whenever you change to the worksheet... which may get pretty annoying... You should get the idea though and be able to modify it to fit your needs.
Private Sub Worksheet_Activate()
If Range("D4").Value = Range("D5").Value Then
Beep
MsgBox "Equal", vbInformation, "Check"
End If
End Sub
You should just be able to copy and paste it into your worksheet class.
If you go to the VB COde Window, select the relevant sheet, you can have the following
Private Sub Worksheet_Change(ByVal Target As Range)
If (Range("D4:D4").Cells(1, 1)) = Range("D5:D5").Cells(1, 1)) Then
MsgBox ("Hi")
End If
End Sub
The worksheet activate will run the code when you select this sheet.
The Worksheet_change will run the code when you make a change in this sheet.
IF you want to have the check only when D4/D5 is modified
If Target.Address = "$D$4" Or Target.Address = "$D$5" Then
If (Range("D4:D4").Cells(1, 1)) = Range("D5:D5").Cells(1, 1)) Then
MsgBox ("Hi")
End If
End If

VBA: access sheet from userForm

I am trying to access my workbook and sheets while I am in a subroutine of my UserForm.
My macro starts when the focus in the worksheet is changed:
Sub Worksheet_SelectionChange(ByVal Target As Range)
'...
End Sub
In my UserForm, I now wanted to define what happens when the red x on the top right is pressed. In that case I want to change the focus/selection in Sheet1 when that UserForm is closed. Though, I want to do this relative to the original position, so relative to Target or relative to the ActiveCell.
What works in the routine above is:
ActiveCell.Offset(0, 1).Select
However, I would like to do this in the routine below:
Sub UserForm_Terminate()
Me.Hide
'change focus here
End
End Sub
I was able to select a cell with
Sheet1.Cells(3, 3).Select
but that is obviously not relative to the original position. Neither Target nor ActiveCell... are available in UserForm_Terminate
I hope anyone can help
Vincenz
I'm not sure I fully understand what you're trying to achieve, but can't you simply use:
Application.ActiveCell
in UserForm_Terminate?

Pass the Protection Status of Excel Worksheet to a Cell

I'm curious as to whether it's possible to pass the protection status of an excel worksheet to a cell of that worksheet.
e.g.
Sheet1 is locked for editing...cell A1 would be programmed to say "locked"
Sheet1 is unlocked...cell A1 would say "unlocked".
A button on the sheet would be used to toggle worksheet protection on and off.
My sheet will be locked upon opening using a workbook_open event.
This is for a sheet where I don't want the formulae getting all mucked up upon use, but where full access might be required. Its more as a reminder to the user that they are in "Unlocked" Mode so to be extra careful.
Is using VBA a foregone conclusion?
I'm a VBA noob but don't mind using code as a solution for this
Any thoughts or suggestions welcome
You could use code in an ActiveX button on Sheet1 to do this simply
Const strPAss = "test"
Private Sub CommandButton1_Click()
If ActiveSheet.ProtectContents Then
ActiveSheet.Unprotect strPAss
[a1].Value = "unlocked"
Else
[a1].Value = "locked"
ActiveSheet.Protect strPAss
End If
End Sub
Put this in the worksheet's code module, which will place a reminder in the Status Bar (this avoids needing to lock/unlock the sheet in order to write the status in to cell A1).
Put this in Sheet1 code module. The macro will execute every time sheet1 is activated.
Private Sub Worksheet_Activate()
If ActiveSheet.ProtectContents then
Application.StatusBar = "This sheet is protected"
Else:
Application.StatusBar = "This sheet is unprotected"
End If
End Sub
Private Sub Worksheet_Deactivate()
Application.StatusBar = False
End Sub
To protect/unprotect the worksheet you could add this to an Insert>Module. Then attach these macros to separate command buttons, or run from the Developer>Macros ribbon.
Const myPassword as String = "password" '<-- replace "password" with your password
Sub Sht1Protect()
Sheet1.Protect myPassword
End Sub
Sub Sht1Unprotect()
Sheet1.Unprotect myPassword
End Sub
To ensure the sheet is always protected when you close the file, insert this in the Workbook code module
Private Sub Workbook_Close()
Sht1Protect
End Sub
You may need additional handling to control whether the file is saved/not saved etc.

Resources