Custom currency format based on another cell value - vba

In cell A1 i might have one of the following currencies "EUR", "USD" or "RON".
In cell B1 i have the following custom cell format: "EUR" * 0.00"/mt"
Can anyone help in telling me how can i set the format in cell B1 to adapt taking into account the value from A1. The code must run only at workbook.open
Thank you in advance!

Place the following Event Macro in the worksheet code area:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim A1 As Range, B1 As Range, BaseFormat As String
Dim temp As String
Set A1 = Range("A1")
Set B1 = Range("B1")
BaseFormat = """EUR"" * 0.00""/mt"""
If Intersect(A1, Target) Is Nothing Then Exit Sub
temp = A1.Value
B1.NumberFormat = Replace(BaseFormat, "EUR", temp)
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!
(to run this only at workbook open, have the Workbook Open macro re-assert the value in A1 and then disable events)

Related

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

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!

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!

Lock rows in Excel using 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!

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