VBA Code to repeat a function in rows 2-100 - excel

I have little experience with VBA code and I am trying to get this code to repeat for rows 2-100. The issue I have found vs other codes that repeat in rows is that mine has multiple end arguments and I'm not sure how to account for this. Any help is greatly appreciated.
Private Sub Worksheet_SelectionChange2(ByVal Target As Range)
a = Date
b = 2
If Cells(b, 3).Value <> Blank Then
If Cells(b, 2).Value = Blank Then
Cells(b, 2).Value = a
Exit Sub
End If
If Cells(b, 2).Value < a Then
Exit Sub
End If
Cells(b, 2).Value = a
End If
End Sub
This is what I'm working with. I tried to make the cell reference a variable that I could count up but whatever I tried it didn't work.
Edit: Sorry for lack of clarification. The code is supposed to put today's date in B2 when C2 goes from being empty to having anything in it. It also prevents the date from being changed if there is already a date there, even if C2 is cleared. I am trying to extend it so that rather than just C2 and B2 it is C2-C100 and then corresponding B2-B100.
Edit 2: C2 is being changed by a manual input. The purpose is to have someone input data into C2 (and the rest of the row) and for the date to be automatically entered and locked so they cannot change it and I can see when the data was inputted.

A Worksheet Change (Timestamp)
This will only work if the values in column C are modified manually i.e. by manually entering, by copy/pasting, and by writing via VBA.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo ClearError
Const fRow As Long = 2
Const sCol As String = "C"
Const dCol As String = "B"
Dim scrg As Range: Set scrg = Columns(sCol).Resize(Rows.Count - fRow + 1)
Dim srg As Range: Set srg = Intersect(scrg, Target)
If srg Is Nothing Then Exit Sub
Dim drg As Range
Dim dCell As Range
Dim sCell As Range
For Each sCell In srg.Cells
Set dCell = sCell.EntireRow.Columns(dCol)
If Len(CStr(dCell.Value)) = 0 Then
If drg Is Nothing Then
Set drg = dCell
Else
Set drg = Union(drg, dCell)
End If
End If
Next sCell
' All cells already contain a date.
If drg Is Nothing Then Exit Sub
Dim dDate As Date: dDate = Now ' after prooving that it works, use 'Date'
' To prevent retriggering this event and any other event while writing.
Application.EnableEvents = False
' Write in one go.
drg.Value = dDate
SafeExit:
' Enable events 'at any cost'.
If Not Application.EnableEvents Then Application.EnableEvents = True
Exit Sub
ClearError:
Debug.Print "Run-time error '" & Err.Number & "': " & Err.Description
Resume SafeExit
End Sub

Related

Sort and copy data based on a date

