Lock rows in Excel using VBA - vba

I have an Excel sheet with columns A to F to be filled out by different users. Once one row is completed another user (control user) enteres "done" in column G. As soon as the user entered "done" in coulmn G, I want a VBA script to lock the entire row (column A to G) so that no one can change any of that row entries any longer. Is that possible using VBA scripting?

We must start with all cells un-protected and the sheet Locked
Enter the following Event macro in the worksheet code area:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim N As Long
N = Target.Row
If Intersect(Target, Range("G:G")) Is Nothing Then Exit Sub
If Target.Text <> "Done" Then Exit Sub
ActiveSheet.Unprotect
Range("A" & N & ":G" & N).Locked = True
ActiveSheet.Protect
End Sub
Because it is worksheet code, it is very easy to install and automatic to use:
right-click the tab name near the bottom of the Excel window
select View Code - this brings up a VBE window
paste the stuff in and close the VBE window
If you have any concerns, first try it on a trial worksheet.
If you save the workbook, the macro will be saved with it.
If you are using a version of Excel later then 2003, you must save
the file as .xlsm rather than .xlsx
To remove the macro:
bring up the VBE windows as above
clear the code out
close the VBE window
To learn more about macros in general, see:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
and
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
To learn more about Event Macros (worksheet code), see:
http://www.mvps.org/dmcritchie/excel/event.htm
Macros must be enabled for this to work!

Related

VBA excel specific cell entry and adding a warning message

I have a worksheet that I am trying to enter new product details in.
One column requires a 5 digit number. What I would like my code to do is once this number is entered if it is not exactly a 5 digit number, send a warning message box asking to check the job number.
The warning message should apply if the entry is <>5 digits and also if it contains letters. However whatever the entry I still want the cell to accept the entry.
In your case I would use Excel Data Validation
Simply change range to be between 9999 and 100000, so it will be a 5 digit number. Or you can use a standard Excel formula to validate the entry.
At the data validation dialog you can also define an alert or error mensage.
For more information: Excel reference on data validation
This is coded for column B.
Place the following in the worksheet code area:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range, r As Range, s As String, L As Long
Dim addy As String
Set rng = Intersect(Range("B:B"), Target)
If rng Is Nothing Then Exit Sub
For Each r In rng
s = r.Text
L = Len(s)
addy = r.Address(0, 0)
If L <> 5 Then
MsgBox "Please check " & addy
Else
For i = 1 To 5
If Not IsNumeric(Mid(s, i, 1)) Then
MsgBox "Please check " & addy
End If
Next i
End If
Next r
End Sub
Because it is worksheet code, it is very easy to install and automatic to use:
right-click the tab name near the bottom of the Excel window
select View Code - this brings up a VBE window
paste the stuff in and close the VBE window
If you have any concerns, first try it on a trial worksheet.
If you save the workbook, the macro will be saved with it.
If you are using a version of Excel later then 2003, you must save
the file as .xlsm rather than .xlsx
To remove the macro:
bring up the VBE windows as above
clear the code out
close the VBE window
To learn more about macros in general, see:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
and
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
To learn more about Event Macros (worksheet code), see:
http://www.mvps.org/dmcritchie/excel/event.htm
Macros must be enabled for this to work!

Secure Timestamp - VBA

I want to create a secure timestamp on my Excel sheet. The VBA I am using will automatically add the current users user name, the time, and the date when a user puts information into column A. So if the users puts something into cell A1 then B1 automatically gets filled with their username and C1 gets filled with the time and date. The only problem is that this method isn’t secure because the user could alter the information after it is automatically populated. I would like to add code to this VBA so it will lock all three cells after the information is populated.
I was planning on using the Protect Sheet feature and only allowing users to “Select unlocked cells” So if the VBA could auto lock the cells then the users would not be able to alter the information.
Moreover I have used Me.Unprotect before changing cells and Me.Protect after that still it is not working
Any help would be much appreciated!
Assume that we start with all the cells on the worksheet unlocked and the sheet password-protected with the password:
6LgSdHjc2uOssv0e1LDI
The following Event Macro will:
unprotect the workbook
detect entries in column A
place the username in column B and the date/timestamp in column C
lock the entries in columns A,B,C
re-protect the worksheet.
This goes in the Worksheet code area:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim A As Range, MyPass As String, sh As Worksheet
Dim unit As Range
Set A = Range("A:A")
MyPass = "6LgSdHjc2uOssv0e1LDI"
Set sh = ActiveSheet
If Intersect(Target, A) Is Nothing Then Exit Sub
Set unit = Union(Target, Target.Offset(0, 1), Target.Offset(0, 2))
Application.EnableEvents = False
sh.Unprotect (MyPass)
unit.Locked = False
Target.Offset(0, 1) = Environ("Username")
Target.Offset(0, 2) = Now()
unit.Locked = True
sh.Protect (MyPass)
Application.EnableEvents = True
End Sub
Because it is worksheet code, it is very easy to install and automatic to use:
right-click the tab name near the bottom of the Excel window
select View Code - this brings up a VBE window
paste the stuff in and close the VBE window
If you have any concerns, first try it on a trial worksheet.
If you save the workbook, the macro will be saved with it.
If you are using a version of Excel later then 2003, you must save
the file as .xlsm rather than .xlsx
To remove the macro:
bring up the VBE windows as above
clear the code out
close the VBE window
To learn more about macros in general, see:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
and
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
To learn more about Event Macros (worksheet code), see:
http://www.mvps.org/dmcritchie/excel/event.htm
Macros must be enabled for this to work!

