Skip to content

Instantly share code, notes, and snippets.

@joseph4tw
joseph4tw / HowLongAgo.vba
Last active March 28, 2018 11:16
Figures out how long ago something happened
Option Explicit
' Summary: Returns a friendly timestamp like "Just now" or "45 minutes ago"
' Param cell: The cell used for the function.
' Should only be a single cell and should be a date with or without time.
Public Function HowLongAgo(ByRef cell As Range) As String
' Only allow dates and only allow single cells
If ((Not IsDate(cell)) Or cell.Cells.Count > 1) Then
HowLongAgo = vbNullString
Exit Function
Option Explicit
Public CopyNewDataRange As Range
Public Sub CopyNewData()
Dim copyTo As Range
Set copyTo = Sheets("Sheet2").Range(CopyNewDataRange.Address)
CopyNewDataRange.Copy copyTo
End Sub
Option Explicit
Private Sub Worksheet_Change(ByVal target As Range)
Dim sh As Worksheet
Set sh = ActiveSheet
Dim newdata As Range
Set newdata = Application.Intersect(target, sh.Range("A2", "A" & sh.Rows.Count))
If (Not newdata Is Nothing) Then
Option Explicit
Public CopyNewDataRange As Range
Public Sub CopyNewData()
Dim copyFrom As Range
Dim copyTo As Range
Set copyFrom = Sheets("Sheet1").Range("A2")
Set copyTo = Sheets("Sheet2").Range("A2")
Option Explicit
Private Sub Worksheet_Change(ByVal target As Range)
Dim sh As Worksheet
Set sh = ActiveSheet
Dim newdata As Range
Set newdata = Application.Intersect(target, sh.Range("A2", "A" & sh.Rows.Count))
If (Not newdata Is Nothing) Then
Option Explicit
Public Sub CopyNewData()
Dim copyFrom As Range
Dim copyTo As Range
Set copyFrom = Sheets("Sheet1").Range("A2")
Set copyTo = Sheets("Sheet2").Range("A2")
copyFrom.Copy copyTo
Option Explicit
Public Sub WriteHello()
Dim cell As Range
Set cell = Selection
cell.Value = "Hello, cell!"
End Sub
Option Explicit
Public Sub DoWork()
Call PrintNumber
End Sub
Private Sub PrintNumber(Optional ByRef number As Variant)
If (IsMissing(number)) Then
Debug.Print "number was not passed"
Else
Option Explicit
Public Sub DoWork()
Call PrintNumber
End Sub
Private Sub PrintNumber(Optional ByVal number As Long = 10)
Debug.Print number
End Sub
Option Explicit
Public Sub DoWork()
Call PrintNumber
End Sub
Private Sub PrintNumber(Optional ByVal number As Long)
Debug.Print number
End Sub