I've been working Worksheet_Change VBA code in Excel as shown below.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A2:A2000")) Is Nothing Then
Call writetag
End If
End Sub
Sub writetag()
ActiveCell.Select
ActiveCell.Offset(0, 1).Select
ActiveCell.Formula = "1st Request"
End Sub
The writetag VBA code alone does its job just fine wherein it would move 1 cell to the right of the active cell and write "1st Request". Then I have the first VBA code - Worksheet_Change - that will trigger the writetag code whenever there are changes made on range A2:A2000.
But it is at this part that the writetag code does not work perfectly. Instead of moving 1 cell to the right, it would move 1 cell to the right and another 1 cell below. So I need to adjust ActiveCell.Offset(0, 1).Select to ActiveCell.Offset(-1, 1).Select just so that it would move to the right cell.
Then after that, I would like to make 3 conditions or Ifs, wherein when I put 1 anywhere on the A2:A2000 range, it will put "1st Request" to its right. If I put 2 anywhere on the range, it will put "2nd Request" to its right, "3rd Request" if I put 3.
Thank you so much for the help.
Remember target is the address of the called cell, in your case suppose you entered in cell A1:
target = [A1] but the problem is that this event fires after the value is changed so after press enter ActiveCell = [A2], then the event is called and the result is
ActiveCell.offset (0,1) = [A2] .offset (0,1) = [B2]
so your code is not working, let's try:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A2:A2000")) Is Nothing Then
Call writetag(Target)
End If
End Sub
Sub writetag(rng As Range)
With rng.Offset(0, 1)
Select Case rng.Value2
Case 1
.Formula = "1st Request"
Case 2
.Formula = "2nd Request"
Case 3
.Formula = "3rd Request"
End Select
End With
End Sub
Use following sub when you enter value and press ENTER from keyboard.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A2:A2000")) Is Nothing Then
Call writetag
End If
End Sub
Sub writetag()
Dim curr As Variant
curr = ActiveCell.Offset(-1, 0)
ActiveCell.Offset(-1, 1) = curr & "st Request"
End Sub
Related
I want to use a radio button to add or subtract from the on hands in the same row. I couldn't figure out how to tie it down to a cell.
I decided to just fill E2-E450 and F2-F450 with a "+" or "-" sign respectively.
I want when you click E2 it adds + 1 to D2 or when you click F3 it subtracts one from D3 and so on and so forth.
Sub MyMacro()
Range("D2").Select
ActiveCell.FormulaR1C1 = Range("D2") + 1
End Sub
Sub MyMacro2()
Range("D2").Select
ActiveCell.FormulaR1C1 = Range("D2") - 1
End Sub
For the sheet this is what I have
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("E2")) Is Nothing Then
Call MyMacro
End If
End If
If Selection.Count = 1 Then
If Not Intersect(Target, Range("F2")) Is Nothing Then
Call MyMacro2
End If
End If
End Sub
Each button is assigned to the OptionButtonClick procedure below, which is stored in a normal module. Clicking the button will return the address of the TopLeftCell that the button is sitting in.
Application.Caller will return the name of the shape that was clicked.
Public Sub OptionButtonClick()
MsgBox ActiveSheet.Shapes(Application.Caller).TopLeftCell.Address
End Sub
I would give a more detailed answer, but I've no idea what the "on hands" are.
I'm trying to create a cell in Excel that resets every time I put a value into it and every value that I put in the cell is stored and added together.
So basically cell A1 would be empty and then I add a value 30, for example. The cell would then store that value and reset to receive more inputs. I then go ahead and put another value in cell A1, 20. The cell should once again reset, but the value stored in cell A1 would now be equal to 50.
I'm very new to VBA so I'm still trying to figure everything out. I tried using some code I found in another post, but was not able to make it work so I was wondering if anyone had any idea on how to proceed with this problem.
This is the code I found and wasn't able to make it work. It was supposed to receive a value in cell A1 and store the same in cell A2, and once you add a new value to A1, it adds it to the previous value in A2.
Private Sub Worksheet_Change(ByVal Target As Range)
If Cells(1, 1).Value <> gdDouble Then
gdDouble = Cells(1, 1).Value
Cells(2, 1).Value = Cells(2, 1).Value + Cells(1, 1).Value
End If
End Sub
Private Sub Workbook_Open()
gdDouble = Sheets("sheet1").Cells(1, 1).Value
End Sub
And in the standard module:
dim gdDouble as double
Thank you
Adjust the code in the worksheet_change event like that
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("A1")) Is Nothing Then Exit Sub
'If Target.CountLarge > 1 Then Exit Sub
On Error GoTo EH
Application.EnableEvents = False
Target.Value = Target.Value + gdDouble
gdDouble = Target.Value
EH:
Application.EnableEvents = True
End Sub
And change gdDouble to a public variable
Public gdDouble As Double
I am currently using the following VBA to run a macro when a value chosen from a dropdown changes, and the code works fine:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("D5")) Is Nothing Then
Select Case Range("D5")
Case "2008": Macro1
Case "2015": Macro1
End Select
End If
End Sub
However I would like to run the following event when another cell changes (also a drop down), the code is written to hide columns, this is the snippet of the additional code:
Sub hideColumnsBasedOnConditionZero()
LastColumn = 11 'Last Column
For i = 1 To LastColumn 'Lopping through each Column
'Hide all the columns with the values as 0 in Row 11
If Cells(1, i) = 0 And Cells(1, i) <> "" Then Columns(i).EntireColumn.Hidden = True
Next
End Sub
Can someone please tell me how to achieve this? The second code is valid but I cannot activate it as the first code is using the change function and is specific to another cell.
You can just add it to your first event
Private Sub Worksheet_Change(ByVal Target As Range)
Dim LastColumn As Long
With Me
If Not Intersect(Target, .Range("D5")) Is Nothing Then
Select Case .Range("D5")
Case "2008", "2015": Macro1
End Select
ElseIf Not Intersect(Target, .Range("Your Other Range")) Is Nothing Then
Call hideColumnsBasedOnConditionZero
End If
End With
End Sub
What I am needing: A macro to be triggered, let's call the macro "MacroRuns", whenever cell C3 returns a different value than it currently has, based on its FORMULA, NOT based on manually typing a different value.
I have spent all day reading through and attempting every "solution" on the first two pages of my google search on this topic. So far, nothing seems to work for me. Please help!!! I would very much appreciate it!
Example:
I have now tried this but it corrupts my file after it works a few times.
Private Sub Worksheet_Calculate()
If Range("E3") <> Range("C3").Value Then
Range("E3") = Range("B3").Value
MsgBox "Successful"
End If
End Sub
Module1, Sheet1 (Calculate), ThisWorkbook (Open)
Highlights
When the workbook opens, the value from C3 is read into the public
variable TargetValue via TargetStart.
When the value in C3 is being calculated, TargetCalc is activated
via the calculate event.If the current value in C3 is different than TargetValue, MacroRuns is triggered and TargetValue is updated with the value in C3.
The Code
Module1
Option Explicit
Public TargetValue As Variant
Private Const cTarget As String = "C3"
Sub TargetCalc(ws as Worksheet)
If ws.Range(cTarget) <> TargetValue Then
MacroRuns
TargetValue = ws.Range(cTarget).Value
End If
End Sub
Sub TargetStart()
TargetValue = Sheet1.Range(cTarget).Value
End Sub
Sub MacroRuns()
MsgBox "MacroRuns"
End Sub
ThisWorkbook
Option Explicit
Private Sub Workbook_Open()
TargetStart
End Sub
Sheet1
Option Explicit
Private Sub Worksheet_Calculate()
TargetCalc Me
End Sub
Right. I have a nugget to add in here, something that completely frustrated me upon trying Ferdinando's code (which by itself is very neat, thank you, Ferdinando!!)
The main point is - if you are going to be using anything beyond just a messagebox (MsgBox "Cell has changed.") you need to add the following lines above AND below this line(otherwise the Excel will simply crash constantly due to endlessly trying to do the same). Don't ask me why this is, but I finally-finally solved my problem with this. So here are the lines:
If Value1 <> Value2 Then
(ADD THIS:) Application.EnableEvents = False
MsgBox "Cell has changed."
(I call a macro running a query from MySQL instead of MsgBox)
(AND ADD THIS:) Application.EnableEvents = True
Hope this helps anyone in the situation I was in!!
If i understood your question you can try this code:
1)Right-click the Sheet tab and then click View Code
copy this code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Value1 As Variant
Static Value2 As Variant
Value1 = Range("C3").value
If Value1 <> Value2 Then
MsgBox "Cell has changed."
End If
Value2 = Range("C3").value
End Sub
i tried this one:
in cell C3 i have wrote =SUM(A1:B1)
when i try to change value in this cells also C3 change and i get the msgBox
Hope this helps
EDIT the code to answer # MD Ismail Hosen
if i understood your problem you can try this example code:
Private Sub Worksheet_Change(ByVal Target As Range)
'in this code i have used two range on the same row, but you can change as
'you want.
'In my case, the range that i check is Range("A1:C1") and the RANGE that i 'save old value is
'RANGE("F1:H1") F1 is the sixth column.
Dim counter As Byte
Dim sizeRange As Byte
sizeRange = 3 ' my size range
For counter = 1 To sizeRange
'on the left i check Range("A1:C1").On the right i check The Range("F1:H1")
If Cells(1, counter) <> Cells(1, counter + 5) Then 'counter start from 1
MsgBox "Range Changed"
Range("A1:C1").Copy Destination:=Range("F1:H1") ' use other code to copy the range
Exit For
End If
Next counter
End Sub
If you have a formula in your range ("A1:C1") you have to use this code to copy the new range value A1:C1 in F1:H1 else you get the error(loop the macro).
'TO use this code if you have formula in the cells.
Range("A1:C1").Select
Selection.Copy
Range("F1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Hope this helps.
I want an if clause to be true if the ActiveCell was "A1" before I clicked on "A2".
Its not connected with a button or anything. It has to work by just clicking on "A2".
That's my try i made:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address(0, 0) = "A2" And ActiveCell.Cells(0, 0) = "A1" Then
MsgBox ("test")
End If
End Sub
Does someone have a solution?
I think you are using the wrong approach here.
If you use the SelectionChange event then it means the selected cell already changed. You have to stock the address of the last cell every time the event is called. To do the trick, put your condition before stocking the ActiveCell as the lastCellAddress and it works perfectly.
Here is the code with comments to help you understand:
'Declare the variable out of the event
Dim lastCellAddress As String
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Check if the last selected cell was A1 when selecting A2
If (ActiveCell.Address = "$A$2" And lastCellAddress = "$A$1") Then
'If the condition is true display your MsgBox
MsgBox ("test")
End If
'Set the current address as the last selected for next selection change
lastCellAddress = ActiveCell.Address
End Sub