Oberon70
Board Regular
- Joined
- Jan 21, 2022
- Messages
- 160
- Office Version
- 365
- Platform
- Windows
Hi, I have the below code, I am trying to accomplish to more things.
1. I need to offset LastCell to the cell above.
2. I don't want a date entered if the cell is note blank.
1. I need to offset LastCell to the cell above.
2. I don't want a date entered if the cell is note blank.
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Application.ScreenUpdating = False
Dim rng As Range
Dim Lastcell As String
Lastcell = FindLast(3)
Set rng = Range("A2:" & (Lastcell.Offset(1, 0)))
If Not Intersect(Target, rng) Is Nothing Then
ActiveSheet.Cells.Interior.ColorIndex = xlColorIndexNone
Target.Interior.ColorIndex = 24
End If
If Not Intersect(Target, rng) Is Nothing Then
If Target.Column = 5 Then
Application.EnableEvents = False
Cells(Target.Row, Target.Column) = Date
Application.EnableEvents = True
End If
End If
If Not Intersect(Target, rng) Is Nothing Then
If Target.Column = 7 Then
Application.EnableEvents = False
Cells(Target.Row, Target.Column) = Date
Application.EnableEvents = True
End If
End If
End Sub
Function FindLast(lRowColCell As Long, _
Optional sSheet As String, _
Optional sRange As String)
'Find the last row, column, or cell using the Range.Find method
'lRowColCell: 1=Row, 2=Col, 3=Cell
Dim lrow As Long
Dim lCol As Long
Dim wsFind As Worksheet
Dim rFind As Range
'Default to ActiveSheet if none specified
On Error GoTo ErrExit
If sSheet = "" Then
Set wsFind = ActiveSheet
Else
Set wsFind = Worksheets(sSheet)
End If
'Default to all cells if range no specified
If sRange = "" Then
Set rFind = wsFind.Cells
Else
Set rFind = wsFind.Range(sRange)
End If
On Error GoTo 0
Select Case lRowColCell
Case 1 'Find last row
On Error Resume Next
FindLast = rFind.Find(What:="*", _
After:=rFind.Cells(1), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
Case 2 'Find last column
On Error Resume Next
FindLast = rFind.Find(What:="*", _
After:=rFind.Cells(1), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0
Case 3 'Find last cell by finding last row & col
On Error Resume Next
lrow = rFind.Find(What:="*", _
After:=rFind.Cells(1), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
On Error Resume Next
lCol = rFind.Find(What:="*", _
After:=rFind.Cells(1), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0
On Error Resume Next
FindLast = wsFind.Cells(lrow, lCol).Address(False, False)
'If lRow or lCol = 0 then entire sheet is blank, return "A1"
If Err.Number > 0 Then
FindLast = rFind.Cells(1).Address(False, False)
Err.Clear
End If
On Error GoTo 0
End Select
Exit Function
ErrExit:
MsgBox "Error setting the worksheet or range."
End Function