Consulting

Results 1 to 5 of 5

Thread: VBA Compile Error "Else without if"

  1. #1
    VBAX Newbie
    Joined
    Apr 2017
    Posts
    4
    Location

    VBA Compile Error "Else without if"

    Hi Guys,

    trying to make this thing work, but getting an error.
    Can you pls take a look?
    Sub Enter_Text()
    'Setting Variables
        Dim Rng As Range
        Dim WorkRng As Range
        Dim Txt As String
        Dim Location As String
        Dim Position As Integer
        Dim Msg As String
        On Error Resume Next
    
    
    'Defining Variables
    xTitleID = "Macro"
    
    
    Set WorkRng = Application.Selection
    Set WorkRng = Application.InputBox("Select a Range", xTitleID, WorkRng.Address, Type:=8)
    
    
    Txt = Application.Selection
    Txt = Application.InputBox("Enter Desired Text", xTitleID, Type:=2)
    
    
    Location = Application.Selection
    Location = Application.InputBox("Type whether you want to add text from LEFT or RIGHT", xTitleID, Type:=2)
    
    
    Position = Application.Selection
    Position = Application.InputBox("Enter Text position with amount of digits", xTitleID, Type:=1)
    
    
    Msg = MsgBox("Please check your criterias and Try Again", , xTitleID)
    
    
    'Setting Output
    
    
    If Location = "LEFT" Then
            For Each Rng In WorkRng
            Rng.Value = VBA.Left(Rng.Value, Postion) & Txt & Mid(Rng.Value, Len(Postion + 1), Len(Rng.Value) - Len(Postion))
            
    ElseIf Location = "RIGHT" Then
            For Each Rng In WorkRng
            Rng.Value = VBA.Left(Rng.Value, Len(Rng.Value) - Len(Right(Rng.Value, Position))) & Txt & Right(Rng.Value, Position)
    Else
        Msg
    End If
    Next
    End Sub

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    You need to add
    Next Rng
    above the offending Else.

    Then the line "Msg" will error.

  3. #3
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    613
    Location
    .
    .
    See if this works for you:

    Option Explicit
    
    
    Sub Enter_Text()
         'Setting Variables
        Dim Rng As Range
        Dim WorkRng As Range
        Dim Txt As String
        Dim Location As String
        Dim Position As Integer
        Dim Msg As String
        Dim xTitleID As String
        'On Error Resume Next
         
         
         'Defining Variables
        xTitleID = "Macro"
         
         
        Set WorkRng = Application.Selection
        Set WorkRng = Application.InputBox("Select a Range", xTitleID, WorkRng.Address, Type:=8)
         
         
        Txt = Application.Selection
        Txt = Application.InputBox("Enter Desired Text", xTitleID, Type:=2)
         
         
        Location = Application.Selection
        Location = Application.InputBox("Type whether you want to add text from LEFT or RIGHT", xTitleID, Type:=2)
         
         
        Position = Application.Selection
        Position = Application.InputBox("Enter Text position with amount of digits", xTitleID, Type:=1)
         
         
        Msg = MsgBox("Please check your criterias and Try Again", , xTitleID)
         
         
         'Setting Output
         
         
        If Location = "LEFT" Then
            For Each Rng In WorkRng
                Rng.Value = VBA.Left(Rng.Value, Position) & Txt & Mid(Rng.Value, Len(Position) + 1, Len(Rng.Value) - Len(Position))
            Next
        End If
        If Location = "RIGHT" Then
                For Each Rng In WorkRng
                    Rng.Value = VBA.Left(Rng.Value, Len(Rng.Value) - Len(Right(Rng.Value, Position))) & Txt & Right(Rng.Value, Position)
                Next
        End If
    End Sub

  4. #4
    VBAX Newbie
    Joined
    Apr 2017
    Posts
    4
    Location
    Thank You Logit!

    works perfectly now!

  5. #5
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    613
    Location
    You are welcome. Thank you !

    Cheers

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •