Copy Row based on if the last 4 column Values are combined are greater than Zero.

BrayBoy

New Member
Joined
Nov 27, 2010
Messages
24
Hello everyone, I am not new to VBA coding but I simply don't do it enough to stay good at it. A little background I have a Spreadsheet with 1,435 lines first 5 Columns basic user information next 12 columns are the hours recorded for this person for the Month. EX: ( Jan-19 = 160) which would be 160 hours for the month. I would like to have the option to Chose how many months used for the condition. but the Default should be 4. Assuming the combined Value is greater than 0 copy the entire row different worksheet ("Steady-State"). Could anyone assist?

NameJan -2019Feb-2019Mar-2019Apr-2019May-2019Jun-2019Jul-2019Aug-2019
John Doe160801841690000
Jane Doe185176171184184160170144
Based on the Default request of the last 4 columns combined being greater than Zero. Only Jane Doe's entire Row would be copied to the Steady-State Sheet.



Thanks for the help
 
Last edited by a moderator:

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try:
VBA Code:
Sub CopyRows()
    Application.ScreenUpdating = False
    Dim LastRow As Long, lCol As Long, response As Long, rName As Range, desWS As Worksheet
    Set desWS = Sheets("Steady-State")
    lCol = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    response = InputBox("Enter the number of columns.")
    For Each rName In Range("A2", Range("A" & Rows.Count).End(xlUp))
        If WorksheetFunction.Sum(Cells(rName.Row, lCol - 3).Resize(, response)) > 0 Then
            rName.EntireRow.Copy desWS.Cells(desWS.Rows.Count, "A").End(xlUp).Offset(1, 0)
        End If
    Next rName
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Code:
Sub do_It()

x = InputBox("no fo columns to check", , 4)


Dim rs As Worksheet
Set rs = Worksheets("Steady-State")
For r = 2 To Cells(Rows.Count, "A").End(xlUp).Row

lc = Cells(r, Columns.Count).End(xlToLeft).Column

tot = WorksheetFunction.Sum(Range(Cells(r, lc), Cells(r, lc - x + 1)))
If tot > 0 Then Rows(r).Copy rs.Rows(rs.Cells(rs.Rows.Count, "A").End(xlUp).Row + 1) 'copy row to new sheet

Next r

End Sub
 
Upvote 0
Try:
VBA Code:
Sub CopyRows()
    Application.ScreenUpdating = False
    Dim LastRow As Long, lCol As Long, response As Long, rName As Range, desWS As Worksheet
    Set desWS = Sheets("Steady-State")
    lCol = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    response = InputBox("Enter the number of columns.")
    For Each rName In Range("A2", Range("A" & Rows.Count).End(xlUp))
        If WorksheetFunction.Sum(Cells(rName.Row, lCol - 3).Resize(, response)) > 0 Then
            rName.EntireRow.Copy desWS.Cells(desWS.Rows.Count, "A").End(xlUp).Offset(1, 0)
        End If
    Next rName
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Code:
Sub do_It()

x = InputBox("no fo columns to check", , 4)


Dim rs As Worksheet
Set rs = Worksheets("Steady-State")
For r = 2 To Cells(Rows.Count, "A").End(xlUp).Row

lc = Cells(r, Columns.Count).End(xlToLeft).Column

tot = WorksheetFunction.Sum(Range(Cells(r, lc), Cells(r, lc - x + 1)))
If tot > 0 Then Rows(r).Copy rs.Rows(rs.Cells(rs.Rows.Count, "A").End(xlUp).Row + 1) 'copy row to new sheet

Next r

End Sub
 
Upvote 0
Rpaulson, thank you for the Quick Response. I liked Both options you Mumps provided but Thought your input boxed allowed users who do not know VBA to change the number of columns.. thank you both again for help ..
 
Upvote 0

Forum statistics

Threads
1,215,749
Messages
6,126,656
Members
449,326
Latest member
asp123

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top