Creating a New Sheet with all relevant data from a different table.

steveh8204

Board Regular
Joined
Aug 20, 2018
Messages
143
Hi,

is it possible to create code to go through a table and if a value is met transfer the whole contents of the row into a new sheet totalling all relevant values.

For example creating a sheet with all the row information for every row that has the value in Column F higher than 0?

Thanks in advance.
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Without knowing the sheet names and exactly how your data is organized, try this macro.
Code:
Sub CopyData()
    Application.ScreenUpdating = False
    Dim LastRow As Long, srcWS As Worksheet
    Set srcWS = Sheets("Sheet1")
    With srcWS
        LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        .Range("F1:F" & LastRow).AutoFilter Field:=1, Criteria1:=">0"
        .Range("F2:F" & LastRow).SpecialCells(xlCellTypeVisible).EntireRow.Copy Sheets.Add(after:=Sheets(Sheets.Count)).Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
        .Range("F1").AutoFilter
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Is this what you want

DATA IN Sheet1

Excel 2016 (Windows) 32 bit
A
B
C
D
E
F
G
H
1
NameDateTypeClassDueValueCodeArea
2
name01
18/04/2019​
RX
3​
18/05/2019​
286​
WZAVSW
3
name02
16/04/2019​
AL
1​
16/05/2019​
593​
LPISNW
4
name03
13/04/2019​
NL
3​
13/05/2019​
413​
PMZUSE
5
name04
15/04/2019​
VC
5​
15/05/2019​
-27​
VKCJNE
6
name05
16/04/2019​
YO
7​
16/05/2019​
-93​
OYSYSW
7
name06
18/04/2019​
XC
8​
18/05/2019​
86​
BXIENW
8
name07
11/04/2019​
DI
5​
11/05/2019​
255​
IGTGSE
9
name08
16/04/2019​
OF
2​
16/05/2019​
95​
DQQHNE
10
name09
16/04/2019​
WK
9​
16/05/2019​
-117​
DULFSW
11
name10
08/04/2019​
KD
5​
08/05/2019​
522​
BPVANW
12
name11
09/04/2019​
HX
6​
09/05/2019​
-136​
RMTPSE
13
name12
15/04/2019​
QQ
3​
15/05/2019​
63​
DBCCNE
14
name13
13/04/2019​
NC
5​
13/05/2019​
685​
UFECSW
15
name14
10/04/2019​
QY
1​
10/05/2019​
-106​
BTDQNW
16
name15
14/04/2019​
QE
9​
14/05/2019​
297​
IQQZSE
17
name16
18/04/2019​
TI
2​
18/05/2019​
586​
ATRMNE
18
name17
11/04/2019​
TN
3​
11/05/2019​
176​
LTXUSW
19
name18
12/04/2019​
XN
2​
12/05/2019​
33​
HWTANW
20
21
Sheet: Sheet1


CREATED SHEET

Excel 2016 (Windows) 32 bit
A
B
C
D
E
F
G
H
1
NameDateTypeClassDueValueCodeArea
2
name01
18/04/2019​
RX
3​
18/05/2019​
286​
WZAVSW
3
name02
16/04/2019​
AL
1​
16/05/2019​
593​
LPISNW
4
name03
13/04/2019​
NL
3​
13/05/2019​
413​
PMZUSE
5
name06
18/04/2019​
XC
8​
18/05/2019​
86​
BXIENW
6
name07
11/04/2019​
DI
5​
11/05/2019​
255​
IGTGSE
7
name08
16/04/2019​
OF
2​
16/05/2019​
95​
DQQHNE
8
name10
08/04/2019​
KD
5​
08/05/2019​
522​
BPVANW
9
name12
15/04/2019​
QQ
3​
15/05/2019​
63​
DBCCNE
10
name13
13/04/2019​
NC
5​
13/05/2019​
685​
UFECSW
11
name15
14/04/2019​
QE
9​
14/05/2019​
297​
IQQZSE
12
name16
18/04/2019​
TI
2​
18/05/2019​
586​
ATRMNE
13
name17
11/04/2019​
TN
3​
11/05/2019​
176​
LTXUSW
14
name18
12/04/2019​
XN
2​
12/05/2019​
33​
HWTANW
15
16
17
Sheet: Sheet1 (2)

VBA

