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











share|improve this question
























  • Why not using the INDEX, MATCH pair i/o VBA ?
    – hornetbzz
    Nov 8 at 13:16















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











share|improve this question
























  • Why not using the INDEX, MATCH pair i/o VBA ?
    – hornetbzz
    Nov 8 at 13:16













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











share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 11:45









Michal

9261020




9261020










asked Nov 8 at 11:29









Ekfa

106




106












  • 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




Why not using the INDEX, MATCH pair i/o VBA ?
– hornetbzz
Nov 8 at 13:16












3 Answers
3






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






share|improve this answer





















  • Thanks for your start-up. I'm still working on this. But your first hint gave me a go.
    – Ekfa
    Nov 28 at 9:34










  • The problem I'm having now, is that it copies the rows to the right sheet. But it's being copied with the formulas in the cells. This means all references are copied and so in my sheets "...StockOrders" it keeps looking at the reference table I made where the "stock" is at. So it does copy the right rows, but as a result of the formulas it shows the wrong data... How can I get it to copy the values, instead of the formulas?
    – Ekfa
    Nov 28 at 10:01










  • You should be able to get rid of the formulas by using PasteSpecial xlPasteValues
    – Curtis000
    Nov 28 at 19:47


















up vote
0
down vote













Update:
Answer 2:
I think I found the error in all this. What is does so far, is copying the right rows at that moment. But it copies with the formulas! This means when the original table (Orders) is being sorted in a different way, or rows get deleted or anything else, the copied rows in "...StockOrders" are showing different values because of there formulas!
What I need is a little help... How to copy the values instead of the formulas?



Answer 1:
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






