PDA

View Full Version : Else without IF error !!!



midomimo
04-26-2018, 04:34 PM
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

Paul_Hossler
04-26-2018, 05:26 PM
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

SamT
04-26-2018, 05:30 PM
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