How to create macro that conditionally copies parts of rows from a main sheet to a summary sheet.

jduzz

New Member
Joined
Jun 18, 2011
Messages
19
Dear all, I have only recently started with Excel and need help with what I think is a relatively easy problem:
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>a) I have a sheet1, it has many rows, in column F there is a status field (sheet1.value = ‘a’, ‘b’, ‘c’, ‘x’ or ‘y’)
b) I maintain this sheet, change whatever I want
c) When finished I want to copy a subset of sheet1 to sheet2 by pushing a button (w. a macro) in sheet1
d) When I push this button all rows in sheet1 where status is ‘x’ or ‘y’ should be copied into sheet2
e) Not the entire rows should be copied into sheet2, only the first 14 fields (the fields in column A through column M)
f) When I maintain sheet1 again, and push the button again, the data in sheet2 that I just generated must be entirely overwritten with the latest from sheet1.
<o:p></o:p>
That is really it.
I have tried this now for days but I cannot make it work unfortunately :(.
Your help is very much appreciated.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Try

Code:
Sub test()
Dim LR As Long, i As Long
Sheets("Sheet2").UsedRange.ClearContents
With Sheets("Sheet1")
    LR = .Range("F" & Rows.Count).End(xlUp).Row
    For i = 1 To LR
        With .Range("F" & i)
            If .Value = "x" Or .Value = "y" Then
                .Offset(, -5).Resize(, 13).Copy Destination:=Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1)
            End If
        End With
    Next i
End With
End Sub
 
Upvote 0
Thanks a million for that.

What I forgot to mention is that in Sheet2, I first want all the rows with 'x', then a blank row, and then all the rows with 'y'...
 
Upvote 0
Try

Code:
Sub test()
Dim LR As Long, i As Long
Sheets("Sheet2").UsedRange.ClearContents
With Sheets("Sheet1")
    LR = .Range("F" & Rows.Count).End(xlUp).Row
    For i = 1 To LR
        With .Range("F" & i)
            If .Value = "x" Then
                .Offset(, -5).Resize(, 13).Copy Destination:=Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1)
            End If
        End With
    Next i
    Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1).Value = " "
    For i = 1 To LR
        With .Range("F" & i)
            If .Value = "y" Then
                .Offset(, -5).Resize(, 13).Copy Destination:=Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1)
            End If
        End With
    Next i
End With
End Sub
 
Upvote 0
Peter, I have tried it on an empty workbook and it works brilliantly!!!

Thanks a million for that!

I will now try build it into my existing excel file...


Regards


JD
 
Upvote 0
Peter, it seems that in my existing worksheet, it only copies the last row with 'x' and after that the last row with 'y', without an empty row between it.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
This while in a new workbook it works fine.<o:p></o:p>
<o:p> </o:p>
What could this be about?<o:p></o:p>
<o:p></o:p>
Also two other questions<o:p></o:p>
<o:p></o:p>
1. I have a number of date fields (DD-MM-YY in columns G and H) that contain the outcome of a calculation from a weeknumber (so the cell has a formula). Can this outcome be copied into sheet2? (Now I get a bunch of symbols)<o:p></o:p>
<o:p></o:p>
2. I have a few numeric fields (0.0 in columns I thru M) that each contain a link to a similar numeric field in a third sheet. Can these be copied into sheet2? (now I get a number, but from completely wrong rows)<o:p></o:p>
<o:p></o:p>
Thanks again!<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
JD<o:p></o:p>
 
Upvote 0
I don't know why it would only copy one row of each - for me it copies them all. Could some cells contain spaces - i.e. "x " instead of "x"?

This will paste values

Code:
[IMG]file:///C:/DOCUME%7E1/PETERJ%7E1/LOCALS%7E1/Temp/moz-screenshot-8.png[/IMG]Sub test()
Dim LR As Long, i As Long
Sheets("Sheet2").UsedRange.ClearContents
With Sheets("Sheet1")
    LR = .Range("F" & Rows.Count).End(xlUp).Row
    For i = 1 To LR
        With .Range("F" & i)
            If .Value = "x" Then
                .Offset(, -5).Resize(, 13).Copy
                Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
            End If
        End With
    Next i
    Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1).Value = " "
    For i = 1 To LR
        With .Range("F" & i)
            If .Value = "y" Then
                .Offset(, -5).Resize(, 13).Copy
                Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
            End If
        End With
    Next i
End With
End Sub
 
Upvote 0
Peter, the values are now copied perfectly.

Two more issues:

1. When the macro runs, the screen flickers.

2. In regards to the idea of having spaces in the status fields: I have filled in a '0' in all status fields, then change a few with x and y, no difference: it only copies the last x and then the last y (without an empty row).

- Released macro security
- Unfreeze sheet1 header
- Unprotect all sheets
- ?

Does not help. What on earth can that be? If it works in a new excel file, why not in my existing one?
 
Upvote 0
This should stop the flicker. Sorry but I don't know why not all rows are being copied.

Code:
Sub test()
Dim LR As Long, i As Long
Application.ScreenUpdating = False
Sheets("Sheet2").UsedRange.ClearContents
With Sheets("Sheet1")
    LR = .Range("F" & Rows.Count).End(xlUp).Row
    For i = 1 To LR
        With .Range("F" & i)
            If .Value = "x" Then
                .Offset(, -5).Resize(, 13).Copy
                Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
            End If
        End With
    Next i
    Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1).Value = " "
    For i = 1 To LR
        With .Range("F" & i)
            If .Value = "y" Then
                .Offset(, -5).Resize(, 13).Copy
                Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
            End If
        End With
    Next i
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0
The only possible explanation could be: might this have to do with the fact that I am using conditional formatting formulas in Sheet1 (to color the cells)?<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
By the way: where the macro used to also copy the color of the different rows, using the last code you sent, it does not do this anymore?<o:p></o:p>
<o:p></o:p>
Thanks again!<o:p></o:p>
<o:p> </o:p>
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,291
Members
452,902
Latest member
Knuddeluff

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