Consulting

Results 1 to 3 of 3

Thread: Else without IF error !!!

  1. #1
    VBAX Newbie
    Joined
    Apr 2018
    Posts
    1
    Location

    Else without IF error !!!

    Dear Team ,
    Iam using the below for exporting range to txt files andi repeate the below for more than one range it was working fine before i add the IF statement , i need the if to check if the Cell ( AT5) is 0 then get MSG box and end the vba and dont continue for the next one else if it include a value then do the rest of the statement .
    iam getting error else without if ?
    appreciate your support .



    Sub Button3_Click()
    Dim AK As Range, AL As Range
    Dim output15 As String
    'here to input the range of the dataa you need to export
    
    
    If Range("AT5") = 0 Then MsgBox Range("AT5") & ".txt" & "Export NoT completed No Data Found"
    counter = counter + 1
    End
    Else
    For Each AL In Range("AU7:AU66").Rows
    For Each AK In AL.Cells
    'Report out first blank cell found in Column A
    
    
    output15 = output15 & AK.Value
    
    Next AK
    output15 = output15 & vbNewLine
    Next AL
    
    ' here to enter the full path and the name of file that will be saved by the name of the departement
    Open "C:\SPG-MI-Migration\F&PScripts\Users" & Range("AT5") & ".txt" For Output As #1
    Print #1, output15
    Close
    
    MsgBox Range("AT5") & ".txt" & "Export completed"
    counter = counter + 1
    
    
    End If
    
    
    End Sub
    Last edited by Paul_Hossler; 04-26-2018 at 05:24 PM.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,738
    Location
    1. I added CODE tags to your macro - you can use the [#] icom and paste the macro code beween them to set it off and to format it

    2. This is a 'one line' If/Then format. If the MsgBox was on a seperate line, there's no error

    If Range("AT5") = 0 Then MsgBox Range("AT5") & ".txt" & "Export NoT completed No Data Found"
    counter = counter + 1
    End
    Else

    3. Indented and with blank lines added to visually seperate blocks


    Sub Button3_Click()
    
        Dim AK As Range, AL As Range
        Dim output15 As String
    
        'here to input the range of the dataa you need to export
        If Range("AT5") = 0 Then
            MsgBox Range("AT5") & ".txt" & "Export NoT completed No Data Found"
            counter = counter + 1
            End
       
         Else
            For Each AL In Range("AU7:AU66").Rows
                For Each AK In AL.Cells
                    'Report out first blank cell found in Column A
                    output15 = output15 & AK.Value
                Next AK
                
                output15 = output15 & vbNewLine
            Next AL
    
            ' here to enter the full path and the name of file that will be saved by the name of the departement
            Open "C:\SPG-MI-Migration\F&PScripts\Users" & Range("AT5") & ".txt" For Output As #1
            Print #1, output15
            Close
            
            MsgBox Range("AT5") & ".txt" & "Export completed"
            counter = counter + 1
        
        End If
    
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    i am getting error else without if
    A one line If, (If Blah Then Blah) stands alone. You can't use an Else or an End if with it. Those require multi line Ifs
    If Blah then
      Blah
    Else
      Blah
    End If
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

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