I'm trying to create a macro that would allow me to extract data from an array to send an email.
The sorting must be done according to the comments. The goal is to detect the date of the day, for example today 22/08/2022, and to extract the line in another page by erasing in the comment box, the comments which are not dated today , ie have the whole line with the last comment in the comment box. On the other hand, if there is no comment dating from today, the line must not be selected or copied.
However, no matter what code I enter, I cannot sort the data according to the date and only retrieve today's comment, knowing that in this excel I only have a few lines but I have to be able to use it for 1000 rows.
How should I go about it?
Thank you and have good day
My example table
The result that I try to have
Solution
Option Explicit
Sub TodaysComments()
Dim srcWs As Worksheet
Dim destWs As Worksheet
Dim myCell As Range
Dim rngToCopy As Range
' Set source and find comments column
Set srcWs = Worksheets("Source")
Set myCell = srcWs.Cells.Find("Commentaires")
If myCell Is Nothing Then
MsgBox "Cannot find column 'Commentaires'!", vbCritical
Exit Sub
End If
' Set and clear destination
Set destWs = Worksheets("Filtered")
destWs.Cells.Clear
' Copy Header
RngCopy CurrentRow(myCell), destWs.Range("A1")
' Loop over comments
NextCell myCell
Do While myCell.Value <> ""
' Search for today's date
If Not myCell.Find(Today) Is Nothing Then
' Aggregate rows to copy
Set rngToCopy = RngUnion(rngToCopy, CurrentRow(myCell))
End If
NextCell myCell
Loop
' No comments today
If rngToCopy Is Nothing Then
MsgBox "No 'Commentaires' rows meet criteria!", vbInformation
Exit Sub
End If
' Copy rows to destination
RngCopy rngToCopy, destWs.Range("A2")
' Clear old comments from destination
Set myCell = destWs.Cells(2, myCell.Column)
Do While myCell.Value <> ""
ClearOldComments myCell
NextCell myCell
Loop
MsgBox "Done!", vbInformation
End Sub
Private Sub RngCopy(SrcRng As Range, DestRng As Range)
SrcRng.Copy
DestRng.PasteSpecial xlPasteAll
DestRng.Range("A1").PasteSpecial xlPasteColumnWidths
Application.CutCopyMode = False
End Sub
Private Function CurrentRow(myCell As Range) As Range
Set CurrentRow = Range(myCell, myCell.Worksheet.Cells(myCell.Row, 1))
End Function
Private Sub NextCell(myCell As Range)
Set myCell = myCell.Offset(1, 0)
End Sub
Function RngUnion(Rng1 As Range, Rng2 As Range) As Range
If Rng2 Is Nothing Then Err.Raise 91 ' Object variable not set
If Rng1 Is Nothing Then
Set RngUnion = Rng2
Exit Function
End If
Set RngUnion = Union(Rng1, Rng2)
End Function
Private Sub ClearOldComments(myCell As Range)
Dim Comments As Variant
Dim i As Long
Comments = VBA.Split(myCell.Value, vbNewLine)
For i = LBound(Comments) To UBound(Comments)
' NOTE: We assume there is only one comment per day.
If InStr(Comments(i), Today) Then
myCell.Value = Comments(i)
Exit Sub
End If
Next
' Should not be possible
Err.Raise 93 ' Invalid pattern string
End Sub
Function Today() As String
Today = FormatDateTime(Date, vbGeneralDate)
End Function

Automatically show date when particular result detected from Excel formula

