+ Reply to Thread
Results 1 to 39 of 39

Matching Column Dates with Row Dates & Paste

  1. #1
    Forum Contributor
    Join Date
    05-27-2012
    Location
    New Brunswick, Canada
    MS-Off Ver
    Excel 2016
    Posts
    327

    Matching Column Dates with Row Dates & Paste

    What I would like to do is insert a macro in the To file that looks at the date row (Row 1) in the From file and pastes into the To file Column 1 at the appropriate row based on both docs having the same Month/Year. The row it's looking at will always be Row 22 on the From file. I've highlighted the data to be copied in the From file and where it should be placed in the To file - both in yellow.

    Any ideas where to get started? I had thoughts of using a ExecuteExcel4Macro("match(" & CLng type code but can't seem to make it work.
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,863

    Re: Matching Column Dates with Row Dates & Paste

    =INDEX([From.xlsx]Sheet1!$B$22:$AA$22,MATCH(B1,[From.xlsx]Sheet1!$B$1:$AA$1,1)) Seems to work just fine, but that's not a VBA macro.

    I can translate that into a UDF like this:
    Please Login or Register  to view this content.
    As programmed, both approaches return an N/A error when the date is in "To" is before the first date in "From". Of course, you can use IFERROR() or IFNA() to do something with that error, if desired.

    Standard rule of thumb is that the native worksheet formula approach will execute faster than VBA, and I would expect that to be the case here as well. Another advantage to the native worksheet formula approach is that the formula will work just fine when the "From" workbook is closed, where the VBA approach will require that "From" be open. You as the programmer will need to decide which of the two programming languages you prefer (I think there are good reasons to prefer native worksheet formulas over VBA in this case, but I don't know all of what you are trying to do with this project).
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  3. #3
    Forum Contributor
    Join Date
    05-27-2012
    Location
    New Brunswick, Canada
    MS-Off Ver
    Excel 2016
    Posts
    327

    Re: Matching Column Dates with Row Dates & Paste

    The reason I thought VBA would work better is to have the ability to reuse the code on multiple To and From workbooks. My To and From workbooks are more of a stripped down version of what I would use the code in.

  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,863

    Re: Matching Column Dates with Row Dates & Paste

    As represented in your samples, the operation is a pretty basic lookup operation. My preference for a simple lookup would be worksheet formulas. I've demonstrated using INDEX(...MATCH()), but it could just as easily have been HLOOKUP() or LOOKUP() or XLOOKUP() or similar. I recognize that the examples are stripped down, so I may not be seeing something that would make VBA preferable.

    You're the one who knows how you will interact with these spreadsheets. The examples I've given should allow you to see how to perform a basic lookup using either a worksheet directly in the spreadsheet, or how to call one of Excel's lookup functions from VBA and perform the lookup in VBA. From there, you will probably need to be the one to decide which approach is going to be easier for you and how you are interacting with these sheets. Let us know if and how we can help further.

  5. #5
    Forum Contributor
    Join Date
    05-27-2012
    Location
    New Brunswick, Canada
    MS-Off Ver
    Excel 2016
    Posts
    327

    Re: Matching Column Dates with Row Dates & Paste

    I tried putting the INDEX formula you provided into Col B on the To document and it just resulted in all 0s down to the bottom.
    I also tried using the mylookup function you provided into Col B but it seems to have crashed Excel, highlighted the "Stop" in the code.

  6. #6
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,863

    Re: Matching Column Dates with Row Dates & Paste

    The Stop statement is a way of entering debug mode while you are developing code so you can test and debug what you are doing. I put a Stop statement at the top of every UDF while the procedure is in development until I am satisfied that the procedure is working exactly how I want. It probably didn't "crash" Excel, it just entered debug mode. If you are unfamiliar with VBA's debugging tools: http://www.cpearson.com/excel/DebuggingVBA.aspx?em_x=22

    I am not sure why the worksheet function is returning 0s. For me, that function returned N/A errors for B1:B12, and the correct numbers for B13:B36. If you enter just the MATCH() function, does it return the expected column number?

  7. #7
    Forum Contributor
    Join Date
    05-27-2012
    Location
    New Brunswick, Canada
    MS-Off Ver
    Excel 2016
    Posts
    327

    Re: Matching Column Dates with Row Dates & Paste

    Oh that makes sense... I removed the Stop and it worked fine.
    I also made a small change in the formula, which I hadn't realized was the issue earlier - changing the MATCH(B1 to MATCH(A1 and now it works fine.

    The issue I forsee having is to change the name of From.xlsx quite a lot whereas using VBA, I could use something like ThisWorkbook for the To and Like to give the prefix of the From. It will always have a certain standard workbook name followed by the month & year, which I could leave out and use wildcards. That's why I was thinking VBA over a formula or UDF.

    Nonetheless, your solutions do work after I fixed the few little 'oops' so I thank you for them.
    Last edited by Wheelie686; 09-18-2023 at 11:45 PM.

  8. #8
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,863

    Re: Matching Column Dates with Row Dates & Paste

    The issue I forsee having is to change the name of From.xlsx quite a lot
    I missed this in the OP. It can be a hassle, though, in my own work, I find it fairly easy to edit a formula to change a file name. Or you can use an INDIRECT() function. Again, it really depends on exactly how you prefer to interact with the file.

    One thing I would add. I am not a database guy, but I have seen many examples in the forum where storing data in separate "filename--date" files is not the best way to build and maintain and query a database. Again, it's really about how you are working with this data, but I have seen many examples where a better database design ends up being better for long term use. (Don't be put off by my calling it a database. I'm not necessarily suggesting that you will need to move to a different application. A spreadsheet can still be used to store a database, and has some tools for maintaining and querying the database. The main idea is that a good database usually has all of the data stored in a single, "flat" file, and then has proper tools for querying the database.)

  9. #9
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,592

    Re: Matching Column Dates with Row Dates & Paste

    Prefer inserting formula(2nd) in this case.
    Please Login or Register  to view this content.

  10. #10
    Forum Contributor
    Join Date
    05-27-2012
    Location
    New Brunswick, Canada
    MS-Off Ver
    Excel 2016
    Posts
    327

    Re: Matching Column Dates with Row Dates & Paste

    jindon,
    Much appreciated! In the testExcecutExcel4Macro() provided, I do have 2 questions.
    1. Would I be able to change it so that it's not From.xlsx but perhaps something like From*.xlsx with a wildcard? I want to take from multiple sources but they would all start with the word From.
    2. In addition, would I be able to change it so that the From workbook could be anywhere? If I'm not mistaken, it's assuming both workbooks are in the same location but in reality I'd rather it not work that way.

  11. #11
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,592

    Re: Matching Column Dates with Row Dates & Paste

    In that case, you need to actually select the workbook from the file explorer.

  12. #12
    Forum Contributor
    Join Date
    05-27-2012
    Location
    New Brunswick, Canada
    MS-Off Ver
    Excel 2016
    Posts
    327

    Re: Matching Column Dates with Row Dates & Paste

    Would I be able to change the code somehow to pick from one of the active workbooks I have open? For further context, it would be something like this.
    ToBk1 pulls from FromBk1!Sheet1
    ToBk2 pulls from FromBk2!Sheet1
    ToBk3 pulls from From Bk3!Sheet1

    The To file name could change to unknown names (it's usually a manger's name so I can't give you a list of possible names) but the From would always from FromBk and then various character afterwards.

    EDIT - I also just noticed that when I move it, it does open a window find the file but it does it repeatedly for every cell I want populated.
    Last edited by Wheelie686; 09-19-2023 at 09:57 AM.

  13. #13
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,592

    Re: Matching Column Dates with Row Dates & Paste

    Problem is to find which ToWB needs to get data from which FromWB.

    If you have clear logic to match up those 2 files, it could be done.
    EDIT - I also just noticed that when I move it, it does open a window find the file but it does it repeatedly for every cell I want populated.
    Of course, it is saying file is not found so asking the correct one.



    I need to go off line now, so my next reply will be sometime tomorrow.

  14. #14
    Forum Contributor
    Join Date
    05-27-2012
    Location
    New Brunswick, Canada
    MS-Off Ver
    Excel 2016
    Posts
    327

    Re: Matching Column Dates with Row Dates & Paste

    There could be some logic that could be helpful maybe?

    From file - Would always be broken down like this
    1. starts with the letters NIT
    2. followed by "_C"
    3. followed by the value in B2 of the sheet I'm running the macro from

    For example, the first 7 characters of the files I've been opening are NIT_C10

    Also isn't this going to specfically looking at Rows 13 to 36 from the To file to match with Row 1 on From? I would rather it look at the entire column instead of just specific rows.
    Last edited by Wheelie686; 09-19-2023 at 01:18 PM.

  15. #15
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,592

    Re: Matching Column Dates with Row Dates & Paste

    So, From workbook names are kind of specified to some extent, but how about the file folder location for each From workbooks?

  16. #16
    Forum Contributor
    Join Date
    05-27-2012
    Location
    New Brunswick, Canada
    MS-Off Ver
    Excel 2016
    Posts
    327

    Re: Matching Column Dates with Row Dates & Paste

    It's broken down monthly and then by categories so it's not an easy find that way either.
    September folder - so in a year, there would be 12 monthly folders.
    1. Category A folder
    2. Category B folder
    3. Category C folder
    4. Category D folder

  17. #17
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,592

    Re: Matching Column Dates with Row Dates & Paste

    Can you just show us how you manually find folder and From workbook?

    Example of few ToWB(which is open as you mentioned esarlier) names and corresponding FromWB names and folder to search.

    If you explain step by step, and if there is clear logic, it can be done.

  18. #18
    Forum Contributor
    Join Date
    05-27-2012
    Location
    New Brunswick, Canada
    MS-Off Ver
    Excel 2016
    Posts
    327

    Re: Matching Column Dates with Row Dates & Paste

    I open the Forecaster Notes - Brock file and see that B1 has 10, so I locate the October folder on a shared drive.
    The sheet name will match the contents of B2, so I would then find the "from" file, which would which would start NIT_C10-2023_1800. As mentioned in a previous post, all files will start with NIT_C, the 10 comes from it being October, -2023 of course is the year, then _1800 comes from the sheet name (also B2). So once that file is located, that's my From file.
    I'm not looking for any macro assistance in finding the From file - there's no easy way to do that since it could be in one of many category subfolders, only to do a copy paste once I have the file located/open.
    Attached Files Attached Files

  19. #19
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,592

    Re: Matching Column Dates with Row Dates & Paste

    OK,

    Is ToWB only Forecaster Notes - Brock.xlsb?‎ or are you also opening other ToWB?

    If you are opening multiple ToWB and multiple FromWB, better create another workbook to hold the code, so that you can run the code from it.

  20. #20
    Forum Contributor
    Join Date
    05-27-2012
    Location
    New Brunswick, Canada
    MS-Off Ver
    Excel 2016
    Posts
    327

    Re: Matching Column Dates with Row Dates & Paste

    Multiple although I was thinking I'd create one template of sorts and then just Save As so the code would be in all the To wbks.

  21. #21
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,592

    Re: Matching Column Dates with Row Dates & Paste

    You mean like Personal.xlsb or Add-In, but in this case you are opening multiple ToWB as well as multiple FromWB.
    The code would then work from "ActiveWorkbook", and when "Activeworkbook" is not the one you want, problem.

    So, my idea is to create a specific workbook to handle this and it is easier.

    This is to show you what my idea is.

    Open only relevant workbooks as well as this workbook and then run the code.
    Please Login or Register  to view this content.
    Attached Files Attached Files

  22. #22
    Forum Contributor
    Join Date
    05-27-2012
    Location
    New Brunswick, Canada
    MS-Off Ver
    Excel 2016
    Posts
    327

    Re: Matching Column Dates with Row Dates & Paste

    Multiple ToWB as well as multiple FromWB tue but only 1 pair at a time.
    Bruce's To and From, do what i want and then close both
    Anne's To and From, do what i want and then close both
    Joe's To and From, do what i want and then close both
    etc, etc, etc. with (right now) 13 pairs

    So I opened GetData, Forecaster Notes - Brock and NIT_C10-2023_1800 in that order and get Object doesn't support this property or method (Error 438) with this highlighted when I debug

    Please Login or Register  to view this content.
    Last edited by Wheelie686; 09-21-2023 at 11:08 AM.

  23. #23
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,592

    Re: Matching Column Dates with Row Dates & Paste

    Can you just change Formula2 to Formula ?

  24. #24
    Forum Contributor
    Join Date
    05-27-2012
    Location
    New Brunswick, Canada
    MS-Off Ver
    Excel 2016
    Posts
    327

    Re: Matching Column Dates with Row Dates & Paste

    That did the trick!

  25. #25
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,592

    Re: Matching Column Dates with Row Dates & Paste

    Try the code with multiple ToWBs with multiple ToWBs and see how it goes.
    Please Login or Register  to view this content.

  26. #26
    Forum Contributor
    Join Date
    05-27-2012
    Location
    New Brunswick, Canada
    MS-Off Ver
    Excel 2016
    Posts
    327

    Re: Matching Column Dates with Row Dates & Paste

    So using the NIT_C10-2023_1800 file as an example, I noticed it it no longer functions if NIT_C10-2023_1800 has anything extra in the file name, NIT_C10-2023_1800 would only be a prefix, not the complete file name. I thought this code put a wildcard in after the C. I tried renaming it NIT_C10-2023_1800-
    Please Login or Register  to view this content.
    Apologies if that wasn't clear.

  27. #27
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,592

    Re: Matching Column Dates with Row Dates & Paste

    That line is testing if workbook.name is not "GetData.xlsm" and doesn't begin with "NIT_C", so the code assumes the workbook is one of ToWB.

  28. #28
    Forum Contributor
    Join Date
    05-27-2012
    Location
    New Brunswick, Canada
    MS-Off Ver
    Excel 2016
    Posts
    327

    Re: Matching Column Dates with Row Dates & Paste

    All my From wbks will stsrt with "NIT_C" though so I don't understand why it doesnt finish.

  29. #29
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,592

    Re: Matching Column Dates with Row Dates & Paste

    Can you upload 1 or 2 set(s) of FromWB and ToWB?

  30. #30
    Forum Contributor
    Join Date
    05-27-2012
    Location
    New Brunswick, Canada
    MS-Off Ver
    Excel 2016
    Posts
    327

    Re: Matching Column Dates with Row Dates & Paste

    Forecaster - Notes Brock is the TO file.
    NIT_C10-2023_1800 is the FROM file.
    The macro I run is called GetShrink - sorry I forgot to delete the other 2.
    The 2 things that make the macro fail are
    1. Adding anything to the end of the FROM file name.
    2. Adding any other sheets to the FROM file other than the current one.
    Attached Files Attached Files

  31. #31
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,592

    Re: Matching Column Dates with Row Dates & Paste

    When FromWB has multiple sheets, you need to specify sheet name or sheet index of the target worksheet.

    Is this the last change that you make?
    If something more, you MUST disclose everything in one post.

  32. #32
    Forum Contributor
    Join Date
    05-27-2012
    Location
    New Brunswick, Canada
    MS-Off Ver
    Excel 2016
    Posts
    327

    Re: Matching Column Dates with Row Dates & Paste

    In the FromWB wbk, the sheet name will always be Monthly-Weekly Summary regardless of sheet number. I tend to prefer not using sheet index because if the sheets get shuffled around, it causes the macro to fail and then I get the blame and have to explain why it failed.

  33. #33
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,592

    Re: Matching Column Dates with Row Dates & Paste

    OK, so sheet name is common, that's good.
    Try change "test" sub to below and Function GetWB should remain intact.
    Jus in case if any FromWB doesn't have sheet named "Monthly-Weekly Summary", it tells you at the end.
    Please Login or Register  to view this content.

  34. #34
    Forum Contributor
    Join Date
    05-27-2012
    Location
    New Brunswick, Canada
    MS-Off Ver
    Excel 2016
    Posts
    327

    Re: Matching Column Dates with Row Dates & Paste

    So now nothing happens, even when Monthly-Weekly Summary is the only sheet and the wbk name doesn't have any extra characters.
    Attached Files Attached Files

  35. #35
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,592

    Re: Matching Column Dates with Row Dates & Paste

    Tested and it is working.

  36. #36
    Forum Contributor
    Join Date
    05-27-2012
    Location
    New Brunswick, Canada
    MS-Off Ver
    Excel 2016
    Posts
    327

    Re: Matching Column Dates with Row Dates & Paste

    the "Test" macro or "GetShrink"?

  37. #37
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,592

    Re: Matching Column Dates with Row Dates & Paste

    GetShrink?

    My code posted here was always "test".

  38. #38
    Forum Contributor
    Join Date
    05-27-2012
    Location
    New Brunswick, Canada
    MS-Off Ver
    Excel 2016
    Posts
    327

    Re: Matching Column Dates with Row Dates & Paste

    I just wanted to be sure which macro you were running. I still can't seem to run Test. I'm baffled why it just does nothing when I run it.

  39. #39
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,592

    Re: Matching Column Dates with Row Dates & Paste

    If you upload your "GetData" workbook, I can test it here.

+ 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: 8
    Last Post: 07-04-2018, 10:03 AM
  2. [SOLVED] Dates in excel sheet not matching dates in comobox1
    By ColemanJames in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-09-2017, 05:28 PM
  3. Replies: 10
    Last Post: 09-04-2013, 08:34 PM
  4. Replies: 3
    Last Post: 01-04-2012, 01:42 AM
  5. Matching dates and returning another column
    By jmoorc in forum Excel General
    Replies: 8
    Last Post: 11-30-2011, 07:49 PM
  6. Replies: 5
    Last Post: 02-02-2011, 06:31 PM
  7. List Matching with Dates: Using Dates as filters
    By Thawk in forum Excel General
    Replies: 6
    Last Post: 02-15-2009, 05:12 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