+ Reply to Thread
Results 1 to 8 of 8

Macro to copy row title to a different sheet based on multiple column criteria

Hybrid View

  1. #1
    Registered User
    Join Date
    12-05-2012
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    6

    Macro to copy row title to a different sheet based on multiple column criteria

    Hi,

    I am having trouble figuring out the correct code that will copy the Row title on Sheet 1 to Sheet 2 based on criteria in columns E & F on Sheet one.

    The Criteria required needed to return the building name is no activity in 2014, column E, and activity in 2015 ,column F, any activity is acceptable both positive and negative. If the criteria is met I am trying to copy the building name to sheet two, if the criteria is not met then move on to the next row.

    I have attached an example spreadsheet. Also, if there are formulas that would work better I am not opposed to using them.

    Thanks in advance for the 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,026

    Re: Macro to copy row title to a different sheet based on multiple column criteria

    Try:

    Sub CopyName()
        Dim bottomA As Integer
        bottomA = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
        Dim c As Range
        For Each c In Sheets("Sheet1").Range("A2:A" & bottomA)
            If c.Offset(0, 4) = "" And c.Offset(0, 5) <> "" Then
                c.Copy Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
            End If
        Next c
    End Sub

  3. #3
    Registered User
    Join Date
    12-05-2012
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Macro to copy row title to a different sheet based on multiple column criteria

    Hey Mumps1,

    A small follow up question. In my actual spreadsheet I need to run the macro for only rows 50-100 of 'Sheet1' with there being information on the rows above and below that range. How could I update your code to only run the macro on that range?

    Thanks again.

  4. #4
    Forum Expert
    Join Date
    12-15-2009
    Location
    Chicago, IL
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,177

    Re: Macro to copy row title to a different sheet based on multiple column criteria

    Sub test()
    Dim Col As Collection
    Dim LR As Long, i As Long
    Dim j5 As Integer, j6 As Integer, j As Integer
    
    Set Col = New Collection
    
    j5 = 5
    j6 = 6
    
    With Worksheets("Sheet1")
    LR = .Cells(Rows.Count, 1).End(xlUp).Row
        For i = 2 To LR
            If .Cells(i, j5).Value = 0 Then
                On Error Resume Next
                Col.Add .Cells(i, 1).Value, CStr(.Cells(i, 1).Value)
            End If
            
            If .Cells(i, j6).Value <> 0 Then
                On Error Resume Next
                Col.Add .Cells(i, 1).Value, CStr(.Cells(i, 1).Value)
            End If
        Next i
    End With
    
    With Worksheets("Sheet2")
        For j = 1 To Col.Count
            .Cells(j + 3, 1) = Col(j)
        Next j
    End With
    End Sub

  5. #5
    Registered User
    Join Date
    12-05-2012
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Macro to copy row title to a different sheet based on multiple column criteria

    It works! Thanks!

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

    Re: Macro to copy row title to a different sheet based on multiple column criteria

    Hi Steve:

    Use this code instead:

    Sub CopyName()
        Dim c As Range
        For Each c In Sheets("Sheet1").Range("A50:A100")
            If c.Offset(0, 4) = "" And c.Offset(0, 5) <> "" Then
                c.Copy Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
            End If
        Next c
    End Sub

  7. #7
    Registered User
    Join Date
    12-05-2012
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Macro to copy row title to a different sheet based on multiple column criteria

    Thank you!

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

    Re: Macro to copy row title to a different sheet based on multiple column criteria

    It was my pleasure.

+ 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