+ Reply to Thread
Results 1 to 25 of 25

Copy from another workbook based on data in a column

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-11-2009
    Location
    Toronto
    MS-Off Ver
    Excel 2010
    Posts
    517

    Copy from another workbook based on data in a column

    Hi everyone, I require a vba code where if in workbook "Summary", sheet "Report" column "E" = orange, apple and cherry then take the sum total from "F" and input that total into another workbook "Report", sheet "Total" in column "E7".
    Thanks

  2. #2
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Copy from another workbook based on data in a column

    Biznez,

    Hi again. Which book would the code reside in? Also you are saying that for all used range of column E, if E has "orange", or "apple" or "cherry" then add the F value to a total, and that total is then put in "Report"?
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  3. #3
    Forum Contributor
    Join Date
    12-11-2009
    Location
    Toronto
    MS-Off Ver
    Excel 2010
    Posts
    517

    Re: Copy from another workbook based on data in a column

    Quote Originally Posted by Arkadi View Post
    Biznez,

    Hi again. Which book would the code reside in? Also you are saying that for all used range of column E, if E has "orange", or "apple" or "cherry" then add the F value to a total, and that total is then put in "Report"?
    Hey Arkadi, yes the code would reside in workbook "Report" sheet "Total"

  4. #4
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Copy from another workbook based on data in a column

    Not too fancy, but will do the trick:
    Sub biznez_5()
    
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim lr As Long
    Dim i As Long
    Dim ftotal As Long
    Dim fname As String
    Dim myArr As Variant
    
    Application.ScreenUpdating = False
    myArr = Array("apple", "orange", "cherry") 'values that we will match against
    fname = "c:\summary.xlsx" 'change to path and proper extension... xlsm, xlsx?
    Set wb = Workbooks.Open(fname)
    Set ws = wb.Worksheets("Report")
    lr = ws.Range("E" & Rows.Count).End(xlUp).Row
    ftotal = 0
    For i = 1 To lr
        If UBound(Filter(myArr, ws.Range("E" & i))) > -1 Then ftotal = ftotal + ws.Range("F" & i)
    Next i
    wb.Close
    Worksheets("Total").Range("E7").Value = ftotal
    Application.ScreenUpdating = True
    
    End Sub

  5. #5
    Forum Contributor
    Join Date
    12-11-2009
    Location
    Toronto
    MS-Off Ver
    Excel 2010
    Posts
    517

    Re: Copy from another workbook based on data in a column

    Quote Originally Posted by Arkadi View Post
    Not too fancy, but will do the trick:
    Sub biznez_5()
    
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim lr As Long
    Dim i As Long
    Dim ftotal As Long
    Dim fname As String
    Dim myArr As Variant
    
    Application.ScreenUpdating = False
    myArr = Array("apple", "orange", "cherry") 'values that we will match against
    fname = "c:\summary.xlsx" 'change to path and proper extension... xlsm, xlsx?
    Set wb = Workbooks.Open(fname)
    Set ws = wb.Worksheets("Report")
    lr = ws.Range("E" & Rows.Count).End(xlUp).Row
    ftotal = 0
    For i = 1 To lr
        If UBound(Filter(myArr, ws.Range("E" & i))) > -1 Then ftotal = ftotal + ws.Range("F" & i)
    Next i
    wb.Close
    Worksheets("Total").Range("E7").Value = ftotal
    Application.ScreenUpdating = True
    
    End Sub
    Hi Arkadi, the file summary.xlsx will always be open. is the path c:\summary.xlsx necessary cuz this file is opened by explorer in excel format so i dont think it would have a path.

  6. #6
    Forum Contributor
    Join Date
    12-11-2009
    Location
    Toronto
    MS-Off Ver
    Excel 2010
    Posts
    517

    Re: Copy from another workbook based on data in a column

    How can this code work if i have additional data in column E in workbook Summary and instead of putting the total in E7, it puts it in E8?

    for example, also looks for "Cat, dog, lion" and puts that total in E8?

  7. #7
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Copy from another workbook based on data in a column

    Sub biznez_5()
    
    Dim wb As Workbook
    dim wb2 as workbook
    Dim ws As Worksheet
    Dim lr As Long
    Dim i As Long
    Dim ftotal As Long
    dim ftotal2 as Long
    Dim myArr As Variant
    Dim myArr2 As Variant
    
    Application.ScreenUpdating = False
    myArr = Array("apple", "orange", "cherry") 'values that we will match against
    myArr2 = Array("cat","dog","lion")
    set wb2 = thisworkbook
    Set wb = Workbooks("summary.xlsx")
    Set ws = wb.Worksheets("Report")
    lr = ws.Range("E" & Rows.Count).End(xlUp).Row
    ftotal = 0
    ftotal 2 = 0
    For i = 1 To lr
        If UBound(Filter(myArr, ws.Range("E" & i))) > -1 Then ftotal = ftotal + ws.Range("F" & i)
        If UBound(Filter(myArr2, ws.Range("E" & i))) > -1 Then ftotal2 = ftotal2 + ws.Range("F" & i)
    Next i
    wb2.Worksheets("Total").Range("E7").Value = ftotal
    wb2.Worksheets("Total").Range("E8").Value = ftotal2
    Application.ScreenUpdating = True
    
    End Sub
    Last edited by Arkadi; 05-01-2015 at 03:03 PM.

  8. #8
    Forum Contributor
    Join Date
    12-11-2009
    Location
    Toronto
    MS-Off Ver
    Excel 2010
    Posts
    517

    Re: Copy from another workbook based on data in a column

    Getting error 'Run-time error '9' Subscript out of range on the red line below

    Sub biznez_5()
    
    Dim wb As Workbook
    dim wb2 as workbook
    Dim ws As Worksheet
    Dim lr As Long
    Dim i As Long
    Dim ftotal As Long
    dim ftotal2 as Long
    Dim myArr As Variant
    Dim myArr2 As Variant
    
    Application.ScreenUpdating = False
    myArr = Array("apple", "orange", "cherry") 'values that we will match against
    myArr2 = Array("cat","dog","lion")
    set wb2 = thisworkbook
    Set wb = Workbooks("summary.xlsx")
    Set ws = wb.Worksheets("Report")
    lr = ws.Range("E" & Rows.Count).End(xlUp).Row
    ftotal = 0
    ftotal 2 = 0
    For i = 1 To lr
        If UBound(Filter(myArr, ws.Range("E" & i))) > -1 Then ftotal = ftotal + ws.Range("F" & i)
        If UBound(Filter(myArr2, ws.Range("E" & i))) > -1 Then ftotal2 = ftotal2 + ws.Range("F" & i)
    Next i
    wb2.Worksheets("Total").Range("E7").Value = ftotal
    wb2.Worksheets("Total").Range("E8").Value = ftotal2
    Application.ScreenUpdating = True
    
    End Sub

  9. #9
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Copy from another workbook based on data in a column

    Sorry... so for other word matches you need a separate total?

  10. #10
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Copy from another workbook based on data in a column

    is summary.xlsx open? and is it called xlsx or xlsm?

  11. #11
    Forum Contributor
    Join Date
    12-11-2009
    Location
    Toronto
    MS-Off Ver
    Excel 2010
    Posts
    517

    Re: Copy from another workbook based on data in a column

    ok now i saved it as xlsx and it still getting that same error message

  12. #12
    Forum Contributor
    Join Date
    12-11-2009
    Location
    Toronto
    MS-Off Ver
    Excel 2010
    Posts
    517

    Re: Copy from another workbook based on data in a column

    its open and on the header it says "Summary [Compatibility Mode]. Its not saved anywhere as i extracted it from an external application.

  13. #13
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Copy from another workbook based on data in a column

    I'm not sure how exactly you reference it, try just "Summary" without extension, or go into vba editor make a module in the Summary workbook, and make a sub with one line:
    msgbox thisworkbook.name
    It should return the name of the book as it is in that condition.

  14. #14
    Forum Contributor
    Join Date
    12-11-2009
    Location
    Toronto
    MS-Off Ver
    Excel 2010
    Posts
    517

    Re: Copy from another workbook based on data in a column

    Quote Originally Posted by Arkadi View Post
    I'm not sure how exactly you reference it, try just "Summary" without extension, or go into vba editor make a module in the Summary workbook, and make a sub with one line:
    msgbox thisworkbook.name
    It should return the name of the book as it is in that condition.
    Summary.xlsx is what i got

  15. #15
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Copy from another workbook based on data in a column

    And is it still open?

  16. #16
    Forum Contributor
    Join Date
    12-11-2009
    Location
    Toronto
    MS-Off Ver
    Excel 2010
    Posts
    517

    Re: Copy from another workbook based on data in a column

    yes its open

  17. #17
    Forum Contributor
    Join Date
    12-11-2009
    Location
    Toronto
    MS-Off Ver
    Excel 2010
    Posts
    517

    Re: Copy from another workbook based on data in a column

    nows im getting

    Run-time error '1004': Method 'Range' of object '_worksheet' failed

    Dim wb As Workbook
    dim wb2 as workbook
    Dim ws As Worksheet
    Dim lr As Long
    Dim i As Long
    Dim ftotal As Long
    dim ftotal2 as Long
    Dim myArr As Variant
    Dim myArr2 As Variant
    
    Application.ScreenUpdating = False
    myArr = Array("apple", "orange", "cherry") 'values that we will match against
    myArr2 = Array("cat","dog","lion")
    set wb2 = thisworkbook
    Set wb = Workbooks("summary.xlsx")
    Set ws = wb.Worksheets("Report")
    lr = ws.Range("E" & Rows.Count).End(xlUp).Row
    ftotal = 0
    ftotal 2 = 0
    For i = 1 To lr
        If UBound(Filter(myArr, ws.Range("E" & i))) > -1 Then ftotal = ftotal + ws.Range("F" & i)
        If UBound(Filter(myArr2, ws.Range("E" & i))) > -1 Then ftotal2 = ftotal2 + ws.Range("F" & i)
    Next i
    wb2.Worksheets("Total").Range("E7").Value = ftotal
    wb2.Worksheets("Total").Range("E8").Value = ftotal2
    Application.ScreenUpdating = True

  18. #18
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Copy from another workbook based on data in a column

    Ok, how did you get past the first error?

    As for the new error... does the sheet "Report" exist in the "Summary" workbook? and does it have data?

  19. #19
    Forum Contributor
    Join Date
    12-11-2009
    Location
    Toronto
    MS-Off Ver
    Excel 2010
    Posts
    517

    Re: Copy from another workbook based on data in a column

    Quote Originally Posted by Arkadi View Post
    Ok, how did you get past the first error?

    As for the new error... does the sheet "Report" exist in the "Summary" workbook? and does it have data?
    Hi Arkadi, is there any simpler way of writing this code?

  20. #20
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Copy from another workbook based on data in a column

    What part are you trying to simplify? Is it that you are getting errors on and off, or having trouble adapting it, or something else? Just trying to understand what your concern is so I can do my best to help accordingly

  21. #21
    Forum Contributor
    Join Date
    12-11-2009
    Location
    Toronto
    MS-Off Ver
    Excel 2010
    Posts
    517

    Re: Copy from another workbook based on data in a column

    Quote Originally Posted by Arkadi View Post
    What part are you trying to simplify? Is it that you are getting errors on and off, or having trouble adapting it, or something else? Just trying to understand what your concern is so I can do my best to help accordingly
    still stuck at the error at "Set wb = Workbooks("Summary.xlsx")"
    getting error subscript out of range. not sure why this is happening when the file is already opened

  22. #22
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Copy from another workbook based on data in a column

    you said before that summary is a workbook extracted from an external application... is it still that way, or is it a saved book at this point?
    Can you upload the report workbook, and if summary is a saved one now, maybe also that one?

  23. #23
    Forum Contributor
    Join Date
    12-11-2009
    Location
    Toronto
    MS-Off Ver
    Excel 2010
    Posts
    517

    Re: Copy from another workbook based on data in a column

    Quote Originally Posted by Arkadi View Post
    you said before that summary is a workbook extracted from an external application... is it still that way, or is it a saved book at this point?
    Can you upload the report workbook, and if summary is a saved one now, maybe also that one?
    summary is a workbook extracted from an external application (access software). The actual file name is Summary_Batch_RPT1.xlsx

  24. #24
    Forum Contributor
    Join Date
    12-11-2009
    Location
    Toronto
    MS-Off Ver
    Excel 2010
    Posts
    517

    Re: Copy from another workbook based on data in a column

    In addition. When the summary file is extracted, it opens new excel application. A whole new workbook. Maybe thats the issue. This code does not connect with the other new excel. Perhaps?

  25. #25
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Copy from another workbook based on data in a column

    Yep that could be... that is a pretty relevant detail I suppose, I'll do a few tests when I can and get back to you.

+ 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] copy from another workbook's worksheet based on column name.
    By Jhail83 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 08-01-2013, 11:30 AM
  2. Replies: 6
    Last Post: 01-29-2013, 07:01 AM
  3. Replies: 4
    Last Post: 09-15-2012, 02:18 PM
  4. Replies: 6
    Last Post: 09-27-2011, 02:11 PM
  5. Replies: 1
    Last Post: 04-01-2006, 03:50 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