I am trying to clear the contents of some cells if the value in a corresponding cell changes.
In the code below, if the value in O1 changes, the contents of Z1 to AD1, and P1 are cleared.
How can I apply this to all rows below, i.e. if O2 changes, the contents of Z2 to AD2, and P2 should be cleared, the same thing for row 3 and so on.
I'd like if new columns/rows are inserted in the worksheet, things won't break.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "O1" Then
Range("Z1:AD1").ClearContents
Range("P1").ClearContents
End If
End Sub
This should get you started
Only looks for changes that occur in Column O
Only triggers if ONE cell is changed at a time.
(you will need to switch to loop if many cells in Column O can be changed in one action such as pasting a range)
Be sure to turn events OFF before making any other changes to worksheet else you will end up in infinite loop leading to your instance of excel crashing
Make dynamic by clearing cells that are on the same row as the changed cell i.e. Target.Row
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("O:O")) Is Nothing Then
If Target.Count = 1 Then
Application.EnableEvents = False
Range(Cells(Target.Row, "Z"), Cells(Target.Row, "AD")).ClearContents
Cells(Target.Row, "P").ClearContents
Application.EnableEvents = True
End If
End If
End Sub
This should work.
Private Sub Worksheet_Change(ByVal Target As Range)
tCol = Target.Column
tRow = Target.Row
If tCol = 15 Then
For i = tRow To tRow + Target.Rows.Count - 1
Range(Cells(i, 26), Cells(i, 30)).ClearContents
Cells(i, 16).ClearContents
Next
End If
End Sub
Use Target.Column and Target.Row to get the number of the targets column and row which can then be used in Cells().
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
Selection based on a range of values in each row
To: Stack overflow
1) I wish to draft a code that allows a value to be selected based on a range of values in each row as indicated in the picture.
My preliminary code below is:-
Private SubWorksheet_SelectionChange(ByVal Target As Range)
If Target.Cells.CountLarge <> 1 Then Exit Sub
If Not Intersect(Target, Me.Range("F7,H7,J7")) Is Nothing Then Me.Range("C7").Value = Target.Value
End If
If Not Intersect(Target, Me.Range("F8, H8, J8")) Is Nothing Then Me.Range("C8").Value = Target.Value End If
If Not Intersect(Target, Me.Range("F9, H9, J9, L9")) Is Nothing Then Me.Range("C9").Value = Target.Value
End If
End Sub
2) Because I have more than 100 rows of selections to be input, the said code on the above will be tediously wordy.
3) I would appreciate it if you could advise me how to refine and make it beautiful. Thank you very much.
From LC Tan 2020-01-16
If I understand correctly, whenever column F, H or J is selected, you want the value of the selected cell to be copied to column C on the same row.
This can be done in a number of ways, but here is a simple example that tests if the selected column is between 6 and 15 (F and O, can be changed), and then tests if the value in the target is a number (the count). It then copies the current cell to column C on the same row as the selected cell.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Cells.CountLarge <> 1 Then Exit Sub
If Target.Column < 6 Or Target.Column > 15 Then Exit Sub
If Not IsNumeric(Target) Then Range("C" & Target.Row).Value = Target.Value
End Sub
There are a fair few other ways to do it, but this should get you well on your way.
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 have a table of values that I need to fill out through a worksheet change function.
What I am trying to do is change a cell in columns B-G, depending on where the target is.
Private Sub Worksheet_Change(ByVal Target As Range)
If (Not Intersect(Target, Range(Cells(12, 2), Cells(14, 7))) Is Nothing) Then
Cells(16,Application.WorksheetFunction.Column(Target))="Hello"
End If
End Sub
I have similar bits of code in the same worksheet_change sub that work fine when I use Target.Offset(1,0) but since my possible target range is in more than 1 Row, I don't know how to make it so that it is always row 16 and the same column as the target....
You need to deal with situations where Target is more than a single cell and disable event handling so when you change a value on the worksheet, the Worksheet_Change doesn't try to run on top of itself.
This will put 'hello' into the cell immediately to the right of any cell within B:G that changes; essentially you would be adding 'hello' to columns C:H on the associated row of each cell in Target.
Private Sub Worksheet_Change(ByVal Target As Range)
if not intersect(target, Range(Cells(12, "B"), Cells(14, "G"))) is nothing then
on error goto safe_exit
application.enableevents = false
dim t as range
for each t in intersect(target, Range(Cells(12, "B"), Cells(14, "G")))
t.Offset(1,0) = "hello"
next t
End If
safe_exit:
application.enableevents = true
End Sub
I have 2 columns in an Excel sheet. A1 contains a drop down with the values "Enable" and "Disable". Cell B1 and C1 needs to be enabled or disabled based on the A1 drop down selection.
I tried using Data Validation but it does not work. Can anyone suggest how I might use Data Validation to accomplish this?
A | B
------------------------
1 Suggestions| (This cell should disable (B1))
------------------------
2 Errors| (Now here drop down will come with values(B2))
------------------------
Current VBA
Private Sub Worksheet_Change(ByVal Target As Range)
ThisRow = Target.Row
If Target = Range("A1") Then
If Target.Value = "Suggestions" Then
Worksheets("Code Review").Range("B:C").Locked = True
End If
Else
Worksheets("Code Review").Range("B:C").Locked = False
End If
End Sub
Also tried like this
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cel As Range
If Not Intersect(Range("A:A"), Target) Is Nothing Then
Application.EnableEvents = False
Me.Unprotect
For Each cel In Intersect(Range("A:A"), Target)
cel.Offset(ColumnOffset:=1).Resize(ColumnSize:=2).Locked = _
cel.Value = "Suggestion"
Next cel
Me.Protect
Application.EnableEvents = True
End If
But this locks everything :( I want just 2 cells to be locked.