I am trying with VBA to get the current date on column H (Date).
In column F (Result)
If I manually type Preferred or Non-preferred.
After pressing Enter, today's date will be automatically put on column H (Date)
When I paste formula instead (which will consider data from column A-E to show result on its cell).
Even if the result gives Preferred or Non-preferred the date will not automatically show up.
Unless I press double-click and enter on each result cell then it will show up.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
Dim St As String
St = "Preferred|Non-Preferred"
If Not Intersect(Columns("F"), Target) Is Nothing Then
Application.EnableEvents = False
For Each c In Intersect(Columns("F"), Target).Cells
If InStr(1, St, c.Value, vbTextCompare) >= 1 Then
Cells(c.Row, "H").Value = Date
Else
If IsEmpty(c) Then Cells(c.Row, "H").Value = ""
End If
Next c
Application.EnableEvents = True
End If
End Sub
Example
A Worksheet Change: Using Precedents
In column F is a formula that calculates dependent on the values of columns A:E (precedents). The code will trigger when the values in columns A:E are manually changed, possibly modifying the value in column H (see OP's description (requirements)).
The Select Case statement could be reduced to two cases: either it begins with Criteria (1) or it doesn't (Else).
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo ClearError
Const sFirstCellAddress As String = "F2"
Const dCol As String = "H"
Const Criteria As String = "Preferred"
Dim srg As Range
With Me.Range(sFirstCellAddress)
Set srg = .Resize(Me.Rows.Count - .Row + 1)
End With
Dim irg As Range: Set irg = Intersect(srg.Precedents, Target)
If irg Is Nothing Then Exit Sub
Set srg = Intersect(irg.EntireRow, Columns(srg.Column))
Dim drg As Range: Set drg = srg.EntireRow.Columns(dCol)
Application.EnableEvents = False
Dim dCell As Range
Dim n As Long
For Each dCell In drg.Cells
n = n + 1
Select Case InStr(1, CStr(srg.Cells(n).Value), Criteria, vbTextCompare)
Case 0
dCell.Value = Empty ' criteria not found
Case 1
dCell.Value = Date ' begins with criteria
Case Else
dCell.Value = Empty ' contains criteria
End Select
Next dCell
SafeExit:
If Not Application.EnableEvents Then Application.EnableEvents = True
ProcExit:
Exit Sub
ClearError:
Debug.Print "Run-time error '" & Err.Number & "': " & Err.Description
Resume SafeExit
End Sub

Private sub update date automatically when value in a cell changes

Im trying to automatically update current date in cell T when text in cell Q is "won" and a value in cell AM is > 0. I tried the code below and it is working if first the value in cell is > 0 and then you update the text in cell Q BUT if you do it in another way (first update cell Q and secondly the value in cell AM) the date doesn't appear in cell T.
Any idea, what Im I missing?
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, [Q:Q]) Is Nothing Then
If UCase(Target) = UCase("won") And Target.Offset(, 22) > 0 Then
Target.Offset(, 2) = Int(Now())
End If
End If
End sub
Your code only checks for changes in Q therefore the update does not take place if you change AM first.
My solution has three parts:
use constants for the columns - in case there are changes to the sheet layout you only have to make adjustments here
worksheet_change: only check if one of the columns is affected then call the according sub - by that the reader of the code immediately understands what is going on here
the main routine that inserts the date if condition is met or removes the date if not (maybe you want to adjust this)
Option explicit
Private Const colStatus As String = "Q"
Private Const colValue As String = "AM"
Private Const colDateWon As String = "S"
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
Set c = Target.Cells(1, 1)
If c.Column = Me.Columns(colStatus).Column Or c.Column = Me.Columns(colValue).Column Then
updateDateWon c.row
End If
End Sub
Private Sub updateDateWon(row As Long)
'--> adjust the name of the sub to your needs
Dim valueToInsert As Variant
With Me
If .Range(colStatus & row) = "won" And .Range(colValue & row) > 0 Then
valueToInsert = Int(Now)
Else
'reset the date in case conditions are not met
valueToInsert = vbNullString
End If
Application.EnableEvents = False 'disable events so that change-event isn't called twice
.Range(colDateWon & row) = valueToInsert
Application.EnableEvents = True
End With
End Sub
A Worksheet Change Applied to Two Non-Adjacent Columns
You need to monitor columns Q and AM for changes.
You need to account for Target being multiple adjacent and non-adjacent cells.
You need to disable events when writing to the worksheet containing this code to not retrigger this event (or trigger any other events).
It is good practice to ensure the re-enabling of events (by using error-handling).
You can combine the cells to be written to (dCell) into a range (drg) and write the stamp in one go.
Int(Now()) or Int(Now) is actually Date.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo ClearError
Const sColsAddress As String = "Q:Q,AM:AM"
Const dCol As String = "T"
Const fRow As Long = 2 ' (e.g. 2 for excluding headers in the first row)
Const sCriteria As String = "won"
Dim srg As Range
With Range(sColsAddress)
Set srg = Intersect(.Cells, Rows(fRow).Resize(Rows.Count - fRow + 1))
End With
Dim sirg As Range: Set sirg = Intersect(srg, Target)
If sirg Is Nothing Then Exit Sub
Dim sirg1 As Range: Set sirg1 = Intersect(sirg.EntireRow, srg.Areas(1))
Dim siCol2 As Long: siCol2 = srg.Areas(2).Column
'Dim dirg As Range: Set dirg = sirg1.EntireRow.Columns(dCol) ' not used
Dim siCell1 As Range
Dim siValue2 As Variant
Dim drg As Range
For Each siCell1 In sirg1.Cells
If StrComp(CStr(siCell1.Value), sCriteria, vbTextCompare) = 0 Then
siValue2 = siCell1.EntireRow.Columns(siCol2).Value
If IsNumeric(siValue2) Then
If siValue2 > 0 Then
If drg Is Nothing Then
Set drg = siCell1.EntireRow.Columns(dCol)
Else
Set drg = Union(drg, siCell1.EntireRow.Columns(dCol))
End If
End If
End If
End If
Next siCell1
If Not drg Is Nothing Then
' Prevent retriggering the event when writing to the worksheet.
Application.EnableEvents = False
drg.Value = Now ' only after testing, use 'dDate = Date'
End If
SafeExit:
' Enable events 'at all cost'.
If Not Application.EnableEvents Then Application.EnableEvents = True
Exit Sub
ClearError:
Debug.Print "Run-time error '" & Err.Number & "': " & Err.Description
Resume SafeExit
End Sub

