Prevent row from being changed after initial write - excel

I want to be able to set values in a cell but prevent the cell from being changed easily afterwards.
The cell will store a random int and so if the user edits the cell and presses enter a new random int is generated. These id's are mapped to unit tests so updates are undesirable.
How could I solve this please?

Here is a simple example for a single cell, cell B9.
It assumes that we start with all cells UNLOCKED
Enter the following event macro in the worksheet code area:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim B9 As Range
Set B9 = Range("B9")
If B9.Value = "" Then Exit Sub
If Intersect(Target, B9) Is Nothing Then Exit Sub
B9.Locked = True
ActiveSheet.Protect Password:="secret"
End Sub
Once a value in entered in B9, that cell is locked and the worksheet is protected with the password secret.
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

Drop-down list of hyperlinks to different sheets in Excel

I have 5 different sheets: S1, S2, S3, S4, S5. And an "Info" sheet where i have the overall information, where in every row I want a dropdown along with direct link to one of the sheets(S1, S2, S3, S4, S5). I tried using Indirect function along with Address but it is not dynamic. Is there a way of how to make a drop-down list with hyperlink to the sheet directly inbuilt with that?
I hope i could describe my situation.
No need to re-invent the wheel. This is already built into Excel. Righ-click the sheet navigation arrows in the bottom left hand corner and the list of sheets will pop up. Click the sheet you want to go to. Hidden sheets (like Sheet3 in my screenshot) will no be listed.
Place a drop-down in cell, say, A5.
In B5 enter:
=HYPERLINK("#" & A5 & "!A1",A5)
This will create a "hot" hyperlink to cell A1 of the sheet you picked in the drop-down:
EDIT#1:
Place your drop-downs in column A. Each drop-down can select any worksheet. Then in the worksheet code area enter:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim A As Range, sht As String, sh As Worksheet
Set A = Range("A:A")
If Intersect(Target, A) Is Nothing Then Exit Sub
sht = Target.Value
For Each sh In Sheets
If sh.Name = sht Then
sh.Activate
End If
Next sh
End Sub
whenever you change a value in column A, the code checks it the new value is a valid sheet-name. If it is, the code jumps to that sheet. If it is not a valid name, nothing bad happens.
This approach has the advantage that if you add/remove worksheets, no code change is required.
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!

Automatically update a cell in Excel with the current date anytime any cell in a column is updated or changed

Not sure if this is possible in Excel, but I would like cell K9 to be updated with today's date every time ANY cell in K11:K119 is changed. Some cells in K11:K119 are blank, but can have data entered into them at any time. Cells in K11:K119 that already have data could change. So I would like cell K9 to update with today's date any time any single blank cell in K11:K119 is updated with data, or any time any single cell in K11:K119 with data is changed. Hope that makes sense.
Place the following Event macro in the worksheet code area:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r1 As Range, r2 As Range
Set r1 = Range("K9")
Set r2 = Range("K11:K119")
If Not Intersect(Target, r2) Is Nothing Then
Application.EnableEvents = False
r1.Value = Date
Application.EnableEvents = True
End If
End Sub
The macro monitors your changes to the worksheet. If you change any value in the input range, the cell with the date is refreshed.
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!
If you don't want to enable macros, try something simpler:
=IF(COUNT(K11:K119)>=0,TODAY(),0)
COUNT(range) will always be ≥0 (so it doesn't matter what you put as the third argument) and it always updates when any member of the range is changed.

excel macro open on row specified in cell

I've specified in cell "B1" where my sheet needs to be opened on (=MATCH(TODAY(),A:A,0)).
And I want to automatically open my excel sheet on that row.
(example: row 10 = today)
or can I use the function "=today()" in VBA ?
I'm kind of a noob in VBA so can you please assist me ?
Place the following Event Macro in the worksheet code area:
Private Sub Worksheet_Activate()
Cells(Range("B1").Value, "A").Select
End Sub
When the worksheet is opened, the proper cell will automatically be Selected:
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!
EDIT#1:
To get the same behavior when the file is opened, leave the previous macro in place and put this macro in the workbook code area:
Private Sub Workbook_Open()
Application.EnableEvents = False
Sheets("Sheet1").Select
Cells(Range("B1").Value, "A").Select
Application.EnableEvents = True
End Sub
(you would replace Sheet1 with the name of your worksheet)

Delete cell content after enter key?

Excel I need it to delete what I type right after I press enter is this possible? This might be easy but I don't know how to.
Yes I am using this data I need it to send the data and then delete itself with the results from that data in.
This is what I have so far: "=COUNTIF(B2;C8)" so if the b2 matches c8 then it adds one and I need it to delete the b2 automatically so that I could enter new value.
Go to File > Options. This will open a window, select the second tab Formulas. Check the box Enable Iterative Calculation.
Note, that this box will uncheck itself if you install updates.
Ta da!!!
Place the following event macro in the worksheet code area:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
Target.ClearContents
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!

Excel VBA if formula value in cell is less than zero then msgbox

Im trying to get a msgbox when the value in the cell which is updated with a formula is less than zero.
For example:
a1= 5
b5= a1
if b5 is less than zero then msgbox "your value is less than zero"
Hope someone can help me
Thank you!
Include the following event macro in the worksheet code area:
Private Sub Worksheet_Calculate()
If [B5] < 0 Then
MsgBox "your value is less than zero"
End If
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!

Resources