+ Reply to Thread
Results 1 to 4 of 4

IF, Then AND - problems with Syntax

Hybrid View

  1. #1
    Registered User
    Join Date
    01-05-2012
    Location
    Salinas
    MS-Off Ver
    Excel 2007
    Posts
    22

    IF, Then AND - problems with Syntax

    Having some problems with Syntax. I have some experience with programming macros, but this doesn't seem to be working... what am I doing wrong? My goal is to compare 4 separate cell pairs in two different worksheets - if the 4 cell pairs are equivalent then one of the worksheets will be populated by another. Here is what I have. Any help would be greatly appreciated:

    For k = 1 To x_last_row
        
        For i = 1 To y_last_row
         
      If
      
      Worksheets("X").Cells(k, 4).Value = Worksheets("Y").Cells(i, 2).Value And
      Worksheets("X").Cells(k, 11).Value = Worksheets("Y").Cells(i, 3).Value And
      Worksheets("X").Cells(k, 13).Value = Worksheets("Y").Cells(i, 4).Value And
      Worksheets("X").Cells(k, 7).Value = Worksheets("Y").Cells(i, 5).Value
        
      THEN
        
        Worksheets("Y").Cells(i, 87) = Worksheets("X").Cells(k, 1)
        Worksheets("Y").Cells(i, 88) = Worksheets("X").Cells(k, 2)
        Worksheets("Y").Cells(i, 89) = Worksheets("X").Cells(k, 3)
        Worksheets("Y").Cells(i, 90) = Worksheets("X").Cells(k, 4)
        Worksheets("Y").Cells(i, 91) = Worksheets("X").Cells(k, 5)
        
        
        End If
    
        Next i
    
    Next k
    Last edited by Cutter; 09-21-2012 at 09:33 PM. Reason: Added code tags

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,489

    Re: IF, Then AND - problems with Syntax

    I would use:

    For k = 1 To x_last_row
    
    For i = 1 To y_last_row
    
    If Worksheets("X").Cells(k, 4).Value = Worksheets("Y").Cells(i, 2).Value _
    And Worksheets("X").Cells(k, 11).Value = Worksheets("Y").Cells(i, 3).Value _
    And Worksheets("X").Cells(k, 13).Value = Worksheets("Y").Cells(i, 4).Value _
    And Worksheets("X").Cells(k, 7).Value = Worksheets("Y").Cells(i, 5).Value _
    
    THEN
    
    Worksheets("Y").Cells(i, 87) = Worksheets("X").Cells(k, 1)
    Worksheets("Y").Cells(i, 88) = Worksheets("X").Cells(k, 2)
    Worksheets("Y").Cells(i, 89) = Worksheets("X").Cells(k, 3)
    Worksheets("Y").Cells(i, 90) = Worksheets("X").Cells(k, 4)
    Worksheets("Y").Cells(i, 91) = Worksheets("X").Cells(k, 5)
    
    End If
    
    Next i
    
    Next k

    Note that you should add code tags as I have.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    08-31-2012
    Location
    HCM City, VN
    MS-Off Ver
    Excel 2007
    Posts
    29

    Re: IF, Then AND - problems with Syntax

    You should remember that you have to use " _" when you seperate two lines from one. Two below codes are same.
    If a = b And a = c Then e = d
    If a = b And _
         a = c Then e = d
    Anyway, TMShucks helped you sloving your problem.
    Last edited by Cutter; 09-22-2012 at 10:36 AM. Reason: Removed whole post quote

  4. #4
    Registered User
    Join Date
    01-05-2012
    Location
    Salinas
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: IF, Then AND - problems with Syntax

    Thanks all for your help! Sorry for the delayed reply... been deep into programming.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1