+ Reply to Thread
Results 1 to 4 of 4

Pulling rows of data using VBA

Hybrid View

soccer4ard Pulling rows of data using VBA 02-08-2018, 12:40 PM
Mumps1 Re: Pulling rows of data... 02-08-2018, 02:52 PM
soccer4ard Re: Pulling rows of data... 02-08-2018, 03:08 PM
JLGWhiz Re: Pulling rows of data... 02-08-2018, 03:09 PM
  1. #1
    Registered User
    Join Date
    10-15-2014
    Location
    Michigan, US
    MS-Off Ver
    2010
    Posts
    5

    Pulling rows of data using VBA

    Hello,

    I'm looking for assistance in the following matter:

    I have an employee list that contains many columns. One of the columns is “Department”
    Each department has separate workbooks. Each week, I go to the employee list and filter it by a specific department and copy filtered rows into the respective department workbook. I was wondering if anyone is able to provide me a template of code for each workbook that would pull those rows automatically from the employee list.

    I know just a bit of VBA (taking classes next semester to learn more) but if you could notate pieces of code and what they do…that would be helpful just for me to keep learning and figuring out how things work.

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

    Re: Pulling rows of data using VBA

    What is the full path of the folder that contains the department workbooks? Are these the only files in that folder? I assume that the department workbook names contain the name of the department from the “Department” column. Can you gives us a sample department name and the name of the corresponding department workbook including the extension (xls, xlsx, xlsm)? What is the column letter of the column that contains the “Department”? Do you want to append each week's data to the bottom of the existing data in each department workbook?
    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
    Registered User
    Join Date
    10-15-2014
    Location
    Michigan, US
    MS-Off Ver
    2010
    Posts
    5

    Re: Pulling rows of data using VBA

    all the department workbooks are in one folder on the same drive as the employee list. Department workbooks names do not necessarily match departments in the department column. However, each department workbook, contains the names of the departments that I want to match and pull in the employee list. My plan was to use it as a look up table.

    For an example, my inventory workbook will contain a range or a table that lists inventory that should be in this workbook. (e.g. batteries, tires). I need VBA code in this workbook to look at the lookup table (it will be the same range in each department workbook) and pull rows from the employee list that contain batteries and/or tires in the Department column. Department column is $G. I do not want new data to be added to the bottom. Instead, I would like old data to be deleted and new data to be added over it.

    Great questions. I didn't think about providing that info...oopsies.

    Does that make sense?

  4. #4
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Pulling rows of data using VBA

    To use VBA efficiently, the database should be constructed for consistency, eg. the headers will be fixed in their columns and rows and will be the same (when possible) between workbooks. This will allow rows of data to be copied from sheets in one workbook to another workbook without having to rearrange the data to accomodate headers. If you are only copying data from specific columns, as opposed to entire rows, and the data is not found in contiguous cells, but are to be pasted to cells under the same headers in the destination workbook, then the consistency in header locations will make it easier to do the copy and paste in code. That said, for yur project, assume the Department number or Name is in column A of the source workbook, the Department workbooks are named exactly as they are listedin column A of the source workbook, and sheet 1 is used for the data in both the sourc and destination workbooks. The code assumes the source workbook as host for the code. It also assumes that the directory path for all workbooks are the same.
    Sub t()
    Dim wb As Workbook, sh As Worksheet, c As Range, fPath As String
    Set sh = ThisWorkbook.Sheets(1)
    fPath = ThisWorkbook.Path
    If Right(fPath, 1) <> "\" Then fPath = fPath & "\"
    sh.Range("A1", sh.Cells(Rows.Count, 1).End(xlUp)).AdvancedFilter xlFilterCopy, , sh.Cells(Rows.Count, 2).End(xlUp)(3), True
        For Each c In sh.sh.Cells(Rows.Count, 2).End(xlUp).CurrentRegion.Offset(1) 'inialalize For loop in column A Unique Dept.
            If c <> "" Then 'Prevents error message for blank cells
                sh.Range("A2", sh.Cells(Row.Count, 1).End(xlUp)).AutoFilter 1, c.Value 'Filter by department
                Set wb = Workbooks.Open(fPath & c.Value & ".xlsx") 'Open department workbook assuming not macro enabled
                'copy the visible rows, excluding headers, to the next available row in destination workbook.
                sh.UsedRange.Offset(1).SpecialCells(xlCellTypeVisible).Copy wb.Sheets(1).Cells(Rows.Count, 1).End(xlUp)(2)
                sh.AutoFilterMode = False 'turns off autofilter
                wb.Close 'close department workbook
            End If
        Next 'increment loop by one cell
    sh.Cells(Rows.Count, 2).End(xlUp).CurrentRegion.ClearContents 'Clears the list of unique department names
    End Sub
    so if all assumtions hold, then this template would work.
    Any code provided by me should be tested on a copy or a mock up of your original data before applying it to the original. Some events in VBA cannot be reversed with the undo facility in Excel. If your original post is satisfied, please mark the thread as "Solved". To upload a file, see the banner at top of this page.
    Just when I think I am smart, I learn something new!

+ 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. [SOLVED] Pulling rows of data from one sheet within a workbook to another
    By tomfohr in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 09-21-2015, 11:45 AM
  2. [SOLVED] Pulling rows of data - leave it blank if there is no data to pull
    By Terisammis in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-06-2014, 10:17 AM
  3. Chart pulling data from rows based on a particular value
    By mikehelvey in forum Excel General
    Replies: 0
    Last Post: 06-15-2012, 06:40 AM
  4. Pulling Data from Filtered Rows on One Tab into another
    By Joe Doucette in forum Excel Programming / VBA / Macros
    Replies: 24
    Last Post: 03-25-2012, 08:22 PM
  5. Pulling specific rows from a data set
    By ExMAN999 in forum Excel General
    Replies: 7
    Last Post: 10-29-2011, 05:55 PM
  6. Pulling Data From Columns into Rows
    By Office Idiot in forum Excel General
    Replies: 1
    Last Post: 07-07-2010, 05:35 PM
  7. Pulling individual rows out of repetitive data.
    By Onestopfanshop in forum Excel General
    Replies: 2
    Last Post: 05-13-2009, 12:03 PM

Tags for this Thread

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