VBA: If…Then…Else Statements Issue

zero269

Active Member
Joined
Jan 16, 2023
Messages
253
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm having some trouble getting an If…Then…Else Statement to work using multiple ElseIf conditions.
Currently, it will only take action on the first condition and none of the others.
VBA Testing.xlsm
A
1Digital Library
2hoopladigital
3openlibrary
4lacountylibrary
5GlendaleLAC
If-Then-Else

I commented out the Else portion as according to M$, it's optional: If...Then...Else statement (VBA)

VBA Code:
Public Sub dLibrary2()

    'Digital Library Column
    Dim Scheme As String, Domain As String, CloudDomain As String
    Scheme = "https://"
    Domain = ActiveCell.Value
    CloudDomain = "https://ebook.yourcloudlibrary.com/library/"
    
    'Validate Selection
    If Len(Domain) > 0 Then
        If Domain = "hoopladigital" Then ActiveCell.Hyperlinks.Add ActiveCell, Scheme & Domain & ".com", , , Domain
        ElseIf Domain = "openlibrary" Then ActiveCell.Hyperlinks.Add ActiveCell, Scheme & Domain & ".org", , , Domain
        ElseIf Domain = LCase(Domain) Then ActiveCell.Hyperlinks.Add ActiveCell, Scheme & Domain & ".overdrive.com", , , Domain
        ElseIf Len(Domain) > 0 Then ActiveCell.Hyperlinks.Add ActiveCell, CloudDomain & Domain, , , Domain
        'Else

    End If

End Sub
Any help would be greatly appreciated...

 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Recommend you restructure to make your code easier to follow and debug.
VBA Code:
Public Sub dLibrary2()
   
    'Digital Library Column
    Dim Scheme As String, Domain As String, CloudDomain As String
    Scheme = "https://"
    Domain = ActiveCell.Value
    CloudDomain = "https://ebook.yourcloudlibrary.com/library/"
   
    'Validate Selection
    If Len(Domain) > 0 Then
        If Domain = "hoopladigital" Then
            ActiveCell.Hyperlinks.Add ActiveCell, Scheme & Domain & ".com", , , Domain
            Debug.Print "Condition 1"
        ElseIf Domain = "openlibrary" Then
            ActiveCell.Hyperlinks.Add ActiveCell, Scheme & Domain & ".org", , , Domain
            Debug.Print "Condition 2"
        ElseIf Domain = LCase(Domain) Then
            ActiveCell.Hyperlinks.Add ActiveCell, Scheme & Domain & ".overdrive.com", , , Domain
            Debug.Print "Condition 3"
        ElseIf Len(Domain) > 0 Then
            ActiveCell.Hyperlinks.Add ActiveCell, CloudDomain & Domain, , , Domain
            Debug.Print "Condition 4"
        Else
            Debug.Print "None of the conditions have been met"
        End If
    End If
End Sub
 
Upvote 0
Solution
Recommend you restructure to make your code easier to follow and debug.
VBA Code:
Public Sub dLibrary2()
  
    'Digital Library Column
    Dim Scheme As String, Domain As String, CloudDomain As String
    Scheme = "https://"
    Domain = ActiveCell.Value
    CloudDomain = "https://ebook.yourcloudlibrary.com/library/"
  
    'Validate Selection
    If Len(Domain) > 0 Then
        If Domain = "hoopladigital" Then
            ActiveCell.Hyperlinks.Add ActiveCell, Scheme & Domain & ".com", , , Domain
            Debug.Print "Condition 1"
        ElseIf Domain = "openlibrary" Then
            ActiveCell.Hyperlinks.Add ActiveCell, Scheme & Domain & ".org", , , Domain
            Debug.Print "Condition 2"
        ElseIf Domain = LCase(Domain) Then
            ActiveCell.Hyperlinks.Add ActiveCell, Scheme & Domain & ".overdrive.com", , , Domain
            Debug.Print "Condition 3"
        ElseIf Len(Domain) > 0 Then
            ActiveCell.Hyperlinks.Add ActiveCell, CloudDomain & Domain, , , Domain
            Debug.Print "Condition 4"
        Else
            Debug.Print "None of the conditions have been met"
        End If
    End If
End Sub
Thanks for taking the time to help me with this.
I realize now I failed to close out my 2nd IF statement.
However, I like the debugging you applied and will use your updated code as is.
VBA Testing.xlsm
A
1Digital Library
2hoopladigital
3openlibrary
4lacountylibrary
5GlendaleLAC
If-Then-Else

Thanks a lot and best regards...
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,184
Members
452,615
Latest member
bogeys2birdies

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