share|improve this answer






























    up vote
    0
    down vote













    Finaly got a good working system.



        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

    'Delete all rows from table NoStockOrders
    On Error Resume Next
    Worksheets("NoStockOrders").ListObjects("NoStockOrders").DataBodyRange.EntireRow.Delete

    'Copy all orders which have no stock to
    'the sheet NoStockOrders
    Worksheets("Orders").Range("ORDERS").AutoFilter _
    Field:=6, Criteria1:="0", VisibleDropDown:=True
    On Error Resume Next
    Worksheets("Orders").Range("ORDERS").SpecialCells _
    (xlCellTypeVisible).Copy
    Range("NoStockOrders").PasteSpecial _
    Paste:=xlPasteValues

    If Worksheets("Orders").ListObjects("Orders").FilterMode Then
    Worksheets("Orders").AutoFilter.ShowAllData
    End If

    'Sorting column STOCK in ORDERS from Z-A
    Worksheets("Orders").ListObjects("Orders").Sort. _
    SortFields.Clear
    Worksheets("Orders").ListObjects("Orders").Sort. _
    SortFields.Add2 Key:=Range("Orders[[#All],[STOCK]]"), SortOn:= _
    xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With Worksheets("Orders").ListObjects("Orders").Sort
    .Apply
    End With

    'Delete all rows from table InStockOrders
    On Error Resume Next
    Worksheets("InStockOrders").ListObjects("InStockOrders").DataBodyRange.EntireRow.Delete

    'Copy all orders which have no stock to
    'the sheet InStockOrders
    Worksheets("Orders").Range("ORDERS").AutoFilter _
    Field:=6, Criteria1:=">0", VisibleDropDown:=True
    On Error Resume Next
    Worksheets("Orders").Range("ORDERS").SpecialCells _
    (xlCellTypeVisible).Copy
    Range("InStockOrders").PasteSpecial _
    Paste:=xlPasteValues

    If Worksheets("Orders").FilterMode Then
    Worksheets("Orders").ShowAllData
    End If

    'Copying the table ORDERS to INSTOCKORDERS
    'Range("Orders").Copy Range("InStockOrders")

    End Sub


    Keep in mind: running this from outside the Orders-table doesn't work properly! It won't clear the filtermode in the table then. (follow my new question: enter link description here)






    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      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

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      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






      share|improve this answer





















      • Thanks for your start-up. I'm still working on this. But your first hint gave me a go.
        – Ekfa
        Nov 28 at 9:34










      • The problem I'm having now, is that it copies the rows to the right sheet. But it's being copied with the formulas in the cells. This means all references are copied and so in my sheets "...StockOrders" it keeps looking at the reference table I made where the "stock" is at. So it does copy the right rows, but as a result of the formulas it shows the wrong data... How can I get it to copy the values, instead of the formulas?
        – Ekfa
        Nov 28 at 10:01










      • You should be able to get rid of the formulas by using PasteSpecial xlPasteValues
        – Curtis000
        Nov 28 at 19:47















      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






      share|improve this answer





















      • Thanks for your start-up. I'm still working on this. But your first hint gave me a go.
        – Ekfa
        Nov 28 at 9:34










      • The problem I'm having now, is that it copies the rows to the right sheet. But it's being copied with the formulas in the cells. This means all references are copied and so in my sheets "...StockOrders" it keeps looking at the reference table I made where the "stock" is at. So it does copy the right rows, but as a result of the formulas it shows the wrong data... How can I get it to copy the values, instead of the formulas?
        – Ekfa
        Nov 28 at 10:01










      • You should be able to get rid of the formulas by using PasteSpecial xlPasteValues
        – Curtis000
        Nov 28 at 19:47













      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






      share|improve this answer












      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







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 8 at 14:47









      Curtis000

      936




      936












      • Thanks for your start-up. I'm still working on this. But your first hint gave me a go.
        – Ekfa
        Nov 28 at 9:34










      • The problem I'm having now, is that it copies the rows to the right sheet. But it's being copied with the formulas in the cells. This means all references are copied and so in my sheets "...StockOrders" it keeps looking at the reference table I made where the "stock" is at. So it does copy the right rows, but as a result of the formulas it shows the wrong data... How can I get it to copy the values, instead of the formulas?
        – Ekfa
        Nov 28 at 10:01










      • You should be able to get rid of the formulas by using PasteSpecial xlPasteValues
        – Curtis000
        Nov 28 at 19:47


















      • Thanks for your start-up. I'm still working on this. But your first hint gave me a go.
        – Ekfa
        Nov 28 at 9:34










      • The problem I'm having now, is that it copies the rows to the right sheet. But it's being copied with the formulas in the cells. This means all references are copied and so in my sheets "...StockOrders" it keeps looking at the reference table I made where the "stock" is at. So it does copy the right rows, but as a result of the formulas it shows the wrong data... How can I get it to copy the values, instead of the formulas?
        – Ekfa
        Nov 28 at 10:01










      • You should be able to get rid of the formulas by using PasteSpecial xlPasteValues
        – Curtis000
        Nov 28 at 19:47
















      Thanks for your start-up. I'm still working on this. But your first hint gave me a go.
      – Ekfa
      Nov 28 at 9:34




      Thanks for your start-up. I'm still working on this. But your first hint gave me a go.
      – Ekfa
      Nov 28 at 9:34












      The problem I'm having now, is that it copies the rows to the right sheet. But it's being copied with the formulas in the cells. This means all references are copied and so in my sheets "...StockOrders" it keeps looking at the reference table I made where the "stock" is at. So it does copy the right rows, but as a result of the formulas it shows the wrong data... How can I get it to copy the values, instead of the formulas?
      – Ekfa
      Nov 28 at 10:01




      The problem I'm having now, is that it copies the rows to the right sheet. But it's being copied with the formulas in the cells. This means all references are copied and so in my sheets "...StockOrders" it keeps looking at the reference table I made where the "stock" is at. So it does copy the right rows, but as a result of the formulas it shows the wrong data... How can I get it to copy the values, instead of the formulas?
      – Ekfa
      Nov 28 at 10:01












      You should be able to get rid of the formulas by using PasteSpecial xlPasteValues
      – Curtis000
      Nov 28 at 19:47




      You should be able to get rid of the formulas by using PasteSpecial xlPasteValues
      – Curtis000
      Nov 28 at 19:47












      up vote
      0
      down vote













      Update:
      Answer 2:
      I think I found the error in all this. What is does so far, is copying the right rows at that moment. But it copies with the formulas! This means when the original table (Orders) is being sorted in a different way, or rows get deleted or anything else, the copied rows in "...StockOrders" are showing different values because of there formulas!
      What I need is a little help... How to copy the values instead of the formulas?



      Answer 1:
      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






      share|improve this answer



























        up vote
        0
        down vote













        Update:
        Answer 2:
        I think I found the error in all this. What is does so far, is copying the right rows at that moment. But it copies with the formulas! This means when the original table (Orders) is being sorted in a different way, or rows get deleted or anything else, the copied rows in "...StockOrders" are showing different values because of there formulas!
        What I need is a little help... How to copy the values instead of the formulas?



        Answer 1:
        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






        share|improve this answer

























          up vote
          0
          down vote










          up vote
          0
          down vote









          Update:
          Answer 2:
          I think I found the error in all this. What is does so far, is copying the right rows at that moment. But it copies with the formulas! This means when the original table (Orders) is being sorted in a different way, or rows get deleted or anything else, the copied rows in "...StockOrders" are showing different values because of there formulas!
          What I need is a little help... How to copy the values instead of the formulas?



          Answer 1:
          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






          share|improve this answer














          Update:
          Answer 2:
          I think I found the error in all this. What is does so far, is copying the right rows at that moment. But it copies with the formulas! This means when the original table (Orders) is being sorted in a different way, or rows get deleted or anything else, the copied rows in "...StockOrders" are showing different values because of there formulas!
          What I need is a little help... How to copy the values instead of the formulas?



          Answer 1:
          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







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 28 at 10:06

























          answered Nov 11 at 11:34









          Ekfa

          106




          106






















              up vote
              0
              down vote













              Finaly got a good working system.



                  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

              'Delete all rows from table NoStockOrders
              On Error Resume Next
              Worksheets("NoStockOrders").ListObjects("NoStockOrders").DataBodyRange.EntireRow.Delete

              'Copy all orders which have no stock to
              'the sheet NoStockOrders
              Worksheets("Orders").Range("ORDERS").AutoFilter _
              Field:=6, Criteria1:="0", VisibleDropDown:=True
              On Error Resume Next
              Worksheets("Orders").Range("ORDERS").SpecialCells _
              (xlCellTypeVisible).Copy
              Range("NoStockOrders").PasteSpecial _
              Paste:=xlPasteValues

              If Worksheets("Orders").ListObjects("Orders").FilterMode Then
              Worksheets("Orders").AutoFilter.ShowAllData
              End If

              'Sorting column STOCK in ORDERS from Z-A
              Worksheets("Orders").ListObjects("Orders").Sort. _
              SortFields.Clear
              Worksheets("Orders").ListObjects("Orders").Sort. _
              SortFields.Add2 Key:=Range("Orders[[#All],[STOCK]]"), SortOn:= _
              xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
              With Worksheets("Orders").ListObjects("Orders").Sort
              .Apply
              End With

              'Delete all rows from table InStockOrders
              On Error Resume Next
              Worksheets("InStockOrders").ListObjects("InStockOrders").DataBodyRange.EntireRow.Delete

              'Copy all orders which have no stock to
              'the sheet InStockOrders
              Worksheets("Orders").Range("ORDERS").AutoFilter _
              Field:=6, Criteria1:=">0", VisibleDropDown:=True
              On Error Resume Next
              Worksheets("Orders").Range("ORDERS").SpecialCells _
              (xlCellTypeVisible).Copy
              Range("InStockOrders").PasteSpecial _
              Paste:=xlPasteValues

              If Worksheets("Orders").FilterMode Then
              Worksheets("Orders").ShowAllData
              End If

              'Copying the table ORDERS to INSTOCKORDERS
              'Range("Orders").Copy Range("InStockOrders")

              End Sub


              Keep in mind: running this from outside the Orders-table doesn't work properly! It won't clear the filtermode in the table then. (follow my new question: enter link description here)






              share|improve this answer

























                up vote
                0
                down vote













                Finaly got a good working system.



                    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

                'Delete all rows from table NoStockOrders
                On Error Resume Next
                Worksheets("NoStockOrders").ListObjects("NoStockOrders").DataBodyRange.EntireRow.Delete

                'Copy all orders which have no stock to
                'the sheet NoStockOrders
                Worksheets("Orders").Range("ORDERS").AutoFilter _
                Field:=6, Criteria1:="0", VisibleDropDown:=True
                On Error Resume Next
                Worksheets("Orders").Range("ORDERS").SpecialCells _
                (xlCellTypeVisible).Copy
                Range("NoStockOrders").PasteSpecial _
                Paste:=xlPasteValues

                If Worksheets("Orders").ListObjects("Orders").FilterMode Then
                Worksheets("Orders").AutoFilter.ShowAllData
                End If

                'Sorting column STOCK in ORDERS from Z-A
                Worksheets("Orders").ListObjects("Orders").Sort. _
                SortFields.Clear
                Worksheets("Orders").ListObjects("Orders").Sort. _
                SortFields.Add2 Key:=Range("Orders[[#All],[STOCK]]"), SortOn:= _
                xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
                With Worksheets("Orders").ListObjects("Orders").Sort
                .Apply
                End With

                'Delete all rows from table InStockOrders
                On Error Resume Next
                Worksheets("InStockOrders").ListObjects("InStockOrders").DataBodyRange.EntireRow.Delete

                'Copy all orders which have no stock to
                'the sheet InStockOrders
                Worksheets("Orders").Range("ORDERS").AutoFilter _
                Field:=6, Criteria1:=">0", VisibleDropDown:=True
                On Error Resume Next
                Worksheets("Orders").Range("ORDERS").SpecialCells _
                (xlCellTypeVisible).Copy
                Range("InStockOrders").PasteSpecial _
                Paste:=xlPasteValues

                If Worksheets("Orders").FilterMode Then
                Worksheets("Orders").ShowAllData
                End If

                'Copying the table ORDERS to INSTOCKORDERS
                'Range("Orders").Copy Range("InStockOrders")

                End Sub


                Keep in mind: running this from outside the Orders-table doesn't work properly! It won't clear the filtermode in the table then. (follow my new question: enter link description here)






                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Finaly got a good working system.



                      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

                  'Delete all rows from table NoStockOrders
                  On Error Resume Next
                  Worksheets("NoStockOrders").ListObjects("NoStockOrders").DataBodyRange.EntireRow.Delete

                  'Copy all orders which have no stock to
                  'the sheet NoStockOrders
                  Worksheets("Orders").Range("ORDERS").AutoFilter _
                  Field:=6, Criteria1:="0", VisibleDropDown:=True
                  On Error Resume Next
                  Worksheets("Orders").Range("ORDERS").SpecialCells _
                  (xlCellTypeVisible).Copy
                  Range("NoStockOrders").PasteSpecial _
                  Paste:=xlPasteValues

                  If Worksheets("Orders").ListObjects("Orders").FilterMode Then
                  Worksheets("Orders").AutoFilter.ShowAllData
                  End If

                  'Sorting column STOCK in ORDERS from Z-A
                  Worksheets("Orders").ListObjects("Orders").Sort. _
                  SortFields.Clear
                  Worksheets("Orders").ListObjects("Orders").Sort. _
                  SortFields.Add2 Key:=Range("Orders[[#All],[STOCK]]"), SortOn:= _
                  xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
                  With Worksheets("Orders").ListObjects("Orders").Sort
                  .Apply
                  End With

                  'Delete all rows from table InStockOrders
                  On Error Resume Next
                  Worksheets("InStockOrders").ListObjects("InStockOrders").DataBodyRange.EntireRow.Delete

                  'Copy all orders which have no stock to
                  'the sheet InStockOrders
                  Worksheets("Orders").Range("ORDERS").AutoFilter _
                  Field:=6, Criteria1:=">0", VisibleDropDown:=True
                  On Error Resume Next
                  Worksheets("Orders").Range("ORDERS").SpecialCells _
                  (xlCellTypeVisible).Copy
                  Range("InStockOrders").PasteSpecial _
                  Paste:=xlPasteValues

                  If Worksheets("Orders").FilterMode Then
                  Worksheets("Orders").ShowAllData
                  End If

                  'Copying the table ORDERS to INSTOCKORDERS
                  'Range("Orders").Copy Range("InStockOrders")

                  End Sub


                  Keep in mind: running this from outside the Orders-table doesn't work properly! It won't clear the filtermode in the table then. (follow my new question: enter link description here)






                  share|improve this answer












                  Finaly got a good working system.



                      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

                  'Delete all rows from table NoStockOrders
                  On Error Resume Next
                  Worksheets("NoStockOrders").ListObjects("NoStockOrders").DataBodyRange.EntireRow.Delete

                  'Copy all orders which have no stock to
                  'the sheet NoStockOrders
                  Worksheets("Orders").Range("ORDERS").AutoFilter _
                  Field:=6, Criteria1:="0", VisibleDropDown:=True
                  On Error Resume Next
                  Worksheets("Orders").Range("ORDERS").SpecialCells _
                  (xlCellTypeVisible).Copy
                  Range("NoStockOrders").PasteSpecial _
                  Paste:=xlPasteValues

                  If Worksheets("Orders").ListObjects("Orders").FilterMode Then
                  Worksheets("Orders").AutoFilter.ShowAllData
                  End If

                  'Sorting column STOCK in ORDERS from Z-A
                  Worksheets("Orders").ListObjects("Orders").Sort. _
                  SortFields.Clear
                  Worksheets("Orders").ListObjects("Orders").Sort. _
                  SortFields.Add2 Key:=Range("Orders[[#All],[STOCK]]"), SortOn:= _
                  xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
                  With Worksheets("Orders").ListObjects("Orders").Sort
                  .Apply
                  End With

                  'Delete all rows from table InStockOrders
                  On Error Resume Next
                  Worksheets("InStockOrders").ListObjects("InStockOrders").DataBodyRange.EntireRow.Delete

                  'Copy all orders which have no stock to
                  'the sheet InStockOrders
                  Worksheets("Orders").Range("ORDERS").AutoFilter _
                  Field:=6, Criteria1:=">0", VisibleDropDown:=True
                  On Error Resume Next
                  Worksheets("Orders").Range("ORDERS").SpecialCells _
                  (xlCellTypeVisible).Copy
                  Range("InStockOrders").PasteSpecial _
                  Paste:=xlPasteValues

                  If Worksheets("Orders").FilterMode Then
                  Worksheets("Orders").ShowAllData
                  End If

                  'Copying the table ORDERS to INSTOCKORDERS
                  'Range("Orders").Copy Range("InStockOrders")

                  End Sub


                  Keep in mind: running this from outside the Orders-table doesn't work properly! It won't clear the filtermode in the table then. (follow my new question: enter link description here)







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 28 at 13:50









                  Ekfa

                  106




                  106






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      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





















































                      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







                      這個網誌中的熱門文章

                      Post-Redirect-Get with Spring WebFlux and Thymeleaf

                      Xamarin.form Move up view when keyboard appear

                      JBPM : POST request for execute process go wrong