Problem with IF

Rafael02

Board Regular
Joined
Jan 17, 2012
Messages
53
I got the following code:

If Cells(1, 2) <> "VERDADEIRO" Then Workbooks.Open myFolder & "\" & strFilename, Local:=True



Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy

Windows("X.xlsx").Activate
Sheets("X").Select
Range("A" & Rows.Count).End(xlUp).Offset(1).Select
ActiveSheet.Paste


else if then cells(3,3)="OK"

end if





Next objFile

It is not working cause it says there is an end if block without the if! All i want it to do is to do all the stuff inside if the cells contains anything except "VERDADEIRO" but if it is "VERDADEIRO" then jump through the stuff inside and put OK.

THANKS!!!!
 
Code:
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
 
Windows("X.xlsx").Activate
Sheets("X").Select
Range("A" & Rows.Count).End(xlUp).Offset(1).Select
ActiveSheet.Paste
 
 
else 
    cells(3,3)="OK"
 
end if

oops, I missed the very first line, but you get the idea.
 
Upvote 0
The very first line of your code
If Cells(1, 2) <> "VERDADEIRO" Then Workbooks.Open myFolder & "\" & strFilename, Local:=True
Is a single self contained If.
Every line below that has no relation to that If.

You need to structure like this

Code:
If somecriteria Then
    Do this
Else
    Do that
End If

Hope that helps.
 
Upvote 0
Hi,

You could check within the helpfiles the different options you have:
1. If ... Then ... on one line
2. using more lines
Code:
Sub test()
    If Cells(1, 2) <> "VERDADEIRO" Then
    Workbooks.Open myFolder & "\" & strFilename, Local:=True
    
    'do your stuff
    
    Else
        If Cells(3, 3) = "OK" Then
        'do other stuff
        End If
    End If
End Sub
kind regards,
Erik
 
Upvote 0
Thanks for the fast answear! it keeps saying ELSE WITHOUT IF!! Let me show you the whole code so you guys can help more:

Sub Oberthur()

Dim FileSys As FileSystemObject
Dim objFile As File
Dim myFolder
Dim strFilename As String

'set path for files - change for your folder
Const myDir As String = "X"

'set up filesys objects
Set FileSys = New FileSystemObject
Set myFolder = FileSys.GetFolder(myDir)
Windows("X").Activate
Sheets("Confere").Select





For Each objFile In myFolder.Files

If Left(objFile.Name, 4) = "BPN_" Then Cells(1, 1) = Mid(objFile.Name, 1, 30)


ActiveSheet.Calculate


strFilename = objFile.Name









If Cells(1, 2) <> "VERDADEIRO" Then Workbooks.Open myFolder & "\" & strFilename, Local:=True



Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy

Windows("X.xlsx").Activate
Sheets("X").Select
Range("A" & Rows.Count).End(xlUp).Offset(1).Select
ActiveSheet.Paste


Else
If Cells(3, 3) = "OK" Then

End If
End If





Next objFile



End Sub
 
Upvote 0
You did not apply what we told you to do.
If you would nicely indent your code, you would see it more easily.

This is what you write, although we told you to not do that.
Code:
If Left(objFile.Name, 4) = "BPN_" Then Cells(1, 1) = Mid(objFile.Name, 1, 30)
End If
 
Upvote 0
As Erik said, if you do the entire IF statement on one line, then you don't need End If, and there is no Else to it.

If you want to do something for true AND false, then you need a multi-line IF, such as

Code:
If...something...Then
    Do something if that's true
Else
    Do something if that's false
End If



You very rarely need Else If (in fact, I can't think of any good reason to use it at all)
 
Upvote 0

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