Execute a function only if a specific cell contains a specific value - excel

The below code works just great.
Now I only want it to function if cell B2 says "2020".
If cell B2 says "2021", for example, I want the value instead to go to sheet "2021".
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim xRtn As Variant
If Selection.Count = 1 Then
If Not Intersect(Target, Range("D9:AS20")) Is Nothing Then
xRtn = Application.InputBox("Insert your value please")
Sheets("2020").Range(Target.Address).Value = xRtn
End If
End If
End Sub
How can I achieve that?

solved myself
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim xRta As Variant
If Selection.Count = 1 Then
If ActiveSheet.Range("B2") = "2020" Then
If Not Intersect(Target, Range("D9:AS20")) Is Nothing Then
xRta = Application.InputBox("Insert your value please")
Sheets("2020").Range(Target.Address).Value = xRta
End If
End If
End If
Dim xRtb As Variant
If Selection.Count = 1 Then
If ActiveSheet.Range("B2") = "2021" Then
If Not Intersect(Target, Range("D9:AS20")) Is Nothing Then
xRtb = Application.InputBox("Insert your value please")
Sheets("2021").Range(Target.Address).Value = xRtb
End If
End If
End If
End Sub

Related

VBA - multiple Worksheet Changes

How do I do this change for 3 pairs of separate cells. I know this 1st code works for 1 pair of cells by putting it on two diff worksheets
Private Sub Worksheet_Change(ByVal Target As Range)
Dim p1 As Range, p2 As Range
Set p1 = Range("L268")
Set p2 = Sheets("Calculator").Range("J2")
If Intersect(Target, p1) Is Nothing Then Exit Sub
Application.EnableEvents = False
p2.Value = p1.Value
Application.EnableEvents = True
End Sub
and then this on the other
Private Sub Worksheet_Change(ByVal Target As Range)
Dim p1 As Range, p2 As Range
Set p1 = Range("J2")
Set p2 = Sheets("Proposal Summary").Range("L268")
If Intersect(Target, p1) Is Nothing Then Exit Sub
Application.EnableEvents = False
p2.Value = p1.Value
Application.EnableEvents = True
End Sub`
but how do I add in two other pairs of cells I also want to equal each other?
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
Dim p1 As Range, p2 As Range
Dim a1 As Range, a2 As Range
Dim h1 As Range, h2 As Range
Set p1 = Range("J2")
Set p2 = Sheets("Proposal Summary").Range("L268")
Set a1 = Range("J3")
Set a2 = Sheets("Proposal Summary").Range("L271")
Set h1 = Range("J4")
Set h2 = Sheets("Proposal Summary").Range("L274")
If Intersect(Target, p1) Is Nothing Then Exit Sub
p2.Value = p1.Value
If Intersect(Target, a1) Is Nothing Then Exit Sub
a2.Value = a1.Value
If Intersect(Target, h1) Is Nothing Then Exit Sub
h2.Value = h1.Value
Application.EnableEvents = True
End Sub
THANK YOU!!!
If I've read the ranges involved correctly you should be able to combine the whole thing like this.
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Not Intersect(Target, Range("J2:J4")) Is Nothing Then
Sheets("Proposal Summary").Range("L268").Offset((Target.Row - 2) * 3).Value = Target.Value
End If
Application.EnableEvents = True
End Sub
If you want this to work the other way, i.e. update the other sheet when a value changes on Proposal Summary you could use this.
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Not Intersect(Target, Range("L268, L271, L274")) Is Nothing Then
Sheets("Calculator").Range("J2").Offset((Target.Row - 268) / 3).Value = Target.Value
End If
Application.EnableEvents = True
End Sub

click cell in range on sheet 1, input box popup, input value into that cell on sheet 2

When i click cell B1, an input box pops up, i put my value in and that value is then entered into cell B1. How do i make it so any cell within a range results in the same input box, and that value is inputted into the cell i clicked in that range, but on a different sheet.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim xRtn As Variant
If Selection.Count = 1 Then
If Not Intersect(Target, Range("B1:C2")) Is Nothing Then
xRtn = Application.Inputbox("Insert your value please")
If xRtn <> False Then Target.Value = xRtn
End If
End If
End Sub
Example: Click cell B1 in sheet 1, input value 5, 5 is entered into cell B1 in sheet 2
Edit: i now have it inputting my value into sheet 2 but i have no idea how to make it input my value into only the specific cell i clicked, currently it inputs my value into all cells within the specified range in sheet 2
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim xRtn As Variant
If Selection.Count = 1 Then
If Not Intersect(Target, Range("B1:C2")) Is Nothing Then
xRtn = Application.Inputbox("Insert your value please")
Sheets("Sheet2").Range("B1:C2").Value = xRtn
End If
End If
End Sub
Edit 2: solved it on my own having absolutely no clue what i was doing never touching code or vba before just trying random stuff
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim xRtn As Variant
If Selection.Count = 1 Then
If Not Intersect(Target, Range("B1:C2")) Is Nothing Then
xRtn = Application.Inputbox("Insert your value please")
Sheets("Sheet2").Range(Target.Address).Value = xRtn
End If
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim xRtn As Variant
If Selection.Count = 1 Then
If Not Intersect(Target, Range("B1:C2")) Is Nothing Then
xRtn = Application.Inputbox("Insert your value please")
Sheets("Sheet2").Range(Target.Address).Value = xRtn
End If
End If
End Sub

Clear the cell of column B if cell A is empty - RANGE

Got a problem and looking for some advice. I've been using the below code for a while now in Excel, it clears the contents of column B if cell A is empty. It works great, but I now need it to work for a specific range (A6:B35). Any ideas?
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Target.Column = 1 Then
If Target.Validation.Type = 3 Then
Application.EnableEvents = False
Target.Offset(0, 1).ClearContents
End If
End If
exitHandler:
Application.EnableEvents = True
Exit Sub
End Sub
You need to test if the active cell (target) falls in the range A6:A35. Like this:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If not intersect(target, range("A6:A35")) is nothing then
If Target.Validation.Type = 3 Then
Application.EnableEvents = False
Target.Offset(0, 1).ClearContents
Application.EnableEvents = True
End If
End if
exitHandler:
End Sub
You should also indent your code so it is more readable. It will help with loops and IF statements.
something like
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng1 As Range
Dim rng2 As Range
Set rng1 = Intersect(Target, Range("A6:B35"))
If rng1 Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each rng2 In rng1
If rng2.Validation.Type = 3 Then rng2.Offset(0, 1).ClearContents
Next
Application.EnableEvents = True
End Sub

Calling one function from the other function in VBA (Excel)

I have two VBA functions, but i am unable to call the other from the first function.
Function 1:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim lastRow As Long
With ActiveSheet
lastRow = .Cells(.Rows.count, "A").End(xlUp).Row
Dim I, J As Integer
For I = 1 To lastRow
If Cells(I, "C").Value = "" Then
MsgBox "Please Enter Business Type Value", vbOKOnly
Exit Sub
End If
Next I
End With
End Sub
And the 2nd function:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
Application.EnableEvents = False
If Not Application.Intersect(Target, Me.Columns(3)) Is Nothing Then
ActiveSheet.Unprotect
Select Case Target.Value
Case Is = "CNS"
Target.Offset(0, 4).Locked = True
Case Is = "cns"
Target.Offset(0, 4).Locked = True
Case Is = "APL"
Target.Offset(0, 4).Locked = False
Case Is = "apl"
Target.Offset(0, 4).Locked = False
Case Else
MsgBox "Value not covered by the program", vbInformation + vbOKOnly
End Select
ActiveSheet.Protect
Else
End If
Application.EnableEvents = True
End Sub
please help somebody..
thanks in advance..
In the same module you just call nameoffunction
You can make function public
public sub function
But it's a poor (sometime good) solution. You should structure your code
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
call modul1.function1 ( Target ) ' As Range)
End sub
Private Sub Worksheet_Change(ByVal Target As Range)
call modul1.function1 ( Target ) ' as range
call modul1.function2 ( Target )
end sub
edit ok ugly way
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
....
call Worksheet_change ( Target)
End sub

How can we find the last item entered within a column

How can we find the last item entered within a column?(note that the last entered item may be A4, while we have data till A1000)
Thanks
If you need the value of the last item entered, then include this event macro in the worksheet code area:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim CellsToWatch As Range, LastValue As Range
Set CellsToWatch = Range("A1:A1000")
Set LastValue = Range("B1")
If Target.Count > 1 Then Exit Sub
If Intersect(Target, CellsToWatch) Is Nothing Then Exit Sub
Application.EnableEvents = False
LastValue.Value = Target.Value
Application.EnableEvents = True
End Sub
If you need the location of the last item entered, then use this:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim CellsToWatch As Range, LastValue As Range
Set CellsToWatch = Range("A1:A1000")
Set LastValue = Range("B1")
If Target.Count > 1 Then Exit Sub
If Intersect(Target, CellsToWatch) Is Nothing Then Exit Sub
Application.EnableEvents = False
LastValue.Value = Target.Address
Application.EnableEvents = True
End Sub
The result will be stored in cell B1
I would create a helper column. This would be a date stamp that is generated using VBA. You can hide we'll just call it column B.
this will go under worksheet change event
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 Then
Application.EnableEvents = False
Me.Cells(Target.Row, 2) = Format(Date + Time, "mm/dd/yyyy h:nn:ss")
Application.EnableEvents = True
End If End Sub
Please note that in Me.Cells(Target.Row,2) the 2 is going to change according to which column you want your date in.
this will go in a separate Module:
Sub get_LastEntered()
Dim ws As Worksheet: Set ws = ActiveSheet
Dim last_Time As Date
Dim last_Row As Long
Dim last_Row_Changed As Long
last_Row = ws.Cells(Rows.Count, 2).End(xlUp).Row
last_Time = Application.WorksheetFunction.Max(ws.Range("B1:B" & last_Row))
last_Row_Changed = Application.WorksheetFunction.Match(last_Time, ws.Range("B1:B" & last_Row),0)
MsgBox "The last Cell that you changed was:" & last_Row_Changed
End Sub

Resources