• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

How can I join 2 statements?

codedreamer

New Member
Hello,


I have a macro where if one condition is true then two things should happen. I tried to used "and", but it gives me an error message. This is what I have:

[pre]
Code:
If ThisWorkbook.Sheets(i).Range("A1").Value = 5 Then
ThisWorkbook.Sheets(i).Range("B2").Value = ThisWorkbook.Sheets(i).Range("A3").Value AND
'           ThisWorkbook.Sheets(i).Range("D4").Value = ThisWorkbook.Sheets(i).Range("C3").Value
[/pre]
Thank you for your help,


Codedreamer
 
Hello !     Can't you see the integrated VBA help ?


If condition Then

    [statements]

End If


One line by statement, AND is only in a condition or a test …
 
[pre]
Code:
If ThisWorkbook.Sheets(i).Range("A1").Value = 5 Then
ThisWorkbook.Sheets(i).Range("B2").Value = ThisWorkbook.Sheets(i).Range("A3").Value
ThisWorkbook.Sheets(i).Range("D4").Value = ThisWorkbook.Sheets(i).Range("C3").Value

End If
[/pre]

No need for the "And" as everything in the If..End if will be done if the Condition after the If is true
 
Hi Hui,


Thank you for your explanation. I did try that way first, but I get a run time error 1004 "application-defined or object defined error". I do not understand what that means.

Thank you for your help,


Codedreamer
 
Hi !     That means if the i variable is greater than the number of sheets (or less than one), an error occurs …


Also in the case of the cell is locked and the sheet is protected …
 
Back
Top