Remember the previous value of formulas - excel

After a deep research on the internet I managed to find a VBA code that allows me to remember the previous result of a formula. I would like to modify this code to obtain the previous value of the formulas in one column in another column next to it.
For example: if '' B2: B80 "contains formulas, I would like" D2: D80 "to show the previous value of those formulas.
The code that I show does not keep the previous values ​​in a single cell but continuously populates a column down and my goal is to obtain the previous value of each formula in a single cell, but of several cells of a column.
Dim xVal As String
Private Sub Worksheet_Change(ByVal Target As Range)
Static xCount As Integer
Application.EnableEvents = False
If Target.Address = Range("C2").Address Then
Range("D2").Offset(xCount, 0).Value = xVal
xCount = xCount + 1
Else
If xVal <> Range("C2").Value Then
Range("D2").Offset(xCount, 0).Value = xVal
xCount = xCount + 1
End If
End If
Application.EnableEvents = True
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
xVal = Range("C2").Value
End Sub

Please try this simple code. I think it will do what you want.
Sub CopyValues()
With Worksheets("Sheet1") ' enter your tab's name here
.Range("B2:B80").Copy
.Cells(2, "D").PasteSpecial xlValues
End With
Application.CutCopyMode = False
End Sub

i use something similar to track changes on another sheet. Maybe this will help?
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim sSheetName As String
sSheetName = "Data"
If ActiveSheet.Name <> "LogDetails" Then
Application.EnableEvents = False
Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Value = ActiveSheet.Name & " - " & Target.Address(0, 0)
Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 1).Value = oldValue
Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 2).Value = Target.Value
Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 3).Value = Environ("username")
Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 4).Value = Now
Sheets("LogDetails").Columns("A:E").AutoFit
Application.EnableEvents = True
End If
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
On Error GoTo ErrHandler:
n = 1 / 0
Debug.Print n
oldValue = Target.Value
oldAddress = Target.Address
Exit Sub
ErrHandler:
n = 1
' go back to the line following the error
Resume Next
oldValue = Target.Value
oldAddress = Target.Address
End Sub
This tracks each change made in all sheets bar the LogDetails so would record all your changes.
I believe if you add the last sub into yours and change the reference it should work.

Related

How to combine multiple worksheet Change events Excel VBA

I need to combine the following 3 subroutines into a single worksheet change event but I am unsure how.
I have tried writing one sub in the worksheet editor and another in the workbook editor. However given that I have 3 subroutines all referring to the same worksheet, I am unsure how to combine them. Any help is greatly appreciated!
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("D3:D100")) Is Nothing Then
Exit Sub
Else
Dim i As Integer
For i = 3 To 100
If Range("D" & i).Value = "Remote" Then
Range("O" & i).Value = "N/A"
Range("P" & i).Value = "N/A"
Range("Q" & i).Value = "N/A"
End If
Next i
End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target,Range("H3:H100")) Is Nothing Then
Exit Sub
Else
Dim e As Integer
For e = 3 To 100
If Range("H" & e).Value = 1 Then
Range("I" & e).Value = "N/A"
End If
Next e
End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target,Range("I3:I100")) Is Nothing Then
Exit Sub
Else
Dim e As Integer
For e = 3 To 100
If Range("I" & e).Value = 1 Then
Range("H" & e).Value = "N/A"
End If
Next e
End If
End Sub
Flip the logic.
If Intersect(Target, Range("D3:D100")) Is Nothing Then
Exit Sub
Else
...
End If
Change this to
If Not Intersect(Target, Range("D3:D100")) Is Nothing Then
' Remove Exit Sub
' Remove Else
...
End If
Do the same for the two other Intersect calls and then combine everything into one Worksheet_Change handler.
Most likely you want to disable events as well, to avoid re-triggering the event when writing to the sheet:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo SafeExit
Application.EnableEvents = False
' Your three Intersect checks
SafeExit:
Application.EnableEvents = True
End Sub
try this. put this in the worksheet, not the workbook
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Integer
If Not Intersect(Target, Range("D3:D100")) Is Nothing Then
c = 1
Else
If Not Intersect(Target, Range("H3:H100")) Is Nothing Then
c = 2
Else
If Not Intersect(Target, Range("I3:I100")) Is Nothing Then
c = 3
End If
End If
End If
Select Case c
Case 1
' your stuff
Case 2
'your stuff
Case 3
'your stuff
Case Else
End Select
End Sub

Excel VBA delete row dont execute vba script

Good day, i got a set of code as below:
Dim oval
Public Sub Worksheet_SelectionChange(ByVal Target As Range)
oval = Target.Value
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("C2:C1048576")) Is Nothing Then
Range("G" & Target.Row) = Now()
Range("H" & Target.Row) = oval
End If
End Sub
This is supposed to update column "G" with current date and time while column "H" with previous content in "C" should a change detected in column "C".
While this code is working, i got an issue whereby deleting a row of data in the middle of the table will shift the lower row up to the deleted row but it will update the column "G" and "H" as well.
is there a way to prevent that? thank you.
This will solve some of your problems:
Dim oval
Public Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
oval = Target.Value
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("C2:C1048576")) Is Nothing Then
Application.EnableEvents = False
Range("G" & Target.Row) = Now()
Range("H" & Target.Row) = oval
Application.EnableEvents = True
End If
End Sub
I would get rid of the public variable and Worksheet_SelectionChange event macro. Application.Undo can be applied within the Worksheet_Change event macro after recording the changed values.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("C:C")) Is Nothing Then
On Error GoTo bm_Safe_Exit
Application.EnableEvents = False
Dim rng As Range, vCEEs As Variant
vCEEs = Range(Cells(1, "C"), Cells(Rows.Count, "C").End(xlUp)).Value
Application.Undo
For Each rng In Intersect(Target, Range("C:C"))
Range("G" & rng.Row) = Now()
Range("H" & rng.Row) = Range("C" & rng.Row)
Range("C" & rng.Row) = vCEEs(rng.Row, 1)
Next rng
End If
bm_Safe_Exit:
Application.EnableEvents = True
End Sub
This should survive pasting multiple values into column C but you risk losing values pasted into other columns.

