+ Reply to Thread
Results 1 to 10 of 10

VBA - Copy cell A if cell AE contains 'YES'

Hybrid View

  1. #1
    Registered User
    Join Date
    09-13-2017
    Location
    london
    MS-Off Ver
    2010
    Posts
    9

    VBA - Copy cell A if cell AE contains 'YES'

    I am trying to create a VB Button that will copy the contents of all worksheets' column A of any rows where the contents of Column AE contain 'YES'. Please see attached excel file.

    However, the VB script is pulling back some odd results (e.g. Acrington Stanley) where rows are being copied even though there is no contents in row AE.

    Can anyone help?
    Attached Files Attached Files

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,107

    Re: VBA - Copy cell A if cell AE contains 'YES'

    Once you copy the row, what do you want to do with it?
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Registered User
    Join Date
    09-13-2017
    Location
    london
    MS-Off Ver
    2010
    Posts
    9
    Quote Originally Posted by Mumps1 View Post
    Once you copy the row, what do you want to do with it?
    Hi. Have a look at the code already written. I want to copy the contents of the cell in Column A to the Sheet ‘Active Pursuits’. I need to form a list of ALL the clubs that have YES in cell AE but only wish to copy over the contents of column A for each of these.

  4. #4
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,107

    Re: VBA - Copy cell A if cell AE contains 'YES'

    Try:
    Private Sub CommandButton1_Click()
        Dim ws As Worksheet
        Dim sh As Worksheet
        Dim bottomE As Long
        Application.ScreenUpdating = False
        Set sh = Sheets("Active Pursuits")
        For Each ws In Worksheets
            If ws.Name <> "Active Pursuits" And ws.Name <> "DO NOT USE" And ws.Name <> "Non-Affiliated Stadia and Arena" Then
                If WorksheetFunction.CountIf(ws.Range("AE:AE"), "Yes") > 0 Then
                    bottomE = ws.Range("E" & Rows.Count).End(xlUp).Row
                    ws.Range("A1:AM" & bottomE).AutoFilter Field:=31, Criteria1:="Yes"
                    ws.Range("A4:A" & bottomE).SpecialCells(xlCellTypeVisible).Copy
                    sh.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
                    If ws.FilterMode Then ws.ShowAllData
                End If
            End If
        Next ws
        Application.CutCopyMode = False
        Application.ScreenUpdating = True
    End Sub

  5. #5
    Registered User
    Join Date
    09-13-2017
    Location
    london
    MS-Off Ver
    2010
    Posts
    9

    Re: VBA - Copy cell A if cell AE contains 'YES'

    I get the following error: Run-time error '1004': we can't do that to a merged cell.

    The debug takes me to code line: ws.Range("A4:A" & bottomE).SpecialCells(xlCellTypeVisible).Copy

  6. #6
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,529

    Re: VBA - Copy cell A if cell AE contains 'YES'

    Possibly, an if statement would work.,
    for example.

      x = Application.WorksheetFunction.CountIf(ws.Range("AE:AE"), "YES")
                    If x > 0 Then
                    .Offset(1, -30).Copy
                    sh.Range("A" & Rows.Count).End(3)(2).PasteSpecial xlPasteValues
                    End If

  7. #7
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,529

    Re: VBA - Copy cell A if cell AE contains 'YES'

    I thought I already answered this,

    Private Sub CommandButton1_Click()
    
        Dim ws As Worksheet
        Dim sh As Worksheet
    
        Application.ScreenUpdating = False
    
        Set sh = Sheets("Active Pursuits")
    
        On Error Resume Next
        For Each ws In Worksheets
            If ws.Name <> "Active Pursuits" And ws.Name <> "DO NOT USE" And ws.Name <> "Non-Affiliated Stadia and Arena" Then
                With ws.Range("AE1", ws.Range("AE" & ws.Rows.Count).End(xlUp))
                    .AutoFilter 1, "YES"
                    x = Application.WorksheetFunction.CountIf(ws.Range("AE:AE"), "YES")
                    If x > 0 Then
                        .Offset(1, -30).Copy
                        sh.Range("A" & Rows.Count).End(3)(2).PasteSpecial xlPasteValues
                    End If
    
                    .AutoFilter
                End With
            End If
        Next ws
    
        Application.CutCopyMode = False
        Application.ScreenUpdating = True
    
    End Sub

  8. #8
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,107

    Re: VBA - Copy cell A if cell AE contains 'YES'

    The macro doesn't return an error when I run it on the file you posted in Post #1. Click here to download your file and give it a try.
    Last edited by Mumps1; 10-18-2017 at 12:27 PM.

  9. #9
    Registered User
    Join Date
    09-13-2017
    Location
    london
    MS-Off Ver
    2010
    Posts
    9

    Re: VBA - Copy cell A if cell AE contains 'YES'

    It still does not copy across every instance where AE = YES. I am also getting some instances where the AE = empty and the data in column A is being copied. If you insert data into approx 10 rows in column A and Mix column AE with YES and NO, you will see the issue. This is occuring across all worksheets.

    Thanks for all your help.

  10. #10
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,107

    Re: VBA - Copy cell A if cell AE contains 'YES'

    I added a few Yes's in AE of Football - Premier League and Football - Championship and all worked properly. Have a look at the Pursuits sheet. Click here.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Loop copy cell of range to 1 cell, and so on till cell empty
    By countryfan_nt in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-16-2017, 02:33 AM
  2. Replies: 13
    Last Post: 11-26-2014, 05:29 PM
  3. Replies: 1
    Last Post: 11-06-2013, 02:56 AM
  4. copy a cell multiple times depending on cell value starting on a specific cell
    By weritadiojomiel in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-17-2013, 05:25 AM
  5. copy a specif cell value, find another like that in sheet copy adjacent cell and pate
    By smwaqas89 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-16-2013, 10:00 AM
  6. Automatically copy multiple cell VALUES to the cell right below the active cell
    By Sarangsood in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-20-2011, 01:36 AM
  7. Replies: 3
    Last Post: 07-15-2010, 08:49 AM

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