Avoiding "On Error Resume Next" if possible

TheWennerWoman

Active Member
Joined
Aug 1, 2019
Messages
309
Office Version
  1. 365
Platform
  1. Windows
I am using a function to validate email addresses on a sheet. Most of the time there is just the one email address in a cell but there are occasions when there are two separated by a semi-colon
Code:
donald.duck@disneyworld.com; mickey.mouse@disneyworld.com

So before passing the email addresses to the function, I am splitting like this
VBA Code:
For a = 2 To lrow2
'On Error Resume Next
emailtoparse = Sheet6.Range("B" & a).Value
result2 = Split(emailtoparse, ";")
firstemail = result2(0)
secondemail = LTrim(result2(1))

However, I am getting a "subscript out of range" error on the variable secondemail whenever there's just a single email address (I assume because result2(1) doesn't exist in those cases).

I can fudge my way through with an On Error Resume Next but I don't like doing that because it'a apparently bad practice :-)

Is there another way I can try?

Thank you for reading.
 
Does this give you any ideas ?
VBA Code:
Sub testSplit()

    Dim emailtoparse As String
    Dim result2 As Variant
    Dim j As Long
    
    emailtoparse = "donald.duck@disneyworld.com"
    result2 = Split(emailtoparse, ";")
    
    For j = 0 To UBound(result2)
        ' do what you need to do with each address
        Debug.Print Trim(result2(j))
    Next j
    
End Sub
 
Upvote 0
Solution
Hi, just for demonstration purposes, here's another way you could approach it.

VBA Code:
Sub testSplit()

    Dim emailtoparse As String
    Dim result2 As Variant
    
    emailtoparse = "donald.duck@disneyworld.com"
    
    For Each result2 In Split(emailtoparse, ";")
        ' do what you need to do with each address
        Debug.Print Trim(result2)
    Next result2
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,226,840
Messages
6,193,278
Members
453,788
Latest member
drcharle

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top