+ Reply to Thread
Results 1 to 14 of 14

Macro to paste values to a new sheet based on a condition

  1. #1
    Registered User
    Join Date
    04-24-2017
    Location
    USA
    MS-Off Ver
    365
    Posts
    83

    Macro to paste values to a new sheet based on a condition

    Hello All,

    I'm looking for a way to create a macro I can run across various workbooks that are used for inventory tracking and management. Each workbook has numerous tabs with various names that are based on dates of inventory purchase. First, I'm not sure if there is a way to make the code read "do this for all sheets in this workbook, regardless of the tab name". Usually codes reference a specific sheet, but my programming skills are at a very beginner level and there may be a way to do it. Possibly this, which I found on a Microsoft website,

    Sub WorksheetLoop()

    Dim WS_Count As Integer
    Dim I As Integer

    ' Set WS_Count equal to the number of worksheets in the active
    ' workbook.
    WS_Count = ActiveWorkbook.Worksheets.Count

    ' Begin the loop.
    For I = 1 To WS_Count

    But I'm not sure if that will achieve what I am looking for.

    So basically all I want it to do it to look in a column for a specific value, in my case I'm looking for the year the inventory has sold. If any cell in the column is the current year, I want it to paste the entire row into a new sheet. Currently when I need to create a YTD sales spreadsheet I just go through each tab of each workbook by hand, filter the year sold column to the current year, and copy/paste the values over to the new sheet.

    So in plain English:
    For all sheets in this workbook, if the value in any cell in the year sold column equals 2019, then copy and paste the entire row into a new sheet, and at the end of all of the pasting of these value, sum each column of sales price, cost, net profit.

    Any ideas on how I can create a macro to handle this for me?

    Thanks in advance

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,815

    Re: Macro to paste values to a new sheet based on a condition

    This is certainly possible. It would be easier to help if you could attach a copy of your destination workbook and at least one copy of your source files. We would also need the full path to the folder where the source files are saved.
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    365 Pro Plus
    Posts
    2,274

    Re: Macro to paste values to a new sheet based on a condition

    Assuming the data in the worksheets has a header in row 1, possibly...
    Please Login or Register  to view this content.
    Last edited by dangelor; 07-31-2019 at 03:54 PM.

  4. #4
    Registered User
    Join Date
    04-24-2017
    Location
    USA
    MS-Off Ver
    365
    Posts
    83

    Re: Macro to paste values to a new sheet based on a condition

    Mumps, the destination workbook would just be the same file for each year. I have files from 2016 through 2019. I just want to have a macro embedded in my personal.xlsx that I can run on any file that I open. I currently don't have the files on the computer I'm working on now.

    dangelor, the files do indeed have headers on each worksheet that are matching across all files. I'll try that code and see if I can get it to work.

    Thanks

  5. #5
    Registered User
    Join Date
    04-24-2017
    Location
    USA
    MS-Off Ver
    365
    Posts
    83

    Re: Macro to paste values to a new sheet based on a condition

    Quote Originally Posted by dangelor View Post
    Assuming the data in the worksheets has a header in row 1, possibly...
    Please Login or Register  to view this content.
    dangelor,

    this is running into an error. .AutoFilter field:=YrSoldCol.Column, Criteria1:="=2019" says object variable or With block variable not set.

    How do I upload a copy of a spreadsheet here? That way you can have something to work off of.

    Thanks

  6. #6
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    365 Pro Plus
    Posts
    2,274

    Re: Macro to paste values to a new sheet based on a condition

    this is running into an error
    Is there a column heading of "Year Sold"?

    How do I upload a copy of a spreadsheet here?
    Click on the "Go Advanced" button and then Manage Attachments.

  7. #7
    Registered User
    Join Date
    04-24-2017
    Location
    USA
    MS-Off Ver
    365
    Posts
    83

    Re: Macro to paste values to a new sheet based on a condition

    I think the file should be attached now
    Last edited by the machine; 08-07-2019 at 08:24 AM.

  8. #8
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    365 Pro Plus
    Posts
    2,274

    Re: Macro to paste values to a new sheet based on a condition

    You have worksheets that do not have a 'Year Sold' column. Remove those worksheets and run the code.

  9. #9
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb

    Hi !

    No need to delete anything but just check if expected column exists …

  10. #10
    Registered User
    Join Date
    04-24-2017
    Location
    USA
    MS-Off Ver
    365
    Posts
    83

    Re: Macro to paste values to a new sheet based on a condition

    I meant to upload just one tab dumbed down so it is less to look at. I just added a clean file of just that sheet with only 5 columns. I'm still getting the same error
    Attached Files Attached Files

  11. #11
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    365 Pro Plus
    Posts
    2,274

    Re: Macro to paste values to a new sheet based on a condition

    It worked for me....
    Attached Files Attached Files

  12. #12
    Registered User
    Join Date
    04-24-2017
    Location
    USA
    MS-Off Ver
    365
    Posts
    83

    Re: Macro to paste values to a new sheet based on a condition

    Does it matter where this Macro is stored? I have it in my PERSONAL.XLSX workbook.

  13. #13
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    365 Pro Plus
    Posts
    2,274

    Re: Macro to paste values to a new sheet based on a condition

    The code is written for the workbook containing the code. As it is, if you run it from your personal.xlsb workbook, it will attempt to manipulate the data in the personal.xlsb workbook. To run it from you personal.xlsb workbook, you will need to change any reference to ThisWorkbook to either ActiveWorkbook or the the name of the workbook you want to manipulate; e.g.,Workbooks("Report.xlsx").

  14. #14
    Registered User
    Join Date
    04-24-2017
    Location
    USA
    MS-Off Ver
    365
    Posts
    83

    Re: Macro to paste values to a new sheet based on a condition

    Thanks, that is why it isn't working properly. I'll add the macro to each workbook and run it.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 5
    Last Post: 11-05-2018, 06:25 AM
  2. [SOLVED] Paste values based on specified condition
    By charm1 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-25-2017, 01:08 PM
  3. Macro to copy values on a sheet based on a condition
    By mcane in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-05-2017, 08:26 PM
  4. Replies: 5
    Last Post: 11-07-2016, 07:49 AM
  5. [SOLVED] Paste values based on a condition to next availble row in different sheet
    By Jenkins87 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-28-2016, 04:00 PM
  6. [SOLVED] Copy and paste (values) to another sheet based on condition
    By KK1234 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-09-2013, 10:25 PM
  7. Copy/Paste from one sheet to another based on condition
    By TheTempest in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-16-2010, 01:13 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1