Code:
Sub Filter_and_Copy()
    Dim ws As Worksheet, rng As Range
    Sheets("[COLOR=#ff0000]Sheet1[/COLOR]").Copy after:=Sheets(Sheets.Count)
    Set ws = Sheets(Sheets.Count)
    ws.AutoFilterMode = False
    Set rng = ws.Range("A:Z")
    rng.AutoFilter Field:=6, Criteria1:="<=0", Operator:=xlAnd
    ws.Rows("2:" & rng.Rows.Count).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    ws.AutoFilterMode = False
End Sub
 
Upvote 0
Without knowing the sheet names and exactly how your data is organized, try this macro.
Code:
Sub CopyData()
    Application.ScreenUpdating = False
    Dim LastRow As Long, srcWS As Worksheet
    Set srcWS = Sheets("Sheet1")
    With srcWS
        LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        .Range("F1:F" & LastRow).AutoFilter Field:=1, Criteria1:=">0"
        .Range("F2:F" & LastRow).SpecialCells(xlCellTypeVisible).EntireRow.Copy Sheets.Add(after:=Sheets(Sheets.Count)).Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
        .Range("F1").AutoFilter
    End With
    Application.ScreenUpdating = True
End Sub

Nice one, thanks. This seems to do the job. Is there a way to get this to copy the data to a pre-formatted sheet that already exists though as opposed to a new one. Otherwise the data is all squashed up.

Thanks very much for your help.
 
Upvote 0
Is this what you want

DATA IN Sheet1

Excel 2016 (Windows) 32 bit
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]A[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]B[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]C[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]D[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]E[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]F[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]G[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]H[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]1[/COLOR]​
NameDateTypeClassDueValueCodeArea
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]2[/COLOR]​
name01
18/04/2019​
RX
3​
18/05/2019​
286​
WZAVSW
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]3[/COLOR]​
name02
16/04/2019​
AL
1​
16/05/2019​
593​
LPISNW
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]4[/COLOR]​
name03
13/04/2019​
NL
3​
13/05/2019​
413​
PMZUSE
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]5[/COLOR]​
name04
15/04/2019​
VC
5​
15/05/2019​
-27​
VKCJNE
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]6[/COLOR]​
name05
16/04/2019​
YO
7​
16/05/2019​
-93​
OYSYSW
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]7[/COLOR]​
name06
18/04/2019​
XC
8​
18/05/2019​
86​
BXIENW
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]8[/COLOR]​
name07
11/04/2019​
DI
5​
11/05/2019​
255​
IGTGSE
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]9[/COLOR]​
name08
16/04/2019​
OF
2​
16/05/2019​
95​
DQQHNE
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]10[/COLOR]​
name09
16/04/2019​
WK
9​
16/05/2019​
-117​
DULFSW
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]11[/COLOR]​
name10
08/04/2019​
KD
5​
08/05/2019​
522​
BPVANW
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]12[/COLOR]​
name11
09/04/2019​
HX
6​
09/05/2019​
-136​
RMTPSE
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]13[/COLOR]​
name12
15/04/2019​
QQ
3​
15/05/2019​
63​
DBCCNE
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]14[/COLOR]​
name13
13/04/2019​
NC
5​
13/05/2019​
685​
UFECSW
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]15[/COLOR]​
name14
10/04/2019​
QY
1​
10/05/2019​
-106​
BTDQNW
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]16[/COLOR]​
name15
14/04/2019​
QE
9​
14/05/2019​
297​
IQQZSE
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]17[/COLOR]​
name16
18/04/2019​
TI
2​
18/05/2019​
586​
ATRMNE
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]18[/COLOR]​
name17
11/04/2019​
TN
3​
11/05/2019​
176​
LTXUSW
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]19[/COLOR]​
name18
12/04/2019​
XN
2​
12/05/2019​
33​
HWTANW
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]20[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]21[/COLOR]​

<tbody>
</tbody>
Sheet: Sheet1

<tbody>
</tbody>


CREATED SHEET

