I need the active cell to have automatically the actual year as soon as a cell in the range A2:A20 is selected. My VBA code changes all the cells at once.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Integer
For i = 2 To 20
If ActiveCell = Cells(i, "A") And Cells(i, "A") = "" Then
Cells(i, "A").Value = Year(Now)
End If
Next i
Range("A:A").EntireColumn.AutoFit
End Sub
Since you want that A2:A20 to have the current year, automatically when the cell is selected, you need to use the Worksheet_SelectionChange event. Then, in that event, you simply test if the cell that was selected is within the A2:A20 range and set the value to the current year if it is.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Me.Range("A2:A20"),Target) Is Nothing Then
Target.Value = Year(Now())
End If
Range("A:A").EntireColumn.AutoFit
End Sub
Related
Whenever I change a value (choose some value from data validation list) in column G, it should clear the cell in the next column H.
So when I choose value in G4, value in H4 will be deleted. When I choose value in G5, tha same would happen with H5.
I tried this, but not working:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 7 Then
For i = 4 To 154 Step 1
If Target.Address = "$G$i" Then
Range("Hi").Select
Selection.ClearContents
End If
Next i
End If
End Sub
No need of iteration for such a task. Since, the cell value is changed by selecting from a drop down validation list, multiple changes are not possible. For such a case, the code simple exists:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 7 Then
If Target.cells.count > 1 Then Exit Sub
Target.Offset(0, 1).ClearContents
End If
End Sub
This can be done like this:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim oCell As Range
For Each oCell In Target.Cells ' You can change many cells at once (i.e. copy-paste or select-delete)
If oCell.Column = 7 Then ' Is it cell in G:G or any other?
If oCell.Text <> vbNullString Then ' Has cell any value?
oCell.Offset(0, 1).ClearContents ' Clear cell in the next column in this row
End If
End If
Next oCell
End Sub
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 need to create an Excel document with updating totals in the A column, based on numbers entered in the B column. Each respective cell in row A should update based on its equivalent B cell value whenever a new value is added, and then the value entered into B is cleared once added to A.
I have gotten things working for one single row but don't have knowledge or understanding on how to best make this work for EACH cell pair in the entire column. I really don't want to copy and paste this 10,000 times and update the cells to reference the correct pair. Code for single cell:
Private bIgnoreEvent As Boolean
Private Sub Worksheet_Change(ByVal Target As Range)
If bIgnoreEvent Then Exit Sub
bIgnoreEvent = True
Cells(1, 2) = Cells(1, 2) + Cells(1, 1)
Cells(1, 1) = ""
bIgnoreEvent = False
End Sub
I am hoping this can be achieved with a loop function, or a range of some sort.
This should work:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range, c As Range
'any updates in ColB?
Set rng = Application.Intersect(Target, Me.Columns(2))
If Not rng Is Nothing Then
Application.EnableEvents = False '<< prevent re-triggering of event
'Process each updated cell
For Each c In rng
If IsNumeric(c.Value) Then
With c.Offset(0, -1)
.Value = .Value + c.Value
End With
c.ClearContents
End If
Next c
Application.EnableEvents = True '<< re-enable event
End If
End Sub
I have a cell in Excel that gets updated on basis of RAND() function.
Each time I update the cell, I want to capture the previous values in a separate column. So that I can compute mean later on.
How to achieve this??
Copy below into Worksheet module and change "A1" cell:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not (Intersect(Target, Range("A1")) Is Nothing) Then
i = i + 1
Worksheets("Sheet1").Cells(i, 2).Value = Worksheets("Sheet1").Cells(1, 1).Value
End If
End Sub
I have a Dynamic Table that changes based on the value of B1.
I want the value of B1 to change by clicking on another cell from another workbook. I'm super new to VBA and don't really know what I'm doing.
What I want is to click on any cell from column E and it will change the value of B1 to equal value of column O.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("E2:E")) Is Nothing Then
Worksheets("Dynamic Table").Range("B1").Value = ActiveCell.Offest(0, 10)
End If
End If
End Sub
My Amateur Code
Do you mean sth like that
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Column = 5 Then
If Target.Cells.CountLarge = 1 Then
Worksheets("Dynamic Table").Range("B1").Value = Target.Offset(, 10).Value
End If
End If
End Sub
I assumed you mean with another workbook just another worksheet.