Get row number on Cell Change in VBA - vba

I know how to check to see if a column has changed, like so
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("H5")) Is Nothing Then
'Pop up a message saying H5 has changed
End If
End Sub
If I want to change another column on the same row, I can do this
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("H5")) Is Nothing Then
Range("A5").Value = "Look at me!"
End If
End Sub
Now what if I want to achieve the above, but for all columns in the range of row1 to the end of the table? Something like this (note, I know this wouldn't work)
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("H1:H")) Is Nothing Then
Range("A" & Target.row).Value = "Look at me!"
End If
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("H:H")) Is Nothing Then
Range("A" & Target.row).Value = "Look at me!"
End If
End Sub
Though you should note that Target can be a multi-cell range if more than one cell is updated in the same operation.
This may be safer:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
For Each c in Target.Cells
If Not Intersect(c, Range("H:H")) Is Nothing Then
'3 ways to accomplish the update
Range("A" & c.Row).Value = "Look at me!"
c.EntireRow.Cells(1).Value = "Look at me!"
Cells(c.Row, 1).Value = "Look at me!"
End If
Next c
End Sub

Related

Limiting the code to run only when within a specific cell range

I'm trying to make a Spin Button that will edit the active cell, but only when it is within a specific cell range. I want the code to start up if the active cell is within the range of Sheet 1 cells J63:J97 and, not run if it is outside that range.
This is the code I have so far. It will edit the active cell, as needed. However, it is not limited to the range I need it to be.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
SpinButton1.Value = Selection.Value
End Sub
Private Sub SpinButton1_Change()
Selection.Value = SpinButton1.Value
End Sub
Try using Application.Intersect.
I have defined a separate Function to do the job.
This code is tested and it works:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If checkIntersection(Target, Range("J63:J97")) Then
SpinButton1.Value = Selection.Value
End If
End Sub
Private Sub SpinButton1_Change()
If checkIntersection(Selection, Range("J63:J97")) Then
Selection.Value = SpinButton1.Value
End If
End Sub
'Check if Range1 and Range2 are intersecting
Function checkIntersection(range1 As Range, range2 As Range) As Boolean
checkIntersection = Not Application.Intersect(range1, range2) Is Nothing
End Function
may check selection with Intersect
Private Sub SpinButton1_Change()
If Not Intersect(Selection, Range("J63:J97")) Is Nothing Then
Selection.Value = SpinButton1.Value
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("J63:J97")) Is Nothing Then
SpinButton1.Value = Selection.Value
End If
End Sub

Vba code for Date today in data validation

I have a spreadsheet that has data validation in cells A2-A999 with the only option in the drop down menu being "Today" (without the quotation marks). I have a VBA code that changes the cell's value to today's date when "Today" is selected in the cell. However, this code has a problem. When I clear the contents of a group of cells, including the cell that has today's date in it, the spreadsheet thinks, then debugs and then closes; for example clearing A1 & B1 simultaneously.
However, if I clear A1 by itself, it clears the cell with no problems.
P.S. By " I clear", I meant to say: "I select the group of cells with the mouse and then hit the backspace button."
Can you guys help me fix the code so that I can clear many cells at the same time, including the cell with data validation.
The code that I am using is pasted in the worksheet section and is as the following:
Private Sub Worksheet_Change(ByVal Target As Range)
selectedVal = Target.Value
If Target.Column = 1 Then
selectedNum = Application.VLookup(selectedVal, Worksheets("DATA-
O").Range("DateToday"), 2, False)
If Not IsError(selectedNum) Then
Target.Value = selectedNum
End If
End If
End Sub
The answer to your problem is (as Dirk Reichel just mentioned in a comment) to loop through each of the affected cells:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
If Not Intersect(Columns(1), Target) Is Nothing Then
For Each c In Intersect(Columns(1), Target).Cells
selectedVal = c.Value
selectedNum = Application.VLookup(selectedVal, Worksheets("DATA-O").Range("DateToday"), 2, False)
If Not IsError(selectedNum) Then
Application.EnableEvents = False 'As recommended by K Paul
c.Value = selectedNum
Application.EnableEvents = True
End If
Next
End If
End Sub
However, based on what you say that the code is doing, I'm not sure why you don't just use:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
If Not Intersect(Columns(1), Target) Is Nothing Then
For Each c In Intersect(Columns(1), Target).Cells
If c.Value = "Today" Then
Application.EnableEvents = False 'As recommended by K Paul
c.Value = Date
Application.EnableEvents = True
End If
Next
End If
End Sub
If you want to be fast, there are 2 ways.
Use Evaluate to do it array-like:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Columns(1), Target) Is Nothing Then
With Intersect(Columns(1), Target)
If Evaluate("AND(" & .Address & "<>""Today"")") Then Exit Sub
.Value = Evaluate("IF(" & .Address & "=""Today"",TODAY()," & .Address & ")")
End With
End If
End Sub
or use Range.Replace which also can be very fast:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Columns(1), Target) Is Nothing Then
Intersect(Columns(1), Target).Replace "Today", Date, xlWhole, , True, , False, False
End If
End Sub
A small hint: hitting ctrl & ; will directly input todays date

