+ Reply to Thread
Results 1 to 41 of 41

Populate History Sheet from Custom function Variables (vlookup)

  1. #1
    Forum Contributor
    Join Date
    01-09-2011
    Location
    Nomans, Land
    MS-Off Ver
    Excel 2007
    Posts
    103

    Talking Populate History Sheet from Custom function Variables (vlookup)

    This macro is the solved one "Credits to Nimrod and JB"
    Short description: It vlookups from your data wb and pastes the results value on the lastcol without you having to input formulas in the worksheet!
    Neat! n1.

    Haleluijah...


    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by exc4libur; 03-31-2011 at 10:23 PM. Reason: SOLUTION ABOVE...

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2003
    Location
    British Columbia , Canada
    MS-Off Ver
    03,07,10,13
    Posts
    727

    Re: Insert FormulaR1C1 and Fill Down Multiple Worksheets

    Could you please explain what you mean by "5 formula" and what each of these formula are suppposed to do ??

  3. #3
    Forum Contributor
    Join Date
    01-09-2011
    Location
    Nomans, Land
    MS-Off Ver
    Excel 2007
    Posts
    103

    Re: Insert FormulaR1C1 and Fill Down Multiple Worksheets

    ...........
    Last edited by exc4libur; 03-28-2011 at 11:18 PM.

  4. #4
    Valued Forum Contributor
    Join Date
    09-21-2003
    Location
    British Columbia , Canada
    MS-Off Ver
    03,07,10,13
    Posts
    727

    Re: Insert FormulaR1C1 and Fill Down Multiple Worksheets

    OK.... good
    So I need a little more detail .. could you give me a concrete example

    List the name of the 5 sheets AND the formula you like to see in each .. OK ?

    By-The-Way ... Are the sheet names constant ? Or will they vary .. on how does the formula vary ?

  5. #5
    Valued Forum Contributor
    Join Date
    09-21-2003
    Location
    British Columbia , Canada
    MS-Off Ver
    03,07,10,13
    Posts
    727

    Re: Insert FormulaR1C1 and Fill Down Multiple Worksheets

    In your code you do some very expensive ,"time wise" , activities. Placing formula's in cells, haveing all formulas continuing to re-calc with each change in sheet , then a copy and a paste .. are all very time expensive.
    In the following solution I just use the Vlookup formula within the confines of the code BUT never place it in the cell. The only thing placed in cell is the resulting value. Hence no formula re-calcs and no copy and pastes ... in other words I've cut out the middle men

    ALSO: This solution allows us to pass variables to the Vlookup formula ... in my example I vary the "Return Column" based on the Source sheet i.e. If the Current Source sheet is named "Week1" then the Column to return in the C2:C6 range would be C3 etc etc.

    Without concrete examples from you ... this is about as far as I can go ...

    Please Login or Register  to view this content.
    Last edited by nimrod; 03-28-2011 at 09:34 PM.

  6. #6
    Forum Contributor
    Join Date
    01-09-2011
    Location
    Nomans, Land
    MS-Off Ver
    Excel 2007
    Posts
    103

    Re: Insert FormulaR1C1 and Fill Down Multiple Worksheets

    Sorry Nim I didn`t answer you asap! My mom was almost shooting me to eat dinner at the table hahaha..

    So, I attached 2 example workbooks!
    => Book1 is the table which I extract data
    => Book2 is where I will store the vlookup data

    No sheets have names..
    I have inserted the vlookup formulas in every sheet in Book2 with the vlookups for each.
    Attached Files Attached Files

  7. #7
    Valued Forum Contributor
    Join Date
    09-21-2003
    Location
    British Columbia , Canada
    MS-Off Ver
    03,07,10,13
    Posts
    727

    Re: Insert FormulaR1C1 and Fill Down Multiple Worksheets

    From a quick look at your attached books I'd say my last code posting is close to what you want ... you just need to change the SheetNames and the Columns to return in the case statement

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

  8. #8
    Forum Contributor
    Join Date
    01-09-2011
    Location
    Nomans, Land
    MS-Off Ver
    Excel 2007
    Posts
    103

    Re: Insert FormulaR1C1 and Fill Down Multiple Worksheets

    Hell YEAH!!!!!!!!!!! Woooohooo ive kept all day on this!! hahaha thanks NIM THANK YOU!!
    Attached Files Attached Files
    Last edited by exc4libur; 03-28-2011 at 11:04 PM.

  9. #9
    Valued Forum Contributor
    Join Date
    09-21-2003
    Location
    British Columbia , Canada
    MS-Off Ver
    03,07,10,13
    Posts
    727

    Re: Insert FormulaR1C1 and Fill Down Multiple Worksheets

    Cool ... I'm glad it all worked out for you .... are my changes in the code making sense to you ??

  10. #10
    Forum Contributor
    Join Date
    01-09-2011
    Location
    Nomans, Land
    MS-Off Ver
    Excel 2007
    Posts
    103

    Re: Insert FormulaR1C1 and Fill Down Multiple Worksheets

    Quote Originally Posted by nimrod View Post
    Cool ... I'm glad it all worked out for you .... are my changes in the code making sense to you ??
    One more detail please!! how to change the sheetnames from the extraction book? like we are only using " [book1.xls]sheet1! ", what if i need it to get from "sheet2!" ?

  11. #11
    Valued Forum Contributor
    Join Date
    09-21-2003
    Location
    British Columbia , Canada
    MS-Off Ver
    03,07,10,13
    Posts
    727

    Re: Insert FormulaR1C1 and Fill Down Multiple Worksheets

    Do you mean the Vlookup Source sheet "EmpLns_I" ??

  12. #12
    Forum Contributor
    Join Date
    01-09-2011
    Location
    Nomans, Land
    MS-Off Ver
    Excel 2007
    Posts
    103

    Re: Insert FormulaR1C1 and Fill Down Multiple Worksheets

    Quote Originally Posted by nimrod View Post
    Do you mean the Vlookup Source sheet "EmpLns_I" ??
    Oh yea, sorry I was using the example! But yes that it, what if I had to use "EmpLns_II"

  13. #13
    Valued Forum Contributor
    Join Date
    09-21-2003
    Location
    British Columbia , Canada
    MS-Off Ver
    03,07,10,13
    Posts
    727

    Re: Insert FormulaR1C1 and Fill Down Multiple Worksheets

    That depends ... is this sheet always going to be varied / or not ? And what is the name dependent on ?

  14. #14
    Forum Contributor
    Join Date
    01-09-2011
    Location
    Nomans, Land
    MS-Off Ver
    Excel 2007
    Posts
    103

    Re: Insert FormulaR1C1 and Fill Down Multiple Worksheets

    Please Login or Register  to view this content.
    Actually I had something more simple in mind, now that we can filldown the vlookup results referencing by sheetname, could we instead of basing ourself by column why not base it by formula? For instance, someone might want to use index mid small instead of vlookup and place results, or average etc.. different formulas according to the sheet and then just placing the result 'like you said - cutting the middleman hahaha'

    So the code would be like:
    Please Login or Register  to view this content.
    Last edited by exc4libur; 03-28-2011 at 11:25 PM.

  15. #15
    Valued Forum Contributor
    Join Date
    09-21-2003
    Location
    British Columbia , Canada
    MS-Off Ver
    03,07,10,13
    Posts
    727

    Re: Insert FormulaR1C1 and Fill Down Multiple Worksheets

    Can you see the pattern of my changes ?
    1) Add var to case statement
    2) Add var to function call
    3) Change section of Vloop from Constant to Var ... with the Var surrounded by """ & Var & """

    Please Login or Register  to view this content.

  16. #16
    Forum Contributor
    Join Date
    01-09-2011
    Location
    Nomans, Land
    MS-Off Ver
    Excel 2007
    Posts
    103

    Re: Insert FormulaR1C1 and Fill Down Multiple Worksheets

    I thought of that yes! But what about this idea below??
    Sorry to take your time nim

    Quote Originally Posted by exc4libur View Post
    Please Login or Register  to view this content.
    Actually I had something more simple in mind, now that we can filldown the vlookup results referencing by sheetname, could we instead of basing ourself by column why not base it by formula? For instance, someone might want to use index mid small instead of vlookup and place results, or average etc.. different formulas according to the sheet and then just placing the result 'like you said - cutting the middleman hahaha'

    So the code would be like:
    Please Login or Register  to view this content.

  17. #17
    Valued Forum Contributor
    Join Date
    09-21-2003
    Location
    British Columbia , Canada
    MS-Off Ver
    03,07,10,13
    Posts
    727

    Re: Insert FormulaR1C1 and Fill Down Multiple Worksheets

    If you want my opinion I would say that this method goes against best practices. You should code so that changes and updates only occur in one place and one line. With you sol'n a change in the Vlookup formula means having to change 5 seperate lines of a hard to read formula. With a change in mine the formula probably doens't need to be changed AND if it does you only have to change it in once. Errrors in coding increase and time for maintenance increase dramatically when you have duplication of code. i.e. the Vlookup formula 5 times instead of once. In my workplace , as in most others, the coding would be returned for refactoring. Sorry

    ALSO: Having the "formula" in it's own function allows for very granular error trapping . In fact if this was Java Code the entire thing would be in 1/2 dozen methods and classes.

    ALOS: By keeping the formula function seperate means that you're generating re-usable code ... do you think I just wrote that function ? I've been using it for years ... just Tweaking it ... but I wouldn't be able to do that if it's imbedded in the main procedure.
    Last edited by nimrod; 03-28-2011 at 11:42 PM.

  18. #18
    Forum Contributor
    Join Date
    01-09-2011
    Location
    Nomans, Land
    MS-Off Ver
    Excel 2007
    Posts
    103

    Talking Re: Insert FormulaR1C1 and Fill Down Multiple Worksheets

    Quote Originally Posted by nimrod View Post
    If you want my opinion I would say that this method goes against best practices. You should code so that changes and updates only occur in one place and one line. With you sol'n a change in the Vlookup formula means having to change 5 seperate lines of a hard to read formula. With a change in mine the formula probably doens't need to be changed AND if it does you only have to change it in once. Errrors in coding increase and time for maintenance increase dramatically when you have duplication of code. i.e. the Vlookup formula 5 times instead of once. In my workplace , as in most others, the coding would be returned for refactoring. Sorry

    ALSO: Having the "formula" in it's own function allows for very granular error trapping .
    Uhnnnnnnnnnnnnnnnn I see ok, doesnt hurt gathering all data worksheets to one workbook and changing the vsheet according to the lookup hahaha! Well Nim thank you very much man, ive been all day trying to crack this! Thanks a million man!!

    Best Rgds!!!!!

  19. #19
    Valued Forum Contributor
    Join Date
    09-21-2003
    Location
    British Columbia , Canada
    MS-Off Ver
    03,07,10,13
    Posts
    727

    Re: Insert FormulaR1C1 and Fill Down Multiple Worksheets

    Cool dude ... it's been fun ... cheers.

    ALOS: By keeping the formula function seperate means that you're generating re-usable code ... do you think I just wrote that function ? I've been using it for years ... just Tweaking it ... but I wouldn't be able to do that if it's imbedded in the main procedure.

  20. #20
    Forum Contributor
    Join Date
    01-09-2011
    Location
    Nomans, Land
    MS-Off Ver
    Excel 2007
    Posts
    103

    Re: Insert FormulaR1C1 and Fill Down Multiple Worksheets

    Quote Originally Posted by nimrod View Post
    Cool dude ... it's been fun ... cheers.

    ALOS: By keeping the formula function seperate means that you're generating re-usable code ... do you think I just wrote that function ? I've been using it for years ... just Tweaking it ... but I wouldn't be able to do that if it's imbedded in the main procedure.
    Im starting to use functions now! hahahaha God its a time saver and veeeeery useful!

    Cheers!

  21. #21
    Valued Forum Contributor
    Join Date
    09-21-2003
    Location
    British Columbia , Canada
    MS-Off Ver
    03,07,10,13
    Posts
    727

    Re: Insert FormulaR1C1 and Fill Down Multiple Worksheets

    Not to mention you're starting to create a library of reusable functions. If they're written well you'll be surprised how little code you need to write .. for example here's my function for retrieving values from closed books ... do you see how each section of the formula is an input to the function... I don't have to read my code ... I just have to provide the required inputs

    Please Login or Register  to view this content.

  22. #22
    Valued Forum Contributor
    Join Date
    09-21-2003
    Location
    British Columbia , Canada
    MS-Off Ver
    03,07,10,13
    Posts
    727

    Re: Insert FormulaR1C1 and Fill Down Multiple Worksheets

    Another thing to note in this is the use of ExecuteExcel4Macro to run any formula on remote/closed books . This will work with any formula AND you don't need to put the formula in a cell ... you can assign the results to a variable in your VBA Code...
    Please Login or Register  to view this content.

  23. #23
    Forum Contributor
    Join Date
    01-09-2011
    Location
    Nomans, Land
    MS-Off Ver
    Excel 2007
    Posts
    103

    Re: Insert FormulaR1C1 and Fill Down Multiple Worksheets

    Yeap i see, what is the difference between private function and only function????

  24. #24
    Valued Forum Contributor
    Join Date
    09-21-2003
    Location
    British Columbia , Canada
    MS-Off Ver
    03,07,10,13
    Posts
    727

    Re: Insert FormulaR1C1 and Fill Down Multiple Worksheets

    "Private" functions and "Private" Subs can only be seen by other functions and subs within the same module. While Public or plain Functions and Subs are available to other open books and modules. Plus private's cannot be seen by the user. This way you can prevent functions being called by people/procedures that shouldn't call them.

  25. #25
    Forum Contributor
    Join Date
    01-09-2011
    Location
    Nomans, Land
    MS-Off Ver
    Excel 2007
    Posts
    103

    Re: Insert FormulaR1C1 and Fill Down Multiple Worksheets

    Quote Originally Posted by nimrod View Post
    "Private" functions and "Private" Subs can only be seen by other functions and subs within the same module. While Public or plain Functions and Subs are available to other open books and modules. Plus private's cannot be seen by the user. This way you can prevent functions being called by people/procedures that shouldn't call them.
    Ohh i c thats a real problem! Thanks for the tip hahaha

    One more thing,

    ExecuteExcel4Macro("VLOOKUP(""" & LookUpValue & """,[Book1.xls]""" & VSheet & """!C1:C6,""" & ReturnColmn & """,FALSE)")

    I am not managing to run the procedure correctly, whats wrong? I checked all spaces and """, but it doesnt run correctly.

  26. #26
    Valued Forum Contributor
    Join Date
    09-21-2003
    Location
    British Columbia , Canada
    MS-Off Ver
    03,07,10,13
    Posts
    727

    Re: Insert FormulaR1C1 and Fill Down Multiple Worksheets

    Here's an example of the use of public and private functions...

    Do you see how the Public functions hide , and do , some of the grunt work for the procedures that call them .. while the core changes would still only occur in one place .. the private function. This is a set of functions that allow me to find the entire bottom of a sheet ... no matter what the version is .

    Please Login or Register  to view this content.

  27. #27
    Forum Contributor
    Join Date
    01-09-2011
    Location
    Nomans, Land
    MS-Off Ver
    Excel 2007
    Posts
    103

    Re: Insert FormulaR1C1 and Fill Down Multiple Worksheets

    How did you learn these things? Im 20 yrs old and in less than 3 months ive seen a whole new world omfg, like excel is MAGIC! hahaha

  28. #28
    Valued Forum Contributor
    Join Date
    09-21-2003
    Location
    British Columbia , Canada
    MS-Off Ver
    03,07,10,13
    Posts
    727

    Re: Insert FormulaR1C1 and Fill Down Multiple Worksheets

    I've been coding since 1974 and I have all my code reviewed by other programmers... I just do the excel stuff as a way to relax and unwind

  29. #29
    Forum Contributor
    Join Date
    01-09-2011
    Location
    Nomans, Land
    MS-Off Ver
    Excel 2007
    Posts
    103

    Re: Insert FormulaR1C1 and Fill Down Multiple Worksheets

    Quote Originally Posted by nimrod View Post
    I've been coding since 1974 and I have all my code reviewed by other programmers... I just do the excel stuff as a way to relax and unwind
    Hahaha holy **** 1974!

  30. #30
    Valued Forum Contributor
    Join Date
    09-21-2003
    Location
    British Columbia , Canada
    MS-Off Ver
    03,07,10,13
    Posts
    727

    Re: Insert FormulaR1C1 and Fill Down Multiple Worksheets

    See how my function is being used in this code ? ... the old method had 65536 hardcoded as the bottom of the sheet. The ThisBook_EntireSheetRowsCount function is more flexable ... it adjusts the bottom of the sheet based on the version your in.

    Please Login or Register  to view this content.

  31. #31
    Valued Forum Contributor
    Join Date
    09-21-2003
    Location
    British Columbia , Canada
    MS-Off Ver
    03,07,10,13
    Posts
    727

    Re: Insert FormulaR1C1 and Fill Down Multiple Worksheets

    Hahaha holy **** 1974!
    lol ... yep ... but don't let the age fool you .. I still snowboard 30-40 days a year and Mtn Bike in the summer ... not to mention IPSC ...

    .... ok ... I've got to go dude ... cheers

  32. #32
    Forum Contributor
    Join Date
    01-09-2011
    Location
    Nomans, Land
    MS-Off Ver
    Excel 2007
    Posts
    103

    Re: Insert FormulaR1C1 and Fill Down Multiple Worksheets

    Quote Originally Posted by nimrod View Post
    See how my function is being used in this code ? ... the old method had 65536 hardcoded as the bottom of the sheet. The ThisBook_EntireSheetRowsCount function is more flexable ... it adjusts the bottom of the sheet based on the version your in.

    Please Login or Register  to view this content.
    Yes!! hahah i tweaked ur code a lil aswell hahah

    Please Login or Register  to view this content.
    But its not working, its only returning blank! Any ideas?

  33. #33
    Valued Forum Contributor
    Join Date
    09-21-2003
    Location
    British Columbia , Canada
    MS-Off Ver
    03,07,10,13
    Posts
    727

    Re: Insert FormulaR1C1 and Fill Down Multiple Worksheets

    Sorry ... I really really have to go ... I'm sure we'll run into each other again... cheers

  34. #34
    Forum Contributor
    Join Date
    01-09-2011
    Location
    Nomans, Land
    MS-Off Ver
    Excel 2007
    Posts
    103

    Re: Insert FormulaR1C1 and Fill Down Multiple Worksheets

    Quote Originally Posted by nimrod View Post
    Sorry ... I really really have to go ... I'm sure we'll run into each other again... cheers
    Oh well! Best regards eh! Thanks for everything, genius from 74 hahaha

  35. #35
    Valued Forum Contributor
    Join Date
    09-21-2003
    Location
    British Columbia , Canada
    MS-Off Ver
    03,07,10,13
    Posts
    727

    Re: Insert FormulaR1C1 and Fill Down Multiple Worksheets

    Where I am I don't have access to excel but .... I think that your full path is the problem it should end with "\"

    Fullpath = "C:\Users\exc4libur\Desktop\"

  36. #36
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Insert FormulaR1C1 and Fill Down Multiple Worksheets

    Quote Originally Posted by nimrod View Post
    Where I am I don't have access to excel but .... I think that your full path is the problem it should end with "\"

    Fullpath = "C:\Users\exc4libur\Desktop\"
    Yes, always always always! I comment all my "fPath" variables with that reminder...
    Please Login or Register  to view this content.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  37. #37
    Valued Forum Contributor
    Join Date
    09-21-2003
    Location
    British Columbia , Canada
    MS-Off Ver
    03,07,10,13
    Posts
    727

    Re: Insert FormulaR1C1 and Fill Down Multiple Worksheets

    JB is right ... the issue of the "\" can be a problem ... soooo let's improve the function to automatically take care of the issue by adding " If Not Right(FullPath, 1) = "\" Then FullPath = FullPath & "\"" to the function. Now since this is a function we don't have to run around to all the places its used ... we just make the single change in one place Now anytime the "\" is forgotten it will be automatically added.


    Please Login or Register  to view this content.

  38. #38
    Forum Contributor
    Join Date
    01-09-2011
    Location
    Nomans, Land
    MS-Off Ver
    Excel 2007
    Posts
    103

    Re: Insert FormulaR1C1 and Fill Down Multiple Worksheets

    Thanks god i solved it lol

    Please Login or Register  to view this content.
    Last edited by exc4libur; 03-31-2011 at 10:17 PM.

  39. #39
    Forum Contributor
    Join Date
    01-09-2011
    Location
    Nomans, Land
    MS-Off Ver
    Excel 2007
    Posts
    103

    Re: Populate History Sheet from Custom function Variables (vlookup)

    Thanks for the help guys!

  40. #40
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Populate History Sheet from Custom function Variables (vlookup)

    Last suggested tweak, your row/col thinking is very xl2003, which is fine, but your code may not work as expected on more recent version. You can improve that with a simple tweak, too:

    Please Login or Register  to view this content.
    That will function the same in all versions where the bottom row and furthest column is different.

  41. #41
    Registered User
    Join Date
    04-08-2011
    Location
    Mumbai
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Populate History Sheet from Custom function Variables (vlookup)

    Hi,

    Thanks Nimrod for the post.
    I did some experiments and have modified according to my requirements. However the function works only for strings and fails when numbers are involved, how do we overcome that ?

    Thanks,
    Vijay
    Last edited by Vbadgeri; 04-15-2011 at 07:50 AM. Reason: My understanding issue

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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