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
Related
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().
I have this code
Private Sub Worksheet-Change(ByVal Target As Range)
If Not Intersect(Target, Range(“M4”)) Is Nothing Then
Range("N4:T4).ClearContents
End If
End Sub
Which works for the 4th row When I change M4 it clears N4 to T4
I need a way to adapt this code so that if I change the value of M5 it deletes N5 to T5 and so on for all the rows.
Can you help
You need to intersect the range you want to observe for changes Me.Range("M4:M10") with Target and then use the Range.Offset property and Range.Resize property to move from the changed cell 1 right and resize it to 7 columns to clear contents.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim AffectedRange As Range
Set AffectedRange = Intersect(Target, Me.Range("M4:M10")) 'range to observe
If Not AffectedRange Is Nothing Then
Dim Cell As Range
For Each Cell In AffectedRange.Cells
Cell.Offset(ColumnOffset:=1).Resize(ColumnSize:=7).ClearContents
'or
'Me.Range("N" & Cell.Row & ":T" & Cell.Row).ClearContents
Next Cell
End If
End Sub
Test Target for if it contains a cell in M, and use its row to clear the relevant cells
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cl as Range
For Each cl In Intersect(Target, Me.Columns(13))
If cl.Row >= 4 Then
cl.EntireRow.Cells(1, 14).Resize(1, 7).ClearContents
End If
Next
End Sub
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 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.
I am using following macro to show value of clicked cell (range A5:A200) in A1 cell:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("A5:A200")) Is Nothing Then
Range("A1").Value = Selection.Value
End If
End If
End Sub
...and it works great. Additionally in A2 I'd like to show related value from C column.
Example:
I click on A10 -> and see the value in A1, then in A2 i'd like to see the value from C10 cell
You can utilize the .row property of Selection. But, you already have Target being used from the arugument within the Worksheet_Selection() event as your "Selection" range, and using that range is preferred over using selection.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Count = 1 Then
If Not Intersect(Target, Range("A5:A200")) Is Nothing Then
Range("A1").Value = Target.Value
Range("A2").Value = Cells(Target.row, "C")
End If
End If
End Sub
So replace your selection ranges with Target, and use the row property within Cell() to get your value from the C column.