How can I get this Excel VBA to run for all rows, instead of just the first row?

So darn new to VBA, almost got this working. I'm trying to reset some Data Validation lists so that if someone changes a Country selection, then a couple of cells will reset. Example, if I pick USA then I want the corresponding State and Shift column to display "Please select..." and if the user changes the country to something other than USA then it doesn't say anything in the State column, only in the Shift column. I got this working but it only runs for the first row. I'm not sure if my range is wrong or if I'm supposed to loop, both of which I'm totally ignorant on.
Option Explicit
'The way this works is if the Payroll Country changes then the sub selections
'of State and Shift should reset based on if the country is USA
'Payroll Country = A column
'State = X column
'Shift = Y column
Private Sub Worksheet_Change(ByVal Target As Range)
'"If Target.Count > 1 Then Exit Sub" is the VBA code to prevent an error if user highlights the range and deletes the data
If Target.Count > 1 Then Exit Sub
If Target.Address = "$A$6" And Target.Value = "USA" Then
Range("X6").Value = "Please select..."
Range("Z6").Value = "Please select..."
ElseIf Target.Address = "$A$6" And Target.Value <> "USA" Then
Range("X6").Value = ""
Range("Z6").Value = "Please select..."
End If
End Sub
Try this:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub 'CountLarge handles larger ranges...
'check Target column and row...
If Target.Column = 1 and Target.Row >= 6 Then
With Target.EntireRow
.Columns("X").Value = IIf(Target.Value = "USA", _
"Please select...", "")
.Columns("Z").Value = "Please select..."
End With
End If
End Sub
A Worksheet Change
If you out-comment the line Application.EnableEvents = False you will notice that there will be more Debug.Print lines (one more for USA, two more for non-USA) i.e. after writing (crg.Value = InitialString), the code calls itself (actually the worksheet change event calls the InitializeCountry procedure) again but 'luckily' exits after the line If irg Is Nothing Then Exit Sub since it is writing to a non-intersecting range, but nevertheless debug-printing the Source Range Address once more, before it continues with the line Debug.Print "Criteria Range (Both Columns): " & crg.Address(0, 0). This should help you to fully understand why the disabling of events is necessary.
Out-comment or delete the range addresses related Debug.Print lines when done testing because they're slowing down the code.
Sheet Module e.g. Sheet1
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
InitializeCountry Target
End Sub
Standard Module e.g. Module1
Option Explicit
Sub InitializeCountry( _
ByVal Target As Range)
' Declare constants.
Const SourceFirstCellAddress As String = "A6"
Const StateColumn As String = "X"
Const ShiftColumn As String = "Z"
Const CriteriaString As String = "USA"
Const InitialString As String = "Please select..."
' Read.
Dim ws As Worksheet: Set ws = Target.Worksheet
Dim sfCell As Range: Set sfCell = ws.Range(SourceFirstCellAddress)
Dim srg As Range: Set srg = sfCell.Resize(ws.Rows.Count - sfCell.Row + 1)
Debug.Print "Source Range Address: " & srg.Address(0, 0)
Dim irg As Range: Set irg = Intersect(srg, Target) ' Intersect Range
If irg Is Nothing Then Exit Sub
Debug.Print "Intersect Range Address: " & irg.Address(0, 0)
' ShiftColumn
Dim crg As Range ' Criteria Range
Set crg = irg.EntireRow.Columns(ShiftColumn)
Debug.Print "Criteria Range (ShiftColumn): " & crg.Address(0, 0)
' StateColumn
Dim erg As Range ' Empty Range
Dim iCell As Range ' Current Intersect Cell
For Each iCell In irg.Cells
If iCell.Value = CriteriaString Then
Set crg = Union(crg, iCell.EntireRow.Columns(StateColumn))
Else
If erg Is Nothing Then
Set erg = iCell.EntireRow.Columns(StateColumn)
Else
Set erg = Union(erg, iCell.EntireRow.Columns(StateColumn))
End If
End If
Next iCell
' Write.
Application.ScreenUpdating = False
' This is crucial to not retrigger the event procedure when writing!
Application.EnableEvents = False
On Error GoTo ClearError
crg.Value = InitialString
Debug.Print "Criteria Range (Both Columns): " & crg.Address(0, 0)
If Not erg Is Nothing Then
erg.ClearContents ' erg.Value = Empty
Debug.Print "Empty Range (StateColumn): " & erg.Address(0, 0)
End If
SafeExit:
Application.EnableEvents = True
Application.ScreenUpdating = True
Exit Sub
ClearError:
' Don't uncomment this line!
Debug.Print "Run-time error '" & Err.Number & "': " & Err.Description
Resume SafeExit
End Sub
' Test multi-range (only possible with VBA).
Sub InitializeCountryTEST()
Dim rg As Range: Set rg = Range("A6:A20,A31:A50,A61:A100")
rg.Value = "USA"
End Sub
' Debug.Print result for 'USA' (no 'Empty Range'):
'Source Range Address: A6:A1048576
'Intersect Range Address: A6:A20,A31:A50,A61:A100
'Criteria Range(ShiftColumn): Z6:Z20
'Criteria Range (Both Columns): Z6:Z20,X6:X20,X31:X50,X61:X100
' Debug.Print result otherwise:
'Source Range Address: A6:A1048576
'Intersect Range Address: A6:A20,A31:A50,A61:A100
'Criteria Range(ShiftColumn): Z6:Z20
'Criteria Range (Both Columns): Z6:Z20
'Empty Range (StateColumn): X6:X20,X31:X50,X61:X100

