+ Reply to Thread
Results 1 to 28 of 28

Lookup value with multiple criteria - Data Range in multiple files

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-09-2013
    Location
    Nagpur, India
    MS-Off Ver
    Excel 2010 / 2013
    Posts
    293

    Lookup value with multiple criteria - Data Range in multiple files

    Hello, My data is divided in 3 workbooks i.e. Data1.xls , Data2.xls & Data3.xls saved in folder "D:\SalesTrendReport" .
    I needed to get Sales qty data from these 3 files to my workbook 'Sales Trend.xlsx' based on Office Code & Product Code.


    Sales Trend.xlsx
    Sales Trend.JPG


    Data1.xls
    Data1.JPG


    Data2.xls
    Data2.JPG


    Data3.xls
    Data3.JPG
    Attached Files Attached Files

  2. #2
    Forum Contributor
    Join Date
    07-09-2013
    Location
    Nagpur, India
    MS-Off Ver
    Excel 2010 / 2013
    Posts
    293

    Re: Lookup value with multiple criteria - Data Range in multiple files

    Attached my 3 data files.
    Attached Files Attached Files

  3. #3
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Lookup value with multiple criteria - Data Range in multiple files

    Why not all data in 1 worksheet and after that analyse the data?
    Notice my main language is not English.

    I appreciate it, if you reply on my solution.

    If you are satisfied with the solution, please mark the question solved.

    You can add reputation by clicking on the star * add reputation.

  4. #4
    Forum Contributor
    Join Date
    07-09-2013
    Location
    Nagpur, India
    MS-Off Ver
    Excel 2010 / 2013
    Posts
    293

    Re: Lookup value with multiple criteria - Data Range in multiple files

    Sir, These were just for example. My actual data is divided in over 15 workbooks with many other set of office codes. So combining all in a single worksheet is a hectic task. Is it possible to get this with formulas/vba to work with existing divided data files?

  5. #5
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Lookup value with multiple criteria - Data Range in multiple files

    Is this a task you have to do often for this (same) files, or just 1 time.

    If you have to do this ofter with the same time, I would go for 1 workbook, with all data in 1 worksheet. After that I would analyse the data.

    If it was just one time, I would open the files. Rightclick in the tab-box and choose (click) for Make a copy. Then in To file, click on new file => a copy of the data are stored in a new workbook. Repeat this action for the other files.
    Last edited by oeldere; 07-05-2020 at 09:45 AM. Reason: text changed

  6. #6
    Forum Contributor
    Join Date
    07-09-2013
    Location
    Nagpur, India
    MS-Off Ver
    Excel 2010 / 2013
    Posts
    293

    Re: Lookup value with multiple criteria - Data Range in multiple files

    Sir, These data files I download now & then from sap bi portal from which I can export/view upto max 6 months data only. If I have to compile data from last 5 years or so, it will generate many files. So combining them in a single workbook 1 by 1 take much of time.

  7. #7
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Lookup value with multiple criteria - Data Range in multiple files

    I am not able to open the xls files at the moment. Since you use excel 2010 you could have post the xlsx files.

    You probably don't get my point. Since you can only download 6 months, it would be easier if you add those new information (download) in a file which already contains the values of the earlier 5 years (minus the new 6 months).

    If you do it your way, you always have to collect 9 files (4,5 year).
    Those values are already available in your earlier collection.

  8. #8
    Forum Contributor
    Join Date
    07-09-2013
    Location
    Nagpur, India
    MS-Off Ver
    Excel 2010 / 2013
    Posts
    293

    Re: Lookup value with multiple criteria - Data Range in multiple files

    Attached data files in .xlsx format.

    ps. My default exported data files are in .xls format only.
    Attached Files Attached Files

  9. #9
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Lookup value with multiple criteria - Data Range in multiple files

    Did you understand my comment?

    You did not respond on that.

    Maybe you will get trouble with the merged cells in A1 and B1.

    My advice would be don't use merged cells.

  10. #10
    Forum Contributor
    Join Date
    07-09-2013
    Location
    Nagpur, India
    MS-Off Ver
    Excel 2010 / 2013
    Posts
    293

    Re: Lookup value with multiple criteria - Data Range in multiple files

    Sir, merged cells in data files are coming in every exported files by default.

  11. #11
    Forum Contributor
    Join Date
    07-09-2013
    Location
    Nagpur, India
    MS-Off Ver
    Excel 2010 / 2013
    Posts
    293

    Re: Lookup value with multiple criteria - Data Range in multiple files

    Sir, I didn't have previously saved compiled data of past years in a single file.

  12. #12
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2406 (Windows 11 23H2 64-bit)
    Posts
    82,450

    Re: Lookup value with multiple criteria - Data Range in multiple files

    Which version of Excel are you using?
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  13. #13
    Forum Contributor
    Join Date
    07-09-2013
    Location
    Nagpur, India
    MS-Off Ver
    Excel 2010 / 2013
    Posts
    293

    Re: Lookup value with multiple criteria - Data Range in multiple files

    Quote Originally Posted by AliGW View Post
    Which version of Excel are you using?
    Ma'am I use Excel version 2010.

  14. #14
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Lookup value with multiple criteria - Data Range in multiple files

    Sir, I didn't have previously saved compiled data of past years in a single file.
    How you add those information to the excel file, since it is not available?

    Sir, merged cells in data files are coming in every exported files by default.
    If you use a formula for your request, the merged cells will cause problems (sooner or later).

  15. #15
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Lookup value with multiple criteria - Data Range in multiple files

    You can use the file, with the directory in cell A1.

    You can run the code below, which is in this file.

    Option Explicit
    
    
    Sub MergeAllWorkbooks_0903()
        Dim MyPath As String, FilesInPath As String
        Dim MyFiles() As String
        Dim SourceRcount As Long, FNum As Long
        Dim mybook As Workbook, BaseWks As Worksheet
        Dim sourceRange As Range, destrange As Range
        Dim rnum As Long, CalcMode As Long
    
     
    MyPath = Range("data!a1")           'Where do you find the directory
    
        'Add a slash at the end if the user forget it
        If Right(MyPath, 1) <> "\" Then
            MyPath = MyPath & "\"
        End If
    
        'If there are no Excel files in the folder exit the sub
        FilesInPath = Dir(MyPath & "*.xls*")
        If FilesInPath = "" Then
            MsgBox "No files found"
            Exit Sub
        End If
    
        'Fill the array(myFiles)with the list of Excel files in the folder
        FNum = 0
        Do While FilesInPath <> ""
            FNum = FNum + 1
            ReDim Preserve MyFiles(1 To FNum)
            MyFiles(FNum) = FilesInPath
            FilesInPath = Dir()
        Loop
    
        'Change ScreenUpdating, Calculation and EnableEvents
        With Application
            CalcMode = .Calculation
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
            .EnableEvents = False
        End With
    
        'Add a new workbook with one sheet
        Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
        rnum = 1
    
        'Loop through all files in the array(myFiles)
        If FNum > 0 Then
            For FNum = LBound(MyFiles) To UBound(MyFiles)
                Set mybook = Nothing
                On Error Resume Next
                Set mybook = Workbooks.Open(MyPath & MyFiles(FNum))
                On Error GoTo 0
    
                If Not mybook Is Nothing Then
    
                    On Error Resume Next
    
                    With mybook.Worksheets(1)                     'sheet1
                        Set sourceRange = .Range("A1:J100")       '2) You can change this range
                    End With
    
                    If Err.Number > 0 Then
                        Err.Clear
                        Set sourceRange = Nothing
                    Else
                        'if SourceRange use all columns then skip this file
                        If sourceRange.Columns.Count >= BaseWks.Columns.Count Then
                            Set sourceRange = Nothing
                        End If
                    End If
                    On Error GoTo 0
    
                    If Not sourceRange Is Nothing Then
    
                        SourceRcount = sourceRange.Rows.Count
    
                        If rnum + SourceRcount >= BaseWks.Rows.Count Then
                            MsgBox "Sorry there are not enough rows in the sheet"
                            BaseWks.Columns.AutoFit
                            mybook.Close savechanges:=False
                            GoTo ExitTheSub
                        Else
    
                            'Copy the file name in column A
                            With sourceRange
                                BaseWks.Cells(rnum, "A"). _
                                        Resize(.Rows.Count).Value = MyFiles(FNum)
                            End With
    
                            'Set the destrange
                            Set destrange = BaseWks.Range("B" & rnum)
    
                            'we copy the values from the sourceRange to the destrange
                            With sourceRange
                                Set destrange = destrange. _
                                                Resize(.Rows.Count, .Columns.Count)
                            End With
                            destrange.Value = sourceRange.Value
    
                            rnum = rnum + SourceRcount
                        End If
                    End If
                    mybook.Close savechanges:=False
                End If
    
            Next FNum
            BaseWks.Columns.AutoFit
        End If
    
    ExitTheSub:
        'Restore ScreenUpdating, Calculation and EnableEvents
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
            .Calculation = CalcMode
        End With
    End Sub
    Last edited by oeldere; 07-05-2020 at 10:38 AM. Reason: text changed

  16. #16
    Forum Contributor
    Join Date
    07-09-2013
    Location
    Nagpur, India
    MS-Off Ver
    Excel 2010 / 2013
    Posts
    293

    Re: Lookup value with multiple criteria - Data Range in multiple files

    Sir, The code created new file which I'm attaching.
    Attached Files Attached Files

  17. #17
    Forum Contributor
    Join Date
    07-09-2013
    Location
    Nagpur, India
    MS-Off Ver
    Excel 2010 / 2013
    Posts
    293

    Re: Lookup value with multiple criteria - Data Range in multiple files

    Quote Originally Posted by abhinavbinkar View Post
    Sir, The code created new file which I'm attaching.
    Sir I was telling about this file which created after running the code. I've saved this file in the same folder by name Sheet2.xlsx

  18. #18
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Lookup value with multiple criteria - Data Range in multiple files

    Yes, that is the idea, and after that you can analyse the data.

  19. #19
    Forum Contributor
    Join Date
    07-09-2013
    Location
    Nagpur, India
    MS-Off Ver
    Excel 2010 / 2013
    Posts
    293

    Re: Lookup value with multiple criteria - Data Range in multiple files

    Sir, Now it requires to get the data from 'Sheet2.xlsx' which I've uploaded to my workbook 'Sales Trend.xlsx'.

  20. #20
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Lookup value with multiple criteria - Data Range in multiple files

    There is no sheet2 in the files you added to the question.

    edit

    The filename is sheet2 (which is confusing).

    But what needs to be done with that file named sheet2.

    It is not clear (at least not clear to me).
    Last edited by oeldere; 07-05-2020 at 12:37 PM.

  21. #21
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Lookup value with multiple criteria - Data Range in multiple files

    See the question in #19

    But what needs to be done with that file named sheet2.

  22. #22
    Forum Contributor
    Join Date
    07-09-2013
    Location
    Nagpur, India
    MS-Off Ver
    Excel 2010 / 2013
    Posts
    293

    Re: Lookup value with multiple criteria - Data Range in multiple files

    Sir, I'm sorry for not able to express properly.
    I'm asking about how I can add that data in my Sales Trend.xlsx file.

  23. #23
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Lookup value with multiple criteria - Data Range in multiple files

    You question is (was) how to add the 3 (or more files) in 1 sheet.

    You get code from me, how you can achieve that.
    I'm asking about how I can add that data in my Sales Trend.xlsx file.
    You did not mentioned earlier Sales Trend.xlsx file

    Neither you did not posted this file (without confidential information).

    How should I be expected to know what needs to be done with that data / file if you can't explain it.

  24. #24
    Forum Contributor
    Join Date
    07-09-2013
    Location
    Nagpur, India
    MS-Off Ver
    Excel 2010 / 2013
    Posts
    293

    Re: Lookup value with multiple criteria - Data Range in multiple files

    Quote Originally Posted by oeldere View Post
    You question is (was) how to add the 3 (or more files) in 1 sheet.

    You get code from me, how you can achieve that.


    You did not mentioned earlier Sales Trend.xlsx file

    Neither you did not posted this file (without confidential information).

    How should I be expected to know what needs to be done with that data / file if you can't explain it.
    .
    Quote Originally Posted by abhinavbinkar View Post
    I needed to get Sales qty data from these 3 files to my workbook 'Sales Trend.xlsx' based on Office Code & Product Code.


    Sales Trend.xlsx
    Attachment 685368

    Sir, I asked for the same at my thread opening post already.
    The file does not contain any confidential info. The file has dummy figures and codes.

  25. #25
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Lookup value with multiple criteria - Data Range in multiple files

    In my earlier explanation I missed 1 step.

    first use this code in each file with data.

    Before you run this code make sure:
    1) Change the name of the sheet in Data
    2) Copy the data in row 2 (the green row)

    The result of the code is in sheet output.



    Sub CONVERTROWSTOCOL_Oeldere_revisted_new()
    
    Dim rsht1 As Long, rsht2 As Long, i As Long, col As Long, wsTest As Worksheet, mr As Worksheet, ms As Worksheet
    
    'check if sheet "ouput" already exist
    
    Const strSheetName As String = "Output"
    
    Set wsTest = Nothing
    On Error Resume Next
    Set wsTest = ActiveWorkbook.Worksheets(strSheetName)
    On Error GoTo 0
     
    If wsTest Is Nothing Then
        Worksheets.Add.Name = strSheetName
    End If
    
    'set the data
                     
    
    Set mr = Sheets("DATA")                                  'this is the name of the source sheet
     
    Set ms = Sheets("Output")                                       'this is the name of the destiny sheet
    
    col = 5
    'End set the data
    
        With ms
         .UsedRange.ClearContents
         .Range("A1:F1").Value = Array("Offie Code", "Product code", "LYr Avg", "Sales Qty Plan", "Month", "Value")
    
        End With
        
        rsht2 = ms.Range("A" & Rows.Count).End(xlUp).Row
        
        
        With mr
              rsht1 = .Range("A" & .Rows.Count).End(xlUp).Row
              For i = 4 To rsht1
                    Do While .Cells(2, col).Value <> "" 'And .Cells(I, col).Value <> ""
                    rsht2 = rsht2 + 1
                   
                    ms.Range("A" & rsht2).Value = .Range("A" & i).Value
                    ms.Range("B" & rsht2).Value = .Range("B" & i).Value
                    ms.Range("C" & rsht2).Value = .Range("C" & i).Value
                    ms.Range("D" & rsht2).Value = .Range("C" & i).Value
                    
                    ms.Range("E" & rsht2).Value = .Cells(2, col).Value
                    
                    ms.Range("F" & rsht2).Value = .Cells(i, col).Value
             
                    col = col + 1
                Loop
                col = 5
            Next
        End With
        
      With ms
      
      
    
    '    .Range("C2:C" & .Rows.Count).SpecialCells(4).EntireRow.Delete
      
        .Columns("A:Z").EntireColumn.AutoFit
        
        End With
        
    End Sub
    After you have done that with all files.

    Use the code I gave you earlier (also in this file).

    After that a pivot table.

    See the attached file.

    A) 1files-data1
    B) 2020-07-05 (oeldere) abhinavbinkar mergefile

    The endresult is in file 2020-07-05 (oeldere) abhinavbinkar mergefile sheet PT Oeldere.

  26. #26
    Forum Contributor
    Join Date
    07-09-2013
    Location
    Nagpur, India
    MS-Off Ver
    Excel 2010 / 2013
    Posts
    293

    Re: Lookup value with multiple criteria - Data Range in multiple files

    Sir,

    First of all I'm sorry that I haven't replied coz I was unwell. I tried the code today, and some things are uncleared.


    Quote Originally Posted by oeldere View Post
    1) Change the name of the sheet in Data
    Do I need to change all my Data workbooks sheet name to 'Data' and then run the code on each workbook one by one?

    Quote Originally Posted by oeldere View Post
    2) Copy the data in row 2 (the green row)
    Not understand this step completely.


    Quote Originally Posted by oeldere View Post

    After that a pivot table.
    As I dont know abou pivot tables, can the vba code help to automate the above steps.

  27. #27
    Forum Contributor
    Join Date
    07-09-2013
    Location
    Nagpur, India
    MS-Off Ver
    Excel 2010 / 2013
    Posts
    293

    Re: Lookup value with multiple criteria - Data Range in multiple files

    Please help.

  28. #28
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Lookup value with multiple criteria - Data Range in multiple files

    Set mr = Sheets("DATA")     'this is the name of the source sheet
    1) That is why you need to change the name of the file in data. The code takes the data from sheet DATA.


    HTML Code: 
    HTML Code: 
    2) You need to have the data (the green row) in row 2. (this is the row for the headers).


    3) After that you can create a pivot table to analyse the data like I did.
    Last edited by oeldere; 07-26-2020 at 04:38 AM.

+ 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] Lookup multiple criteria in multiple columns in multiple rows; return true if exists
    By ufdlim in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-07-2019, 02:03 PM
  2. Replies: 0
    Last Post: 10-16-2013, 12:42 PM
  3. [SOLVED] Lookup value from range for corresponding row where multiple criteria are met
    By 0Twintig in forum Excel Formulas & Functions
    Replies: 17
    Last Post: 09-03-2013, 01:54 PM
  4. [SOLVED] Lookup Multiple criteria with range
    By serene11111 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 03-21-2013, 02:36 AM
  5. Replies: 11
    Last Post: 02-07-2013, 02:22 AM
  6. [SOLVED] VBA to lookup in specified range for multiple files
    By kmfdm515 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 09-07-2012, 10:30 AM
  7. Replies: 1
    Last Post: 05-16-2011, 05:00 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