I get a type mismatch error in VBA, and I'm not sure why.
The purpose of this macro is to go through a column in an Excel spreadsheet and add all the emails to the array. After each email is added to the first array, it must also be added to the second array, but split in two by the @ symbol to separate the name from the domain. For example: person@gmail.com to person and gmail.com .
The problem I get is that when it comes to the point where the email is supposed to be split, it throws a type mismatch error.
In particular, this part:
strDomain = Split(strText, "@")
Here is the complete code:
Sub addContactListEmails() Dim strEmailList() As String 'Array of emails Dim blDimensioned As Boolean 'Is the array dimensioned? Dim strText As String 'To temporarily hold names Dim lngPosition As Long 'Counting Dim strDomainList() As String Dim strDomain As String Dim dlDimensioned As Boolean Dim strEmailDomain As String Dim i As Integer Dim countRows As Long 'countRows = Columns("E:E").SpecialCells(xlVisible).Rows.Count countRows = Range("E:E").CurrentRegion.Rows.Count MsgBox "The number of rows is " & countRows 'The array has not yet been dimensioned: blDimensioned = False Dim counter As Long Do While counter < countRows counter = counter + 1 ' Set the string to the content of the cell strText = Cells(counter, 5).Value If strText <> "" Then 'Has the array been dimensioned? If blDimensioned = True Then 'Yes, so extend the array one element large than its current upper bound. 'Without the "Preserve" keyword below, the previous elements in our array would be erased with the resizing ReDim Preserve strEmailList(0 To UBound(strEmailList) + 1) As String Else 'No, so dimension it and flag it as dimensioned. ReDim strEmailList(0 To 0) As String blDimensioned = True End If 'Add the email to the last element in the array. strEmailList(UBound(strEmailList)) = strText 'Also add the email to the separation array strDomain = Split(strText, "@") If strDomain <> "" Then If dlDimensioned = True Then ReDim Preserve strDomainList(0 To UBound(strDomainList) + 1) As String Else ReDim strDomainList(0 To 0) As String dlDimensioned = True End If strDomainList(UBound(strDomainList)) = strDomain End If End If Loop 'Display email addresses, TESTING ONLY! For lngPosition = LBound(strEmailList) To UBound(strEmailList) MsgBox strEmailList(lngPosition) Next lngPosition For i = LBound(strDomainList) To UBound(strDomainList) MsgBox strDomainList(strDomain) Next 'Erase array 'Erase strEmailList End Sub
source share