Excel 2016 (Windows) 32 bit
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]A[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]B[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]C[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]D[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]E[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]F[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]G[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]H[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]1[/COLOR]​
NameDateTypeClassDueValueCodeArea
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]2[/COLOR]​
name01
18/04/2019​
RX
3​
18/05/2019​
286​
WZAVSW
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]3[/COLOR]​
name02
16/04/2019​
AL
1​
16/05/2019​
593​
LPISNW
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]4[/COLOR]​
name03
13/04/2019​
NL
3​
13/05/2019​
413​
PMZUSE
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]5[/COLOR]​
name06
18/04/2019​
XC
8​
18/05/2019​
86​
BXIENW
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]6[/COLOR]​
name07
11/04/2019​
DI
5​
11/05/2019​
255​
IGTGSE
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]7[/COLOR]​
name08
16/04/2019​
OF
2​
16/05/2019​
95​
DQQHNE
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]8[/COLOR]​
name10
08/04/2019​
KD
5​
08/05/2019​
522​
BPVANW
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]9[/COLOR]​
name12
15/04/2019​
QQ
3​
15/05/2019​
63​
DBCCNE
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]10[/COLOR]​
name13
13/04/2019​
NC
5​
13/05/2019​
685​
UFECSW
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]11[/COLOR]​
name15
14/04/2019​
QE
9​
14/05/2019​
297​
IQQZSE
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]12[/COLOR]​
name16
18/04/2019​
TI
2​
18/05/2019​
586​
ATRMNE
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]13[/COLOR]​
name17
11/04/2019​
TN
3​
11/05/2019​
176​
LTXUSW
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]14[/COLOR]​
name18
12/04/2019​
XN
2​
12/05/2019​
33​
HWTANW
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]15[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]16[/COLOR]​
[COLOR=[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF]#FFFFFF[/URL] ]17[/COLOR]​

<tbody>
</tbody>
Sheet: Sheet1 (2)

<tbody>
</tbody>

VBA

Code:
Sub Filter_and_Copy()
    Dim ws As Worksheet, rng As Range
    Sheets("[COLOR=#ff0000]Sheet1[/COLOR]").Copy after:=Sheets(Sheets.Count)
    Set ws = Sheets(Sheets.Count)
    ws.AutoFilterMode = False
    Set rng = ws.Range("A:Z")
    rng.AutoFilter Field:=6, Criteria1:="<=0", Operator:=xlAnd
    ws.Rows("2:" & rng.Rows.Count).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    ws.AutoFilterMode = False
End Sub

Thats exactly what I need but I've had a few problems when I've run the code. Firstly an error message came up saying "The name Vigoraws already exists, click yes to use that name etc" which was kind of strange as that isn't the name of any of the sheets but is actually the name of one in a different File I didn't even have open.

Theres a few sheets with different names in case that could be a part of the problem.
 
Upvote 0
Insert this line of code:
Code:
Columns.AutoFit
below this line:
Code:
.Range("F1").AutoFilter
If this change doesn't do what you want and if you want to copy the data to an existing sheet, I would need to know the name of that sheet.
 
Upvote 0
Insert this line of code:
Code:
Columns.AutoFit
below this line:
Code:
.Range("F1").AutoFilter
If this change doesn't do what you want and if you want to copy the data to an existing sheet, I would need to know the name of that sheet.

Thanks for the quick reply. I could do with it being in an existing sheet as I need to create Macros to manipulate Data in that sheet. The sheet name is "GraphicWIP".

Thanks again.
 
Upvote 0
Try:
Code:
Sub CopyData()
    Application.ScreenUpdating = False
    Dim LastRow As Long, srcWS As Worksheet
    Set srcWS = Sheets("Sheet1")
    With srcWS
        LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        .Range("F1:F" & LastRow).AutoFilter Field:=1, Criteria1:=">0"
        .Range("F2:F" & LastRow).SpecialCells(xlCellTypeVisible).EntireRow.Copy Sheets("GraphicWIP").Cells(Sheets("GraphicWIP").Rows.Count, "A").End(xlUp).Offset(1, 0)
        .Range("F1").AutoFilter
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try:
Code:
Sub CopyData()
    Application.ScreenUpdating = False
    Dim LastRow As Long, srcWS As Worksheet
    Set srcWS = Sheets("Sheet1")
    With srcWS
        LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        .Range("F1:F" & LastRow).AutoFilter Field:=1, Criteria1:=">0"
        .Range("F2:F" & LastRow).SpecialCells(xlCellTypeVisible).EntireRow.Copy Sheets("GraphicWIP").Cells(Sheets("GraphicWIP").Rows.Count, "A").End(xlUp).Offset(1, 0)
        .Range("F1").AutoFilter
    End With
    Application.ScreenUpdating = True
End Sub

Nice one, thats perfect. I can adapt that easily to a few other files that will be very handy for.

Thanks again for your help, it has been very much appreciated.
 
Upvote 0

Forum statistics

Threads
1,215,554
Messages
6,125,487
Members
449,233
Latest member
Deardevil

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