+ Reply to Thread
Results 1 to 14 of 14

Filter data from one sheet to 14 existing sheets in different workbooks

  1. #1
    Registered User
    Join Date
    06-15-2013
    Location
    Israel
    MS-Off Ver
    Excel 2016
    Posts
    38

    Question Filter data from one sheet to 14 existing sheets in different workbooks

    Hi,
    I have a workbook containing information about different student from different classes and sections.
    Column B and C are for Class and section respectively.
    Data to be copied is from column D to column Y. Row on is for headers.
    I need a VBA Code or Macro to copy student information to an existing sheets in different workbooks.

    Each workbook is named using class name and section (ex. (grade Seven A"), Name of sheet is "Marks" for all workbooks.
    Any ideas?

    Note:
    New filtered data will replace the old data in the range C4:X64

    Khalil

  2. #2
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Filter data from one sheet to 14 existing sheets in different workbooks

    Based on your information regarding the data layout I would suggest a macro using autofilter.

    Macro starts by filtering "Master" file using "class" and "section" as filters. The filtered result should be copied.

    The "right" target workbook should be opened, old data deleted and the new data pasted to it.

    The target workbook is then saved and closed.

    Macro loops, sets new autofilter, copies, opens new target workbook .....

    I assume that all target workbooks are placed in the same folder so a macro like that should not be too difficult to write. The only thing I'm unsure about is seeing you location is Israel so I think you read and write "right to left" whereas most European read and write "left to right" and I guess your worksheets in an excel workbook is placed according to the "right to left" rule.

    Alf

  3. #3
    Registered User
    Join Date
    06-15-2013
    Location
    Israel
    MS-Off Ver
    Excel 2016
    Posts
    38

    Re: Filter data from one sheet to 14 existing sheets in different workbooks

    Thanks Alf,
    Yes, my Excel sheet are from right-to-left but the names of columns and rows are the same, they do not change;
    They are exactly as if you European.
    My issue is that I do not know how to write the code or command lines for macros.
    Is it possible that the macros opens the workbooks and delete the old data in the specific range?

    Khalil

  4. #4
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Filter data from one sheet to 14 existing sheets in different workbooks

    This is a macro that opends all files in a specific folder and tries to activate a sheet called "Marks" if this sheet don't excist then macro closes the file without saving and opends the next file in folder. If the sheet "Marks" excists then the range E4:X64 is cleared and the modified file is saved and closed, then macro processes the next file in folder.

    The sFile command will find all files in the specified folder that contains ".xls" + anything after the "s" so ".xls*" will find ".xls" files, ".xlsx" files, ".xlsm" files or ".xlsb" files.

    Please Login or Register  to view this content.
    Instead of having a "hard coded" path to where the file is found (sPath = "C:\Myfiles\") one can have a macro that lets user select the folder where files should be processed.

    Alf
    Last edited by Alf; 12-24-2015 at 06:21 AM.

  5. #5
    Registered User
    Join Date
    06-15-2013
    Location
    Israel
    MS-Off Ver
    Excel 2016
    Posts
    38

    Re: Filter data from one sheet to 14 existing sheets in different workbooks

    Thanks Alf,
    I do understand the logic and some of the lines in the code.
    The code does not include the "Filtering" part in the "Master" worksheet for "Class" and "Section".
    How is it done?
    Can you help please?
    Khalil

  6. #6
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Filter data from one sheet to 14 existing sheets in different workbooks

    The code does not include the "Filtering" part in the "Master" worksheet for "Class" and "Section".
    That is because I don't know the layout of the data in the master file. Setting a filer for "Class" and "Section" one could use somthing like this:

    Please Login or Register  to view this content.
    where the field is a rellative column address based on where your data starts. If it starts in column A then column B is field 2 and c is field 3. But if data starts in column B and filtering should be done in column B and C then field address becomes 1 and 2.

    In order to help you with this code I suggest you upload a file with all class and section names and the general data that is in the master file, then on a sheet show how you wish to filter data and finaly I need to know the file name structure so when filtering is done the right file is opened and the data copied to it.

    Don't forget to remove all sensitive data from the file before uploading.

    Alf

  7. #7
    Registered User
    Join Date
    06-15-2013
    Location
    Israel
    MS-Off Ver
    Excel 2016
    Posts
    38

    Re: Filter data from one sheet to 14 existing sheets in different workbooks

    Hi Alf,
    Attached is the master file with sample artificial data named "AllStudentsMaster.xlsx",
    and one of the target files named "Grade 7 B.xlsm"
    I hope this will help.
    Thanks
    Khalil
    Attached Files Attached Files

  8. #8
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Filter data from one sheet to 14 existing sheets in different workbooks

    Perhaps something like this?

    Please Login or Register  to view this content.
    First of all make a copy of all you original files and put them in a special folder for you to test this macro on!!!!

    So you need to change the string "sPath" to suit your needs.

    Macro starts by looping through all values in column B sheet1, it compaires the value B2:C2 to the value B3:C3, if these are equal then macro just tests net pair of values. When B3:C3 are compaired to B4:C4 these are differnt (C3 is different to C4) so the values B3:C3 gets copied to sheet2. After all different values are found sheet2 gets activated and macro adds values from B column with C colum and add the ".xlsm" exstention in the D column in order to build a string. The first one being "Grade 7 B.xlsm" So based on data in B and C column all file names are build.

    Then a range name "Info" is created, sheet1 activated and an autofiter set up with value from column B as first criteria and C as second. The filterd result is copied. The Offset setting should copy from 1 row below heading and 5 rows to left i.e. column E. This is based on your comments that the files to update should first get rid of old data range E4:X64. But looking at the "Grade 7 B.xlsm" file it seems to me that data starts at column E row 14??

    After opening the workbook and sheet "Marks" gets activated, macro clears range E14:X64 and pastes in the new data to range E14 and saves and closes workbook. Then "Thisworkbook" (i.e. "AllStudentsMaster") get activated and the next values in "Info" range gets fed into the autofilter, the nex file opened and so forth...

    Having gone through all data in "Info" sheet2 gets activated, data cleared and range name "Info" deleted, sheet1 activated and original autofilter setting reset.

    Alf

  9. #9
    Registered User
    Join Date
    06-15-2013
    Location
    Israel
    MS-Off Ver
    Excel 2016
    Posts
    38

    Re: Filter data from one sheet to 14 existing sheets in different workbooks

    Hi Alf,

    thanks for the code,
    I think it needs to be adjusted.
    Below is the code with some notes when it was executed.

    Option Explicit

    Sub ExtrData()
    Dim cell As Range
    Dim i As Integer
    Dim sFile As String
    Dim sPath As String
    Dim wb As Workbook

    sPath = "C:\Stony\"

    Application.ScreenUpdating = False
    i = 2

    Sheets("Sheet1").Activate

    For Each cell In Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row)

    If cell & cell.Offset(0, 1) = cell.Offset(1, 0) & cell.Offset(1, 1) Then
    Else
    Range(cell, cell.Offset(0, 1)).Copy
    Sheet2.Range("B" & i).PasteSpecial xlPasteValues
    Application.CutCopyMode = False
    i = i + 1
    End If

    Next

    Sheets("Sheet2").Activate

    For Each cell In Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row)

    cell.Offset(0, 2) = cell & " " & cell.Offset(0, 1) & ".xlsm"

    Next

    Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row).Name = "Info"

    Application.ScreenUpdating = True

    Sheets("Sheet1").Activate

    For Each cell In Range("Info")
    sFile = cell.Offset(0, 2)

    With ActiveSheet
    .AutoFilterMode = False
    .UsedRange.AutoFilter
    .UsedRange.AutoFilter field:=2, Criteria1:=cell
    .UsedRange.AutoFilter field:=3, Criteria1:=cell.Offset(0, 1)
    End With

    ActiveSheet.AutoFilter.Range.Offset(1, 3).Copy

    Set wb = Workbooks.Open(sPath & sFile)

    wb.Close SaveChanges:=True ' After running this line,
    ' I will get a Run-Time error 9
    ' Subsript out of Range
    ' and the following line: Sheets("Marks").Activate is in Yellow. and Grade 7 B.xlsm is not open, and the range to copy is still selected.

    ' I added an apostrophie to the line (wb.Close SaveChanges:=True) to make it a remark
    ' The code continued and opened the Grade 7 B.xlsm and it:
    ' 1- deleted the range C14:X64
    ' 2- gave an error message that it could not - paste special - the values.
    ' the sheet "Marks" is protected sheet using password 112. When I unprotect the sheet,
    ' it also gave the same error not being able to paste special the values.


    Sheets("Marks").Activate

    Range("C14:X64").ClearContents

    Range("C14").PasteSpecial xlPasteAll

    wb.Close SaveChanges:=True

    ThisWorkbook.Activate

    Next

    Sheets("Sheet2").Activate

    ActiveSheet.UsedRange.ClearContents

    ActiveWorkbook.Names("Info").Delete

    Sheets("Sheet1").Activate

    ActiveSheet.UsedRange.AutoFilter

    Application.ScreenUpdating = False

    End Sub

  10. #10
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Filter data from one sheet to 14 existing sheets in different workbooks

    Have a go at this modefication and hopefully this will work better.

    Yes you were right it was one "wb.Close" too many, I've also set a new range name "data". The advantage with this is that after filtering you just get the filtered values you wish for so if for instance you wish to include "ceeluer" you will have to replace X with Y as the Y column is not oncluded in the present setup for the "data" range.

    In the "PasteSpecial" command to the target file I have used the "xlPasteAll", could be that it is better to use the "xlPasteValues".

    If you still have problems with this code I would suggest you select the "Step through" option for this macro and press the F8 button to work you way through the macro and check what happends with the "MasterAllStudents" and one "Grade" file.


    Please Login or Register  to view this content.
    Alf

  11. #11
    Registered User
    Join Date
    06-15-2013
    Location
    Israel
    MS-Off Ver
    Excel 2016
    Posts
    38

    Re: Filter data from one sheet to 14 existing sheets in different workbooks

    Thanks Alf
    It works fine.

    I included the celular also
    Thank again

  12. #12
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Filter data from one sheet to 14 existing sheets in different workbooks

    You are welcome and thanks for feedback.

    If this solves you problem don't forget to mark thread "Solved" as per forum rules.

    Alf

    Ps
    To mark your thread solved do the following:
    -Go to the top of the first post
    -Select Thread Tools
    -Select Mark thread as Solved

  13. #13
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Filter data from one sheet to 14 existing sheets in different workbooks

    Not to happy about this part of the code as it really is not checking for duplicates, just checking that a value is not the same as the next one. As you use Excel 2007 one can use Excel's "Remove duplicate" function so get rid of this part of the code

    Please Login or Register  to view this content.
    and replace it with this instead

    Please Login or Register  to view this content.
    Alf

  14. #14
    Registered User
    Join Date
    06-15-2013
    Location
    Israel
    MS-Off Ver
    Excel 2016
    Posts
    38

    Re: Filter data from one sheet to 14 existing sheets in different workbooks

    Thanks again Alf

    Khalil

+ 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. Combine columns data from multiple sheets to existing master sheet via cell value
    By 253.Asmo in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-16-2015, 12:13 PM
  2. [SOLVED] VBA code required to insert extra/New sheets with existing sheet data
    By mchilapur in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-22-2015, 01:52 AM
  3. Trying to Pull data from Master Sheet onto New and Existing Sheets
    By fjlaff in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 03-13-2014, 09:20 AM
  4. Replies: 1
    Last Post: 10-24-2013, 01:05 PM
  5. Splitting Data from 1 Sheet to Multiple Existing Sheets
    By cloud19 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-29-2013, 03:52 PM
  6. Split Data in Home Sheet to Multiple Existing Sheets
    By tboyle35 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-23-2012, 04:23 PM
  7. Appending data from a second sheet into existing sheets
    By Ace of Clubs in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-09-2009, 10:54 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