+ Reply to Thread
Results 1 to 27 of 27

Macro to fill in cells based on 2 variables

  1. #1
    Forum Contributor
    Join Date
    03-04-2014
    Location
    calgary, canada
    MS-Off Ver
    Excel 2013
    Posts
    108

    Wink Macro to fill in cells based on 2 variables

    Hi Guys, please see attached workbook.

    this will be saved in a directory where there will be hundreds of .csv files. What i need is for each cell to populate when the macro is run with the value of a specific cell (stays the same every time) based on row 1 above the cell and column A.

    i also need it to ignore if the file is not there.

    for example - cell B2 needs to look for file name A2 & "Unacceptable" & B1.csv cell E2 and return that value. if that file does not exist it needs to just ignore and go to the next one. If you can help that would be awesome!!!

    let me know if you need more detail
    Attached Files Attached Files

  2. #2
    Forum Contributor
    Join Date
    03-04-2014
    Location
    calgary, canada
    MS-Off Ver
    Excel 2013
    Posts
    108

    Re: Macro to fill in cells based on 2 variables

    Its kind of liek a vlookup but i dont really want to name 1000 files that may or may not exist.....

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

    Re: Macro to fill in cells based on 2 variables

    What value is needed for each of the 4 columns? Can you provide a sample CSV or two for reference?
    _________________
    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!)

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

    Re: Macro to fill in cells based on 2 variables

    This seems like it should work if you filenames are really: 349UnacceptableQ1.csv
    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 04-29-2014 at 02:21 AM. Reason: replaced code, errors.

  5. #5
    Forum Contributor
    Join Date
    03-04-2014
    Location
    calgary, canada
    MS-Off Ver
    Excel 2013
    Posts
    108

    Re: Macro to fill in cells based on 2 variables

    hey Jerry - i am attaching a file .csv file example. if i use the code above it gives me an error - type mismatch.
    Attached Files Attached Files

  6. #6
    Forum Contributor
    Join Date
    03-04-2014
    Location
    calgary, canada
    MS-Off Ver
    Excel 2013
    Posts
    108

    Re: Macro to fill in cells based on 2 variables

    obviously i changed the file path to the correct one which is g:\RBR CSV Files\
    Last edited by winwall; 04-28-2014 at 05:14 PM.

  7. #7
    Forum Contributor
    Join Date
    03-04-2014
    Location
    calgary, canada
    MS-Off Ver
    Excel 2013
    Posts
    108

    Re: Macro to fill in cells based on 2 variables

    the one i have sent you is for Q2 - through out the year there will be a file created with Q3 at the end and so on

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

    Re: Macro to fill in cells based on 2 variables

    Correction in macro in post #4.

  9. #9
    Forum Contributor
    Join Date
    03-04-2014
    Location
    calgary, canada
    MS-Off Ver
    Excel 2013
    Posts
    108

    Re: Macro to fill in cells based on 2 variables

    i copied the changes in and still a type mismatch. sorry

  10. #10
    Forum Contributor
    Join Date
    03-04-2014
    Location
    calgary, canada
    MS-Off Ver
    Excel 2013
    Posts
    108

    Re: Macro to fill in cells based on 2 variables

    i believe this is the line where the type mismatch is occurring

    Please Login or Register  to view this content.

  11. #11
    Forum Contributor
    Join Date
    03-04-2014
    Location
    calgary, canada
    MS-Off Ver
    Excel 2013
    Posts
    108

    Re: Macro to fill in cells based on 2 variables

    Hey Jerry - i had a play with the code - see below - now there are no error messages - unfortuantely nothing happens....

    Please Login or Register  to view this content.
    Last edited by winwall; 04-28-2014 at 06:47 PM.

  12. #12
    Forum Contributor
    Join Date
    03-04-2014
    Location
    calgary, canada
    MS-Off Ver
    Excel 2013
    Posts
    108

    Re: Macro to fill in cells based on 2 variables

    what this is doing is loking for file name on first rng through - 2unacceptable2.csv - does that help with identifying what to do to fix it?

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

    Re: Macro to fill in cells based on 2 variables

    Ha, if you want 2 at the end, then that's what needs to be in your title of the columns, as well. It's using the text down column A and B1:E1 for the filenames. Change ""Q2" to "2" then.

  14. #14
    Forum Contributor
    Join Date
    03-04-2014
    Location
    calgary, canada
    MS-Off Ver
    Excel 2013
    Posts
    108

    Re: Macro to fill in cells based on 2 variables

    i want it to read like the .csv file i attached above sorry Jerry. i was playing with it to find out where the error may be so i changed the f(csv) to = o so that i could see what the file name was called that it was looking for - thats where the 2unacceptable2.csv came from. so its not pulling the text from column A and its not putting a q in the file name

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

    Re: Macro to fill in cells based on 2 variables

    Ah, I see in your uploaded sample in post #2 the filename is actually "235 Unacceptable Q1.CSV"... notice the spaces? Those weren't evident in your original question.

    I've made another correction to the code in post #4 to account for those spaces. You can put Q1, Q2, etc back in B1:E1.

  16. #16
    Forum Contributor
    Join Date
    03-04-2014
    Location
    calgary, canada
    MS-Off Ver
    Excel 2013
    Posts
    108

    Re: Macro to fill in cells based on 2 variables

    Ok Jerry - i had to add Q to the fcsv line and the workbook open line as per below. however now i click it and nothing happens at all....

    Please Login or Register  to view this content.

  17. #17
    Forum Contributor
    Join Date
    03-04-2014
    Location
    calgary, canada
    MS-Off Ver
    Excel 2013
    Posts
    108

    Re: Macro to fill in cells based on 2 variables

    What i mean by nothing is that the macro runs and no population of information happens

  18. #18
    Forum Contributor
    Join Date
    03-04-2014
    Location
    calgary, canada
    MS-Off Ver
    Excel 2013
    Posts
    108

    Re: Macro to fill in cells based on 2 variables

    if i go step by step through the macro until i find a file that defineitly exists - that i see is that the line
    Please Login or Register  to view this content.
    leaves a value of zero when i hold the mouse over it.

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

    Re: Macro to fill in cells based on 2 variables

    Last try:

    Please Login or Register  to view this content.

  20. #20
    Forum Contributor
    Join Date
    03-04-2014
    Location
    calgary, canada
    MS-Off Ver
    Excel 2013
    Posts
    108

    Re: Macro to fill in cells based on 2 variables

    Morning Jerry - well we seem to have moved forward a long way - when i ran the macro every single .csv file opened one by one which is great....unfortunately nothing was copied over though..any last thoughts?

  21. #21
    Forum Contributor
    Join Date
    03-04-2014
    Location
    calgary, canada
    MS-Off Ver
    Excel 2013
    Posts
    108

    Re: Macro to fill in cells based on 2 variables

    i just did a trial - using only 1 row with a number that i knew existed in the directory. what i did was type the letter M into the Q2 corresponding row and when the macro finished running - the cell had been cleared. it just seems to be unable to pick up cell E2 from the .csv file...this is the only prob left

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

    Re: Macro to fill in cells based on 2 variables

    The code as shown is working for me:
    http://screencast.com/t/SZgKTEhFFe1

    I have no suggestions about what might be keeping it from transferring the correct value on your system.

  23. #23
    Forum Contributor
    Join Date
    03-04-2014
    Location
    calgary, canada
    MS-Off Ver
    Excel 2013
    Posts
    108

    Re: Macro to fill in cells based on 2 variables

    i have no idea Jerry - i changed the code to
    Please Login or Register  to view this content.
    and it worked fine. then changed it back and all that happened was all teh cells with completed in them were deleted....its really strange.

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

    Re: Macro to fill in cells based on 2 variables

    You can send me more of those CSV to test with, but from what I can see the code part is fine.

    You could post the workbook where you have this macro installed so I can check that work as well.

  25. #25
    Forum Contributor
    Join Date
    03-04-2014
    Location
    calgary, canada
    MS-Off Ver
    Excel 2013
    Posts
    108

    Re: Macro to fill in cells based on 2 variables

    sent to you directly

  26. #26
    Forum Contributor
    Join Date
    03-04-2014
    Location
    calgary, canada
    MS-Off Ver
    Excel 2013
    Posts
    108

    Re: Macro to fill in cells based on 2 variables

    file attached
    Attached Files Attached Files

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

    Re: Macro to fill in cells based on 2 variables

    The problem is where you put the macro. It doesn't go in a sheet module, it goes in a standard code module. (Insert > Module)
    http://screencast.com/t/bZ1dDomCf

    Also, you didn't edit the macro to refer to your Detail sheet, it still refers to "Sheet1", fix that as well. Here's the edited macro I used that should speed things up a bit for you:

    Please Login or Register  to view this content.
    This should also be notably faster with screenupdating turned off.

+ 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] Macro to fill cells with a number based on value of another cell
    By Blokeman in forum Excel Programming / VBA / Macros
    Replies: 27
    Last Post: 05-04-2014, 11:07 PM
  2. Replies: 12
    Last Post: 03-07-2014, 08:55 AM
  3. [SOLVED] Macro to fill blank cells in column A based on non-blank cells
    By ktalamantez in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-28-2014, 02:47 PM
  4. [SOLVED] Macro to auto fill in cells based on value on different cells
    By juskojj in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-11-2013, 01:35 PM
  5. Macro to automatically fill cells based on previous entries
    By jerinjan in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-03-2008, 08:13 AM

Tags for this Thread

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