+ Reply to Thread
Results 1 to 5 of 5

Code to check and match not working

Hybrid View

  1. #1
    Registered User
    Join Date
    03-18-2011
    Location
    Dayton, ME, USA
    MS-Off Ver
    Excel 2004 for MAC 11.5.4
    Posts
    77

    Code to check and match not working

    Hi

    I have the following code that is supposed to check cell AC for "YES" , if that is correct then put data into AH:AT from the row immediately above it.

    What am I doing wrong?

    Thanks
    Jon

    Sub fillgap()
    With ActiveSheet
    LR = .Range("A" & .Rows.Count).End(xlUp).Row
    If Range("AC" & Rw) = ("YES") Then
    Range("AH:AT" & Rw) = Range("AH:AT" & Rw - 1)
    End If
    End With
    
    End Sub
    Last edited by endoskeleton; 05-25-2011 at 04:00 PM.

  2. #2
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Code to check and match not working

    What's the Rw variable? Shouldn't that be LR???
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  3. #3
    Registered User
    Join Date
    03-18-2011
    Location
    Dayton, ME, USA
    MS-Off Ver
    Excel 2004 for MAC 11.5.4
    Posts
    77

    Re: Code to check and match not working

    Hi Dave

    I'm very new to VBA, but on a steep learning curve.

    I see what you mean but when I replace the Rw with LR, the macro runs but nothing happens.

    It seems like it is finding no instances of "YES" within col AC.

    The "YES" in Column AC is actually generated by a formula
     If(AB5=AB4,"YES,"")
    and then copied down the column does that make a difference ?

    Cheers

    Jon

  4. #4
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Code to check and match not working

    Your code only looks at the last cell in Colum AC. If you want to look at every cell in the column, you have to loop through each of them:

    Sub fillgap()
    
        Dim LR As Long
    
        With ActiveSheet
            For LR = 2 To .Range("A" & .Rows.Count).End(xlUp).Row
                If Range("AC" & LR) = ("YES") Then
                    Range("AH" & LR - 1 & ":AT" & LR - 1).Copy Destination:=Range("AH" & LR & ":AT" & LR)
                End If
            Next LR
        End With
    
    End Sub

  5. #5
    Registered User
    Join Date
    03-18-2011
    Location
    Dayton, ME, USA
    MS-Off Ver
    Excel 2004 for MAC 11.5.4
    Posts
    77

    Re: Code to check and match not working

    Hi Dave,

    Ahh I see,.

    Didn't work the way at wanted at first until I realized that is was actually overwriting where I already had data. Once I changed the destination range it worked like a dream.

    Thanks so much for your help.

    Jon

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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