Copy rows which match criteria into two or more different sheets in Excel with VBA
up vote
0
down vote
favorite
Is there anyone willing to help a really self-taught beginner (with a lot of enthusiasm to learn this stuff)?
For a stock and order file I'm looking for a way to copy the rows in a table which match 1 criteria to another sheet. At the same time I want all other rows (which do not match the 1 criteria) to be copied into another sheet.
I made it to the point where I can copy the rows of table ORDERS on sheet ORDERS, to the table INSTOCKORDERS on sheet INSTOCKORDERS. But what I really want is to make two different sheets out of all the orders. One with the items that are ordered and are in stock. And another sheet with the orders/items that are NOT in stock.
After this action took place, it should empty the ORDERS-table. And after that, the next time it should place the ORDERS into the tables INSTOCKORDERS and NOSTOCKORDERS below their last row.
What I meant to do with the sorting is to put all the items with a stockquantity of 0 on top of the table, which I thought could make it easier to copy all the ones with 0 items to NOSTOCKORDERS and all the other ones to INSTOCKORDERS. But I have no clue on how to make this a neat working action let alone, how to start (sorry for my beginners ignorance...) I went through some former questions, ran into AutoFilter, but didn't get it...
Sub CopyOrders()
'Sorting column STOCK in ORDERS from a-z
Worksheets("Orders").ListObjects("Orders").Sort. _
SortFields.Clear
Worksheets("Orders").ListObjects("Orders").Sort. _
SortFields.Add2 Key:=Range("Orders[[#All],[STOCK]]"), SortOn:= _
xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With Worksheets("Orders").ListObjects("Orders").Sort
.Apply
End With
'Copying the table ORDERS to INSTOCKORDERS
Range("Orders").Copy Range("InStockOrders")
End Sub
excel vba copy rows
add a comment |
up vote
0
down vote
favorite
Is there anyone willing to help a really self-taught beginner (with a lot of enthusiasm to learn this stuff)?
For a stock and order file I'm looking for a way to copy the rows in a table which match 1 criteria to another sheet. At the same time I want all other rows (which do not match the 1 criteria) to be copied into another sheet.
I made it to the point where I can copy the rows of table ORDERS on sheet ORDERS, to the table INSTOCKORDERS on sheet INSTOCKORDERS. But what I really want is to make two different sheets out of all the orders. One with the items that are ordered and are in stock. And another sheet with the orders/items that are NOT in stock.
After this action took place, it should empty the ORDERS-table. And after that, the next time it should place the ORDERS into the tables INSTOCKORDERS and NOSTOCKORDERS below their last row.
What I meant to do with the sorting is to put all the items with a stockquantity of 0 on top of the table, which I thought could make it easier to copy all the ones with 0 items to NOSTOCKORDERS and all the other ones to INSTOCKORDERS. But I have no clue on how to make this a neat working action let alone, how to start (sorry for my beginners ignorance...) I went through some former questions, ran into AutoFilter, but didn't get it...
Sub CopyOrders()
'Sorting column STOCK in ORDERS from a-z
Worksheets("Orders").ListObjects("Orders").Sort. _
SortFields.Clear
Worksheets("Orders").ListObjects("Orders").Sort. _
SortFields.Add2 Key:=Range("Orders[[#All],[STOCK]]"), SortOn:= _
xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With Worksheets("Orders").ListObjects("Orders").Sort
.Apply
End With
'Copying the table ORDERS to INSTOCKORDERS
Range("Orders").Copy Range("InStockOrders")
End Sub
excel vba copy rows
Why not using the INDEX, MATCH pair i/o VBA ?
– hornetbzz
Nov 8 at 13:16
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Is there anyone willing to help a really self-taught beginner (with a lot of enthusiasm to learn this stuff)?
For a stock and order file I'm looking for a way to copy the rows in a table which match 1 criteria to another sheet. At the same time I want all other rows (which do not match the 1 criteria) to be copied into another sheet.
I made it to the point where I can copy the rows of table ORDERS on sheet ORDERS, to the table INSTOCKORDERS on sheet INSTOCKORDERS. But what I really want is to make two different sheets out of all the orders. One with the items that are ordered and are in stock. And another sheet with the orders/items that are NOT in stock.
After this action took place, it should empty the ORDERS-table. And after that, the next time it should place the ORDERS into the tables INSTOCKORDERS and NOSTOCKORDERS below their last row.
What I meant to do with the sorting is to put all the items with a stockquantity of 0 on top of the table, which I thought could make it easier to copy all the ones with 0 items to NOSTOCKORDERS and all the other ones to INSTOCKORDERS. But I have no clue on how to make this a neat working action let alone, how to start (sorry for my beginners ignorance...) I went through some former questions, ran into AutoFilter, but didn't get it...
Sub CopyOrders()
'Sorting column STOCK in ORDERS from a-z
Worksheets("Orders").ListObjects("Orders").Sort. _
SortFields.Clear
Worksheets("Orders").ListObjects("Orders").Sort. _
SortFields.Add2 Key:=Range("Orders[[#All],[STOCK]]"), SortOn:= _
xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With Worksheets("Orders").ListObjects("Orders").Sort
.Apply
End With
'Copying the table ORDERS to INSTOCKORDERS
Range("Orders").Copy Range("InStockOrders")
End Sub
excel vba copy rows
Is there anyone willing to help a really self-taught beginner (with a lot of enthusiasm to learn this stuff)?
For a stock and order file I'm looking for a way to copy the rows in a table which match 1 criteria to another sheet. At the same time I want all other rows (which do not match the 1 criteria) to be copied into another sheet.
I made it to the point where I can copy the rows of table ORDERS on sheet ORDERS, to the table INSTOCKORDERS on sheet INSTOCKORDERS. But what I really want is to make two different sheets out of all the orders. One with the items that are ordered and are in stock. And another sheet with the orders/items that are NOT in stock.
After this action took place, it should empty the ORDERS-table. And after that, the next time it should place the ORDERS into the tables INSTOCKORDERS and NOSTOCKORDERS below their last row.
What I meant to do with the sorting is to put all the items with a stockquantity of 0 on top of the table, which I thought could make it easier to copy all the ones with 0 items to NOSTOCKORDERS and all the other ones to INSTOCKORDERS. But I have no clue on how to make this a neat working action let alone, how to start (sorry for my beginners ignorance...) I went through some former questions, ran into AutoFilter, but didn't get it...
Sub CopyOrders()
'Sorting column STOCK in ORDERS from a-z
Worksheets("Orders").ListObjects("Orders").Sort. _
SortFields.Clear
Worksheets("Orders").ListObjects("Orders").Sort. _
SortFields.Add2 Key:=Range("Orders[[#All],[STOCK]]"), SortOn:= _
xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With Worksheets("Orders").ListObjects("Orders").Sort
.Apply
End With
'Copying the table ORDERS to INSTOCKORDERS
Range("Orders").Copy Range("InStockOrders")
End Sub
excel vba copy rows
excel vba copy rows
edited Nov 8 at 11:45
Michal
837920
837920
asked Nov 8 at 11:29
Ekfa
35
35
Why not using the INDEX, MATCH pair i/o VBA ?
– hornetbzz
Nov 8 at 13:16
add a comment |
Why not using the INDEX, MATCH pair i/o VBA ?
– hornetbzz
Nov 8 at 13:16
Why not using the INDEX, MATCH pair i/o VBA ?
– hornetbzz
Nov 8 at 13:16
Why not using the INDEX, MATCH pair i/o VBA ?
– hornetbzz
Nov 8 at 13:16
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
You are right, the AutoFilter function is what you want. Autofilter needs a criteria to filter for, and then needs to be told which column (field) to find the criteria in. Then you need to copy the visible cells remaining, after you filter. In the code below, replace the Workbooks, Worksheets, and Range with what you need for your workbook:
Sub FilterNoStock()
Workbooks.Worksheets.Range.AutoFilter Field=5, Criteria1:="0", VisibleDropDown:=True
Workbooks.Worksheets.Range.SpecialCells(xlCellTypeVisible).Copy
End Sub
In the same way, you can filter for all but 1 value:
Sub FilterInStock()
Workbooks.Worksheets.Range.AutoFilter Field=5, Criteria1:="<>" & "0", VisibleDropDown:=True
Workbooks.Worksheets.Range.SpecialCells(xlCellTypeVisible).Copy
End Sub
I would also recommend pairing this with the Rows.Count function to be able to dynamically copy only the rows that contain data, and paste it at the end of your lists in your InStock and NoStock sheets. With the Rows.Count method, you make VBA count how many rows you have, set that to a variable, then use that variable to declare the length of your range. Right now you are copying the entire worksheet, which is going to be problematic if you are trying to compile multiple orders in your InStock and NoStock sheets, as pasting will overwrite all your data from previous orders: Description of Rows.Count
add a comment |
up vote
0
down vote
Thanks to Curtis000 it's getting somewhere. I now have a piece of VBA which makes a copy of items which are in stock. It gets copied to the sheet "InStockOrders" to the table with the same name. However there's one minor problem going on.
The firts 5 columns are copied with the right values in it. But the 6th column and further get the values copied which belong to the first to rows of my table "ORDERS".
Sub FilterInStock()
Worksheets("Orders").Range("B2,I2").AutoFilter Field:=6, Criteria1:="<>" & "0", VisibleDropDown:=True
Worksheets("Orders").Range("ORDERS").SpecialCells(xlCellTypeVisible).Copy Range("InStockOrders")
If ActiveSheet.FilterMode Then
ActiveSheet.ShowAllData
End If
End Sub
But the table NoStockOrders has a perfectly right copy of the ones that should be there. Up until now I'm running the two VBA's separately. In this sample: first the FilterInStock and then the FilterNoStock.
Sub FilterNoStock()
Worksheets("Orders").Range("B2,I2").AutoFilter Field:=6, Criteria1:="0", VisibleDropDown:=True
Worksheets("Orders").Range("ORDERS").SpecialCells(xlCellTypeVisible).Copy Range("NoStockOrders")
If ActiveSheet.FilterMode Then
ActiveSheet.ShowAllData
End If
End Sub
Orders-table
InStockOrders-table
NoStockOrders-table
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
You are right, the AutoFilter function is what you want. Autofilter needs a criteria to filter for, and then needs to be told which column (field) to find the criteria in. Then you need to copy the visible cells remaining, after you filter. In the code below, replace the Workbooks, Worksheets, and Range with what you need for your workbook:
Sub FilterNoStock()
Workbooks.Worksheets.Range.AutoFilter Field=5, Criteria1:="0", VisibleDropDown:=True
Workbooks.Worksheets.Range.SpecialCells(xlCellTypeVisible).Copy
End Sub
In the same way, you can filter for all but 1 value:
Sub FilterInStock()
Workbooks.Worksheets.Range.AutoFilter Field=5, Criteria1:="<>" & "0", VisibleDropDown:=True
Workbooks.Worksheets.Range.SpecialCells(xlCellTypeVisible).Copy
End Sub
I would also recommend pairing this with the Rows.Count function to be able to dynamically copy only the rows that contain data, and paste it at the end of your lists in your InStock and NoStock sheets. With the Rows.Count method, you make VBA count how many rows you have, set that to a variable, then use that variable to declare the length of your range. Right now you are copying the entire worksheet, which is going to be problematic if you are trying to compile multiple orders in your InStock and NoStock sheets, as pasting will overwrite all your data from previous orders: Description of Rows.Count
add a comment |
up vote
0
down vote
accepted
You are right, the AutoFilter function is what you want. Autofilter needs a criteria to filter for, and then needs to be told which column (field) to find the criteria in. Then you need to copy the visible cells remaining, after you filter. In the code below, replace the Workbooks, Worksheets, and Range with what you need for your workbook:
Sub FilterNoStock()
Workbooks.Worksheets.Range.AutoFilter Field=5, Criteria1:="0", VisibleDropDown:=True
Workbooks.Worksheets.Range.SpecialCells(xlCellTypeVisible).Copy
End Sub
In the same way, you can filter for all but 1 value:
Sub FilterInStock()
Workbooks.Worksheets.Range.AutoFilter Field=5, Criteria1:="<>" & "0", VisibleDropDown:=True
Workbooks.Worksheets.Range.SpecialCells(xlCellTypeVisible).Copy
End Sub
I would also recommend pairing this with the Rows.Count function to be able to dynamically copy only the rows that contain data, and paste it at the end of your lists in your InStock and NoStock sheets. With the Rows.Count method, you make VBA count how many rows you have, set that to a variable, then use that variable to declare the length of your range. Right now you are copying the entire worksheet, which is going to be problematic if you are trying to compile multiple orders in your InStock and NoStock sheets, as pasting will overwrite all your data from previous orders: Description of Rows.Count
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
You are right, the AutoFilter function is what you want. Autofilter needs a criteria to filter for, and then needs to be told which column (field) to find the criteria in. Then you need to copy the visible cells remaining, after you filter. In the code below, replace the Workbooks, Worksheets, and Range with what you need for your workbook:
Sub FilterNoStock()
Workbooks.Worksheets.Range.AutoFilter Field=5, Criteria1:="0", VisibleDropDown:=True
Workbooks.Worksheets.Range.SpecialCells(xlCellTypeVisible).Copy
End Sub
In the same way, you can filter for all but 1 value:
Sub FilterInStock()
Workbooks.Worksheets.Range.AutoFilter Field=5, Criteria1:="<>" & "0", VisibleDropDown:=True
Workbooks.Worksheets.Range.SpecialCells(xlCellTypeVisible).Copy
End Sub
I would also recommend pairing this with the Rows.Count function to be able to dynamically copy only the rows that contain data, and paste it at the end of your lists in your InStock and NoStock sheets. With the Rows.Count method, you make VBA count how many rows you have, set that to a variable, then use that variable to declare the length of your range. Right now you are copying the entire worksheet, which is going to be problematic if you are trying to compile multiple orders in your InStock and NoStock sheets, as pasting will overwrite all your data from previous orders: Description of Rows.Count
You are right, the AutoFilter function is what you want. Autofilter needs a criteria to filter for, and then needs to be told which column (field) to find the criteria in. Then you need to copy the visible cells remaining, after you filter. In the code below, replace the Workbooks, Worksheets, and Range with what you need for your workbook:
Sub FilterNoStock()
Workbooks.Worksheets.Range.AutoFilter Field=5, Criteria1:="0", VisibleDropDown:=True
Workbooks.Worksheets.Range.SpecialCells(xlCellTypeVisible).Copy
End Sub
In the same way, you can filter for all but 1 value:
Sub FilterInStock()
Workbooks.Worksheets.Range.AutoFilter Field=5, Criteria1:="<>" & "0", VisibleDropDown:=True
Workbooks.Worksheets.Range.SpecialCells(xlCellTypeVisible).Copy
End Sub
I would also recommend pairing this with the Rows.Count function to be able to dynamically copy only the rows that contain data, and paste it at the end of your lists in your InStock and NoStock sheets. With the Rows.Count method, you make VBA count how many rows you have, set that to a variable, then use that variable to declare the length of your range. Right now you are copying the entire worksheet, which is going to be problematic if you are trying to compile multiple orders in your InStock and NoStock sheets, as pasting will overwrite all your data from previous orders: Description of Rows.Count
answered Nov 8 at 14:47
Curtis000
936
936
add a comment |
add a comment |
up vote
0
down vote
Thanks to Curtis000 it's getting somewhere. I now have a piece of VBA which makes a copy of items which are in stock. It gets copied to the sheet "InStockOrders" to the table with the same name. However there's one minor problem going on.
The firts 5 columns are copied with the right values in it. But the 6th column and further get the values copied which belong to the first to rows of my table "ORDERS".
Sub FilterInStock()
Worksheets("Orders").Range("B2,I2").AutoFilter Field:=6, Criteria1:="<>" & "0", VisibleDropDown:=True
Worksheets("Orders").Range("ORDERS").SpecialCells(xlCellTypeVisible).Copy Range("InStockOrders")
If ActiveSheet.FilterMode Then
ActiveSheet.ShowAllData
End If
End Sub
But the table NoStockOrders has a perfectly right copy of the ones that should be there. Up until now I'm running the two VBA's separately. In this sample: first the FilterInStock and then the FilterNoStock.
Sub FilterNoStock()
Worksheets("Orders").Range("B2,I2").AutoFilter Field:=6, Criteria1:="0", VisibleDropDown:=True
Worksheets("Orders").Range("ORDERS").SpecialCells(xlCellTypeVisible).Copy Range("NoStockOrders")
If ActiveSheet.FilterMode Then
ActiveSheet.ShowAllData
End If
End Sub
Orders-table
InStockOrders-table
NoStockOrders-table
add a comment |
up vote
0
down vote
Thanks to Curtis000 it's getting somewhere. I now have a piece of VBA which makes a copy of items which are in stock. It gets copied to the sheet "InStockOrders" to the table with the same name. However there's one minor problem going on.
The firts 5 columns are copied with the right values in it. But the 6th column and further get the values copied which belong to the first to rows of my table "ORDERS".
Sub FilterInStock()
Worksheets("Orders").Range("B2,I2").AutoFilter Field:=6, Criteria1:="<>" & "0", VisibleDropDown:=True
Worksheets("Orders").Range("ORDERS").SpecialCells(xlCellTypeVisible).Copy Range("InStockOrders")
If ActiveSheet.FilterMode Then
ActiveSheet.ShowAllData
End If
End Sub
But the table NoStockOrders has a perfectly right copy of the ones that should be there. Up until now I'm running the two VBA's separately. In this sample: first the FilterInStock and then the FilterNoStock.
Sub FilterNoStock()
Worksheets("Orders").Range("B2,I2").AutoFilter Field:=6, Criteria1:="0", VisibleDropDown:=True
Worksheets("Orders").Range("ORDERS").SpecialCells(xlCellTypeVisible).Copy Range("NoStockOrders")
If ActiveSheet.FilterMode Then
ActiveSheet.ShowAllData
End If
End Sub
Orders-table
InStockOrders-table
NoStockOrders-table
add a comment |
up vote
0
down vote
up vote
0
down vote
Thanks to Curtis000 it's getting somewhere. I now have a piece of VBA which makes a copy of items which are in stock. It gets copied to the sheet "InStockOrders" to the table with the same name. However there's one minor problem going on.
The firts 5 columns are copied with the right values in it. But the 6th column and further get the values copied which belong to the first to rows of my table "ORDERS".
Sub FilterInStock()
Worksheets("Orders").Range("B2,I2").AutoFilter Field:=6, Criteria1:="<>" & "0", VisibleDropDown:=True
Worksheets("Orders").Range("ORDERS").SpecialCells(xlCellTypeVisible).Copy Range("InStockOrders")
If ActiveSheet.FilterMode Then
ActiveSheet.ShowAllData
End If
End Sub
But the table NoStockOrders has a perfectly right copy of the ones that should be there. Up until now I'm running the two VBA's separately. In this sample: first the FilterInStock and then the FilterNoStock.
Sub FilterNoStock()
Worksheets("Orders").Range("B2,I2").AutoFilter Field:=6, Criteria1:="0", VisibleDropDown:=True
Worksheets("Orders").Range("ORDERS").SpecialCells(xlCellTypeVisible).Copy Range("NoStockOrders")
If ActiveSheet.FilterMode Then
ActiveSheet.ShowAllData
End If
End Sub
Orders-table
InStockOrders-table
NoStockOrders-table
Thanks to Curtis000 it's getting somewhere. I now have a piece of VBA which makes a copy of items which are in stock. It gets copied to the sheet "InStockOrders" to the table with the same name. However there's one minor problem going on.
The firts 5 columns are copied with the right values in it. But the 6th column and further get the values copied which belong to the first to rows of my table "ORDERS".
Sub FilterInStock()
Worksheets("Orders").Range("B2,I2").AutoFilter Field:=6, Criteria1:="<>" & "0", VisibleDropDown:=True
Worksheets("Orders").Range("ORDERS").SpecialCells(xlCellTypeVisible).Copy Range("InStockOrders")
If ActiveSheet.FilterMode Then
ActiveSheet.ShowAllData
End If
End Sub
But the table NoStockOrders has a perfectly right copy of the ones that should be there. Up until now I'm running the two VBA's separately. In this sample: first the FilterInStock and then the FilterNoStock.
Sub FilterNoStock()
Worksheets("Orders").Range("B2,I2").AutoFilter Field:=6, Criteria1:="0", VisibleDropDown:=True
Worksheets("Orders").Range("ORDERS").SpecialCells(xlCellTypeVisible).Copy Range("NoStockOrders")
If ActiveSheet.FilterMode Then
ActiveSheet.ShowAllData
End If
End Sub
Orders-table
InStockOrders-table
NoStockOrders-table
edited Nov 11 at 14:20
answered Nov 11 at 11:34
Ekfa
35
35
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206850%2fcopy-rows-which-match-criteria-into-two-or-more-different-sheets-in-excel-with-v%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Why not using the INDEX, MATCH pair i/o VBA ?
– hornetbzz
Nov 8 at 13:16