When I delete or clear contents of a cells getting a debug message

I have a VBA code to capitalize most of my worksheet but when I delete or clear the contents I get the debug message.
Any help would be appreciated!
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Not Intersect(Target, Range("A:Z")) Is Nothing Then
Target.Value = UCase(Application.Substitute(Target.Value, " ", ""))
End If
Application.EnableEvents = True
End Sub
If you delete/change/add more than a single cell then Target is more than one cell and you cannot make the value uppercase as you are doing. Loop through the intersection of Target and columns A:Z and make each uppercase.
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Not Intersect(Target, Range("A:Z")) Is Nothing Then
dim rng as range
for each rng in Intersect(Target, Range("A:Z"))
rng.Value = UCase(Replace(rng.Value, chr(32), vbnullstring))
next rng
End If
Application.EnableEvents = True
End Sub

Multiple cell value change to trigger macros

I have different set of values in cells G3:G4 and D15:D10000 in a single sheet. I want run two separate codes when G columns or D columns are changed. How I can identify which set of columns are changed?
out this in your worksheet code pane
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("G3:G4")) Is Nothing Then
'code when some cell in range "G3:G4" is changed
ElseIf Not Intersect(Target, Range("D15:D10000")) Is Nothing Then
'code when some cell in range "D15:D10000" is changed
End If
End Sub
Put the code below in your relevant worksheet, in the Worksheet_Change event.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim WatchRange As Range
Dim IntersectRange As Range
Set WatchRange = Application.Union(Range("G3:G4"), Range("D15:D10000"))
Set IntersectRange = Intersect(Target, WatchRange)
If Not IntersectRange Is Nothing Then
Select Case Target.Column
Case 4 ' column D
Call A
Case 7 ' column G
Call B
End Select
End If
End Sub
Below are examples of Sub A and Sub B:
Sub A()
MsgBox "Running Sub A"
End Sub
Sub B()
MsgBox "Running Sub B"
End Sub

Show value of specific cell in Excel macro

I am new to excel macro.
What I need to to is by clicking on specific cell in A column I will know It`s value. Can you help me with this?
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("D1:D200")) Is Nothing Then
MsgBox Range.("D" & Row_No).Value
End If
End If
End Sub
Use the code below to check the value of any cell in Column A once you click on it, this code should be added to your relevant Sheet to Worksheet_SelectionChange event :
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim WatchRange As Range
Dim IntersectRange As Range
' setting Column A as watched Range
Set WatchRange = Range("A:A")
Set IntersectRange = Intersect(Target, WatchRange)
If Not IntersectRange Is Nothing Then
If Selection.Count = 1 Then
MsgBox Target.Value
Else
MsgBox "You have selected more than 1 cell !"
End If
End If
End Sub