How to run automatically Excel macros?

I have created a cricket sheet in excel. I have also created a macro which is used to display a message when overs bowled are equal to 20 (A T-20 Match). My problem is that when the over get to 20 no message is shown unless I press the shortcut key. Is there anything which I can do so that whenever overs reach to 20 a message automatically displays.
My code is:
Sub Innings()
If Range("H43") = 20# Then
MsgBox "Innings Completed"
End If
End Sub
Include the following macros in the worksheet code area:
Private Sub Worksheet_Calculate()
Call Innings
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("H43")) Is Nothing Then
Call Innings
End If
End Sub
Because they are worksheet code macros, they are very easy to install and automatic to use:
right-click the tab name near the bottom of the Excel window
select View Code - this brings up a VBE window
paste the stuff in and close the VBE window
If you have any concerns, first try them on a trial worksheet.
If you save the workbook, the macros will be saved with it.
If you are using a version of Excel later then 2003, you must save
the file as .xlsm rather than .xlsx
To remove the macros:
bring up the VBE windows as above
clear the code out
close the VBE window
To learn more about macros in general, see:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
and
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
To learn more about Event Macros (worksheet code), see:
http://www.mvps.org/dmcritchie/excel/event.htm
Macros must be enabled for this to work!
Gary's Student's code is working well. You just need to write Worksheet_Change and Worksheet_Calculate to the List1 not to the Module1. I add a picture.

Excel basic timestamp using VBA

I'm creating a very basic excel workbook for my own personal record, and what I'm wanting to do is having a record timestamp(in column B) whenever I add/edit the value of column A. As I've never dealt with VBA before, but with other programming languages, can someone point me into a right direction to start with?
I'm thinking of opening the macros to write a sub, then using a for loop to screen the whole columns and set the timestamp using TODAY() by seeing which cells in colA have been changed. But I reckon this will be a very inefficient method (as whole worksheet screening is required), I wonder what's the proper way of doing this? Thank you.
Include the following event macro in the worksheet code area:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim A As Range, L As Long, r As Range, rInt As Range
Set A = Range("A:A")
Set rInt = Intersect(A, Target)
If rInt Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each r In rInt
r.Offset(0, 1) = Now()
Next r
Application.EnableEvents = True
End Sub
Because it is worksheet code, it is very easy to install and automatic to use:
right-click the tab name near the bottom of the Excel window
select View Code - this brings up a VBE window
paste the stuff in and close the VBE window
If you have any concerns, first try it on a trial worksheet.
If you save the workbook, the macro will be saved with it.
If you are using a version of Excel later then 2003, you must save
the file as .xlsm rather than .xlsx
To remove the macro:
bring up the VBE windows as above
clear the code out
close the VBE window
To learn more about macros in general, see:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
and
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
To learn more about Event Macros (worksheet code), see:
http://www.mvps.org/dmcritchie/excel/event.htm
Macros must be enabled for this to work!

vba convert formula into value if value is >0 in specific range

A question: I am trying to convert a formula in a specific range into its value once the value of that formula is more than 0 in vba. I have found some results for converting an entire workbook to just it's value, but nothing more detailed than that.
Sub ConvertCellsToValues()
ActiveSheet.Cells.Copy
ActiveSheet.Cells.PasteSpecial Paste:=xlPasteValues
End Sub
Say the range in question is A1 thru A10.
Place the following event macro in the worksheet code area:
Private Sub Worksheet_Calculate()
Dim A As Range, r As Range
Set A = Range("A1:A10")
For Each r In A
If r.HasFormula And r.Value > 0 Then
r.Value = r.Value
End If
Next r
End Sub
Because it is worksheet code, it is very easy to install and automatic to use:
right-click the tab name near the bottom of the Excel window
select View Code - this brings up a VBE window
paste the stuff in and close the VBE window
If you have any concerns, first try it on a trial worksheet.
If you save the workbook, the macro will be saved with it.
If you are using a version of Excel later then 2003, you must save
the file as .xlsm rather than .xlsx
To remove the macro:
bring up the VBE windows as above
clear the code out
close the VBE window
To learn more about macros in general, see:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
and
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
To learn more about Event Macros (worksheet code), see:
http://www.mvps.org/dmcritchie/excel/event.htm
Macros must be enabled for this to work!
The .Copy and .PasteSpecial work for any range:
Sub ConvertCellsToValues(Param as Range)
Param.Copy
Param.PasteSpecial Paste:=xlPasteValues
End Sub