Skip to content

Instantly share code, notes, and snippets.

@ashrithr
Last active December 22, 2015 22:49
Show Gist options
  • Save ashrithr/6542340 to your computer and use it in GitHub Desktop.
Save ashrithr/6542340 to your computer and use it in GitHub Desktop.
Excel VBA subroutine to get email columns from tow sheets and dump the difference to another sheet
Function CreateSheetIf(strSheetName As String) As Boolean
'Returns false if does not creates a worksheet
'Returns true if it creates a worksheet
Dim wsTest As Worksheet
CreateSheetIf = False
'Check to see if the worksheet exist, if does not exists wsTest will be "Nothing"
'otherwise wsTest will be assigned to the worksheet
Set wsTest = Nothing
On Error Resume Next
Set wsTest = ActiveWorkbook.Worksheets(strSheetName)
On Error GoTo 0
If wsTest Is Nothing Then
CreateSheetIf = True
Worksheets.Add.Name = strSheetName
End If
End Function
Function IsInArray(arr As Variant, valueToFind As Variant) As Boolean
'Checks if an element(valueToFind) exists in array(arr), if exists returns True
'otherwise returns False
Dim i As Long
For Each Itm In arr
If StrComp(Itm, valueToFind) = 0 Then
IsInArray = True
Exit For
End If
Next Itm
End Function
Function ArrayUDiff(array1 As Variant, array2 As Variant) As Variant
'Takes in two arrays and returns another array which contains the elements from Array1-Array2
Dim tempArray As Variant
Dim i As Long
' start with a single element
ReDim tempArray(0)
' if element in first array does not exist in second array, keep it
For Each Itm In array1
If Not IsInArray(array2, Itm) Then
ReDim Preserve tempArray(UBound(tempArray) + 1)
tempArray(UBound(tempArray)) = Itm
End If
Next Itm
' first element is Empty, so shift all elements one position up
For i = LBound(tempArray) To UBound(tempArray) - 1
tempArray(i) = tempArray(i + 1)
Next i
' remove last element
If UBound(tempArray) <> 0 Then
ReDim Preserve tempArray(LBound(tempArray) To UBound(tempArray) - 1)
End If
ArrayUDiff = tempArray
End Function
Sub Temp()
'declare worksheets to use
Dim w1 As Worksheet, w2 As Worksheet, w3 As Worksheet
Dim i As Long
'Create worksheet "TestSheet" if it doesn't exist.
CreateSheetIf("TestSheet")
'Set the worksheets
Set w1 = Worksheets("RAVE Export List.csv")
Set w2 = Worksheets("rave_people_091013.csv")
Set w3 = Worksheets("TestSheet")
' declare an unallocated array
Dim emailAryW1 As Variant, emailAryW2 As Variant, resultAry As Variant
'get last element from w1 worksheet and w2 worksheet
Dim LastrowW1 As Integer, LastrowW2 As Integer
LastrowW1 = w1.Cells(Rows.Count, 1).End(xlUp).Row
LastrowW2 = w2.Cells(Rows.Count, 1).End(xlUp).Row
'Fill the array strTempAry with emailid(s) from w1 & w2
emailAryW1 = w1.Range("A2:A" & LastrowW1).Value
emailAryW2 = w2.Range("D1:D" & LastrowW2).Value
'Calculate the diff between arrays
resultAry = ArrayUDiff(emailAryW1, emailAryW2)
'Write out the array to range
w3.Range("A1:A" & LastrowW1) = emailAryW1
w3.Range("B1:B" & LastrowW2) = emailAryW2
'Write result output
i = 0
For Each cell In w3.Range("C1:C" & UBound(resultAry) + 1).Cells
cell.Value = resultAry(i)
i = i + 1
Next
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment