I'm using the code below to loop through a array(populated from a range) and then add the value of the cell to another array, then I paste the new array into a table in Excel. It's working fine but it also pastes all the empty array values into the table as well, this is a problem as I have a drop down list using that table and it contains lots of empty values at the end.
Is there a way I can either remove the empty values from the array or only paste the values which aren't empty?
Sub newFilterStaff()
Dim sourceData, targetData() As Variant
Dim sourceRange, targetRange, rng As Range
Dim sheet As Worksheet
Dim i, staffCount As Integer
Dim time As Long
Dim name As String
time = GetTickCount
'Set default values
staffCount = 0
Set sheet = Worksheets("wfm_staff")
Set sourceRange = sheet.[C1:C100]
sourceData = sourceRange.Value
'sheet.Range("E2:E50").Clear 'Clear previous list
For i = LBound(sourceData, 1) To UBound(sourceData, 1)
If sourceData(i, 1) <> "XXXX" And i <> 1 Then 'Remove header row
Set rng = sheet.Range("A" & i)
With rng
name = .Value
End With
ReDim Preserve targetData(0 To staffCount) 'Add name to array
targetData(staffCount) = name
staffCount = staffCount + 1
End If
Next
Range("E2:E" & UBound(targetData) + 1) = WorksheetFunction.Transpose(targetData)
Debug.Print GetTickCount - time, , "ms"
End Sub