VBA clear cell C if I clear cell B

I am new to VBA and have been trying to get this to work for the last few days.
I have 2 columns.
B-student
C-date
What I want is when a student comes in and puts their initials in column B then it fills in the date in column C in that row.
Now if i delete the students initials I want it to clear the C cell also for that row.
Here is my code
Private Sub Worksheet_Change(ByVal Target As Range)
Dim wb As Workbook
Set wb = Workbooks("Training")
Dim ws As Worksheet
Set ws = wb.Worksheets("Sheet1")
Dim StaffRange As Range
Set StaffRange = ws.Range("B5:B40")
Dim StaffTime As Range
' If they put in initials in StaffRange then proceed
If Not Intersect(Target, StaffRange) Is Nothing Then
Set StaffTime = ws.Range("C" & Target.Row)
If StaffTime.Value <> "" Then Exit Sub 'if there is already a date then exit
StaffTime.Value = Now ' put in the date time
'now if they clear StaffRange then clear StaffTime
ElseIf Intersect(Target, StaffRange) Is Nothing Then
Set StaffTime = ws.Range("C" & Target.Row)
StaffTime.ClearContents ' make blank
End If
End Sub
Thank you for any and all help.
To fix your problem, just change references to .Value = "" to .clear.
As well, you need to add a reference to the sheet you are working within, otherwise, your reference to Range can "confuse" the macro.
Explanation
Dim wb As Workbook: Set wb = Workbooks(ThisWorkbook.Name) ' defines the workbook you are working in. You could change "ThisWorkbook" to the actual workbook name, but note that any changes to the workbook name (such as auto recover) will require you to modify this variable.
Dim ws As Worksheet: Set ws = wb.Worksheets("Sheet1") ' defines the worksheet within the workbook defined above.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim StaffRange As Range
Dim StaffTime As Range
Dim TrainerRange As Range
Dim TrainerTime As Range
Dim wb As Workbook: Set wb = Workbooks(ThisWorkbook.Name)
Dim ws As Worksheet: Set ws = wb.Worksheets("Sheet1")
Set StaffRange = ws.Range("B5:B40")
Set TrainerRange = ws.Range("D5:D40")
' If they put in initials in StaffRange then procede with entering the date stamp
If Not Intersect(Target, StaffRange) Is Nothing Then
Set StaffTime = ws.Range("C" & Target.Row)
If StaffTime.Value <> "" Then Exit Sub 'if there is already a date in field do not update and exit
StaffTime.Value = Now ' put in the date time
' now if they clear StaffRange then clear StaffTime
' cell cleared
ElseIf Intersect(Target, StaffRange) Is Nothing Then
Set StaffTime = ws.Range("C" & Target.Row)
' If StaffTime.Value = "" Then Exit Sub ' if it is already clear exit
StaffTime.clear ' make blank
' If they put in initials in TrainerRange then procede with entering the date stamp
ElseIf Not Intersect(Target, TrainerRange) Is Nothing Then
Set TrainerTime = ws.Range("E" & Target.Row)
If TrainerTime.Value <> "" Then Exit Sub
TrainerTime.Value = Now
' now if they clear TrainerRange then clear TrainerTime
' cell cleared
ElseIf Intersect(Target, TrainerRange) Is Nothing Then
clearing
Set StaffTime = ws.Range("E" & Target.Row)
' If StaffTime.Value = "" Then Exit Sub ' if it is already clear exit
StaffTime.clear ' make blank
End If
End Sub
You can do this:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim b As Range, c As Range, rng As Range
'updates in range of interest?
Set rng = Application.Intersect(Me.Range("B5:B40"), Target)
If rng Is Nothing Then Exit Sub 'nothing to process...
For Each b In rng.Cells
Set c = b.Offset(0, 1)
If Len(b.Value) > 0 Then
If Len(c.Value) = 0 Then c.Value = Now 'value entered: add time
Else
c.ClearContents 'value cleared: clear time
End If
Next b
End Sub
I got it to work. Thank you both for your code. I learned from both and came up with this.
If it can be cleaned up I would appreciate any pointers.
Thanks again
Private Sub Worksheet_Change(ByVal Target As Range)
Dim b As Range, c As Range, d As Range, e As Range, rngb As Range, rngd As Range
'updates in range of interest?
Set rngb = Application.Intersect(Me.Range("B5:B40"), Target)
Set rngd = Application.Intersect(Me.Range("D5:D40"), Target)
If Not rngb Is Nothing Then
For Each b In rngb.Cells
Set c = b.Offset(0, 1)
If Len(b.Value) > 0 Then
If Len(c.Value) = 0 Then c.Value = Now 'value entered: add time
Else
c.ClearContents 'value cleared: clear time
End If
Next b
End If
If Not rngd Is Nothing Then
For Each d In rngd.Cells
Set e = d.Offset(0, 1)
If Len(d.Value) > 0 Then
If Len(e.Value) = 0 Then e.Value = Now 'value entered: add time
Else
e.ClearContents 'value cleared: clear time
End If
Next d
End If
End Sub

Resources