Cancel macro 1 if macro 2 is running

I have two macros altogether, one macro assigned to my Private Worksheet_Change event and the other assigned to my Private Worksheet_SelectionChange event like so:
Macro 1
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'If Hours Column Selected
If Not Intersect(Target, Range("Z" & ActiveCell.Row)) Is Nothing And Range("Z" & ActiveCell.Row).Value <> "" Then
NewValue = Application.InputBox("Please Enter Your Delegated Reference:")
If NewValue <> vbNullString Then
Dim rw2 As Long, cell2 As Range
rw2 = ActiveCell.Row
With Worksheets("Data").Columns("I:I")
Set cell2 = .find(What:=NewValue, LookIn:=xlFormulas, _
LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
If Not cell2 Is Nothing Then
Application.DisplayAlerts = False
cell2.Offset(0, 4).Value = Sheet1.Range("Y" & ActiveCell.Row).Value
cell2.Offset(0, 5).Value = Sheet1.Range("H" & ActiveCell.Row).Value
cell2.Offset(0, 6).Value = Sheet1.Range("I" & ActiveCell.Row).Value
MsgBox "Found"
Sheet1.Range("Y" & ActiveCell.Row).Value = cell2.Offset(0, 1).Value
Sheet1.Range("H" & ActiveCell.Row).Value = cell2.Offset(0, 2).Value
Sheet1.Range("I" & ActiveCell.Row).Value = cell2.Offset(0, 3).Value
Application.DisplayAlerts = True
Else
MsgBox "Not Found"
Sheet1.Range("A5").Select
End If
End With
Else
If NewValue = vbNullString Then
MsgBox "Not Found"
Sheet1.Range("A5").Select
End If
End If
End If
End Sub
Macro 2
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("Y" & ActiveCell.Row)) Is Nothing Then
myValue3 = MsgBox("This is a message")
End If
End Sub
The problem I have is when I click on my active cell row in column Z I am running macro 1 and asking it to update the value of my active cell row in column Y. However when the information in column Y is updated, it is causing macro 2 to run where I get a msgbox displaying and I don't want this to happen.
Whilst I still require macro 2 and do want the msgbox to display, I only want it to display when I click on the cell in column Y. So in other words, I want to be able to cancel out macro 2 if macro 1 is running.
I have tried using application.displayevents = false in macro 1 but this doesn't work.
Please can someone show me the best way to do this?
you can use
Application.EnableEvents = False
.. at the start of your macro1 to disable events and
Application.EnableEvents = True
To turn it back on again at the end of macro1.
Please try the following:
[1] Add a new Module to your VBA Project, with the following:
Public EventRunning as Boolean
[2] Modify your Worksheet_SelectionChange macro as follows:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
EventRunning=True
...
EventRunning=False
End Sub
[3] Modify your Worksheet_Change as follows:
Private Sub Worksheet_Change(ByVal Target As Range)
If EventRunning Then Exit Sub
...
Best wishes Ben

how do I do this in Excel. I used this to work for column A but I also want to do additional columns

How do I do this in Excel? I used this to work for column A but I also want to do additional columns
Private Sub Worksheet_Change(ByVal Target As Range)
Dim T As Range, r As Range
Set T = Intersect(Target, Range("A:A"))
If T Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each r In T
With r
.Offset(0, 1).Value = .Offset(0, 1).Value + .Value
.ClearContents
End With
Next r
Application.EnableEvents = True
End Sub
Try this:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim T As Range, r As Range
Dim columnArray() As String, columnsToCopy As String
Dim i As Integer
columnsToCopy = InputBox("What columns (A,B,C, etc.) would you like to copy the data of? Use SPACES, to separate columns")
columnArray() = Split(columnsToCopy)
For i = LBound(columnArray) To UBound(columnArray)
Set T = Intersect(Target, Range("" & columnArray(i) & ":" & columnArray(i) & "")) 'Columns(columnArray(i)) & ":" & Columns(columnArray(i))))
If T Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each r In T
With r
.Offset(0, 1).Value = .Offset(0, 1).Value + .Value
.ClearContents
End With
Next r
Next i
Application.EnableEvents = True
End Sub
That will create a popup, asking you for the columns you want to run this on. What's the thought behind running this every time a cell changes? That's going to be a lot of pop-ups, etc. But let me know if this doesn't work or has some error.

Number format of a cell based on info given on an adjacent cell (VBA)

Trying to set the number format of a cell based on the currency selected on an adjacent cell using VBA. See sample below.
So far I am using the below code but I cannot seem to make the format to appear properly
Option Explicit
Public preValue As Variant
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
Dim Rng As Range
Dim ccy As String
ccy = Range("A3").Value
pctFormat = "0.000%"
fxFormat = ccy + " " + pctFormat
If Target.Cells.Count > 1 Then Exit Sub
On Error Resume Next
If Not Intersect(Target, Range("A2:A10")) Is Nothing Then
If Target.Value <> preValue And Target.Value <> "" Then
Application.EnableEvents = False
Cells(Target.Row, Target.Column + 1).NumberFormat = fxFormat
Application.EnableEvents = True
End If
End If
On Error GoTo 0
End Sub
you could try this option to set your fxFormat variable:
fxFormat = "[$" & ccy & "] 0.000%"
perhaps:
fxFormat = chr(34) & ccy & chr(34) & " " & pctFormat

Resources