+ Reply to Thread
Results 1 to 12 of 12

Macro for calculating + plotting various results for all input values in relevant columns

  1. #1
    Registered User
    Join Date
    07-10-2019
    Location
    Cologne
    MS-Off Ver
    2019
    Posts
    6

    Question Macro for calculating + plotting various results for all input values in relevant columns

    Hi,

    I am new to Excel VBA but already forced to handle more complex issues. I’m not able to solve following problem:

    I’ve created a (Poisson-)Matrix in L4:AF24 where each cell is of the following form, e.g. L4

    =POISSON(0;B2;FALSE)*POISSON(0;E2;FALSE)
    where (0;…)*(0;…) increase by going down or right within the matrix.
    B2 and E2 are in this example my first two input values for which I want to calculate my results.

    The results are always three values which represent the sum of a certain part of the matrix. So all three result values are only dependent on my two input values from columns B and E.

    The three result values should be presented in columns F:H, two rows below the row which includes the two input values in B and E. For the example of my first two input values B2 and E2 the results should be presented in F4:H4 by calculating the results using the L4:AF24, described above.

    Since there are thousands of relevant values in columns B and E (only in every 82th row) I need to create a macro. Unfortunately I’m still not able to handle this issue! I’ve already tried some code, e.g. „application.calculate“ but I’m not sure how to calculate with two input values although searching for useful information since days..

    Is there anybody who is able and willing to help? It would be great, thank you!

  2. #2
    Forum Expert Pepe Le Mokko's Avatar
    Join Date
    05-14-2009
    Location
    Belgium
    MS-Off Ver
    O365 v 2402
    Posts
    13,446

    Re: Macro for calculating + plotting various results for all input values in relevant colu

    Attach a sample workbook (not a picture or pasted copy). Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.

  3. #3
    Registered User
    Join Date
    07-10-2019
    Location
    Cologne
    MS-Off Ver
    2019
    Posts
    6

    Re: Macro for calculating + plotting various results for all input values in relevant colu

    I see, it's a bit chaotic following all these words. I've created a test file where the values in B and E are not in every 82nd row but in each row. But this hopefully doesn't make an elementary difference? Hope my goal now gets clear?
    Attached Files Attached Files

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

    Re: Macro for calculating + plotting various results for all input values in relevant colu

    Are you required to use VBA for this? It looks like the kind of problem that a Data table would readily solve without the need for VBA. If you are unfamiliar with data tables: https://www.excel-easy.com/examples/data-tables.html

    Steps I took:
    1) A data table is going to want only one input rather than two. I can reduce the two inputs to one input, if I add a column of row numbers immediately adjacent to the desired output columns and use an INDEX() function in the data table I/O cells. I select column F and insert a column.
    2) Fill this new column F with row numbers. In F2:F5 I enter 1, 2, 3, 4. Then select F2:F5, grab the little + at the lower right, and drag down to fill the column with the numbers 1 to 304.
    3) I need a row number "input cell" over by the Poisson table. I choose to use J27 for this input cell, and enter 1 in J27.
    4) J26 and K26 need to get the desired input values from columns B and E based on the row number in J27, so I enter an INDEX() function in each. J26 =INDEX($B$2:$B$305,$J$26). K26 =INDEX($E$2:$E$305,$J$26).
    5) I need a simple formula at the top of the output so that the data table will know what the output cells are. I replace the 1 in G1 with =M$26 and copy that into H1:I1. (If you want to keep the 1, 2, 3, insert a row below the 1, 2, 3 and enter these formulas into the new row 2).
    6) Now I am ready to invoke the data table command. Select G1:I305 (G2:I306 if you inserted a row) -> Call the Data table command -> column input is J27 (or J28 if you added a row) -> OK. columns G:I should fill in with the appropriate outputs based on the different inputs. One advantage is that data tables will autocalculate with each calculate event, so the data table will calculate automatically if any of your input data changes.

    Hopefully I explained that all correctly. If not, the basic idea should still work (fixing any errors in references and such that I made). Are you required to use VBA? Will something like this work for you?
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  5. #5
    Registered User
    Join Date
    07-10-2019
    Location
    Cologne
    MS-Off Ver
    2019
    Posts
    6

    Re: Macro for calculating + plotting various results for all input values in relevant colu

    Thank you so much for your help but unfortunately it is not working at all. I've followed your instructions but every time I try to create the data table excels says "Reference to input field is invalid". However this is only a test file. In the real scenario the input values are only in every 82nd row and the results should be presented in columns F:H but 2 rows below the row that includes the input values for this calculation. I guess I am forced to use VBA otherwise I would need time until I'm 78 years old.

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

    Re: Macro for calculating + plotting various results for all input values in relevant colu

    I've followed your instructions but every time I try to create the data table excels says "Reference to input field is invalid".
    Not sure why you are getting the error. It seems to work fine for me. Did you change something a little (a different "column input cell" or used a different column for the index numbers or something)? Something different in your newer version of Excel? Are you certain you followed all the steps? Can you upload a new sample with what you could get to work? My guess is that something is slightly different so that Excel cannot correctly link the column input cell to the correct column in the data table or Excel cannot link the output cells to the correct columns in the data table.

    In the real scenario the input values are only in every 82nd row and the results should be presented in columns F:H but 2 rows below the row that includes the input values for this calculation.
    I guess if you want to insist on using VBA we can go that direction. However, I would still be inclined to use a data table like this. In this scenario, where the inputs are spread out, I would build a consolidated table from the sparse data. Then set up the data table. Finally, the output cells can pull their results from the consolidated data table. Using your sample sheet as a template and assuming we can figure out why you are having trouble getting the actual data table to work, the only additional changes that would be needed would be:

    1) Columns B and E in the sample sheet/consolidated table template would need INDEX() or other lookup function to pull the data from every 82nd row of the main sheet. Details depend on exactly what you mean by "every 82nd' row. For example, if you mean rows 82, 164, 246, etc., then this INDEX() function could be something like =INDEX(main!B:B,$F2*82) in B2. Copy into column E (and adapt references, if necessary).
    2) The output cells (every 82nd row but 2 rows below the input cells) in the main sheet would need INDEX() or other lookup functions to pull the results columns G, H, and I of the consolidated data table. If there is a convenient index number somewhere in the 82 row block that an INDEX() function could reference, this could be as simple as =INDEX(datatable!G$2:I$305,referencetoindexnumber). With appropriate relative and absolute references, you may be able to simply copy this into all of the output cells (every 82nd row 2 rows below the input cells).

    I don't know how far to encourage this non-VBA solution -- especially if you are absolutely certain that you would prefer to use VBA over native spreadsheet functionality. Are interested in pursuing this kind of non-VBA solution? (I would note that VBA is going to need to do most of the same tasks -- it's really just a difference in programming language, if you want to think of it like that. It's not like VBA will necessarily be easier or faster. Unless you find programming in VBA easier than native spreadsheet functions).
    Last edited by MrShorty; 07-12-2019 at 11:24 AM.

  7. #7
    Registered User
    Join Date
    07-10-2019
    Location
    Cologne
    MS-Off Ver
    2019
    Posts
    6

    Re: Macro for calculating + plotting various results for all input values in relevant colu

    "...so I enter an INDEX() function in each. J26 =INDEX($B$2:$B$305,$J$26). K26 =INDEX($E$2:$E$305,$J$26)"
    Don't you mean INDEX(...;$J$27) in both formulas?

    I've tried it again but it doesn't work. I don't know what I'm doing wrong, the message described before always appears. I've uploaded the new file. Thank you!

    Seems like you know something I couldn't have ever imagined. If I can figure out what my problem is I could imagine it'll work with your way, without VBA.
    Attached Files Attached Files

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

    Re: Macro for calculating + plotting various results for all input values in relevant colu

    Yes, I did mean J27 in both. Good catch.

    It looks like it is all ready to add the data table. In this latest file I A) Select F1:I305 (I had G1:I305 in my previous instructions, so maybe that was the mistake) then B) Data -> What if analysis -> Data table -> enter J27 in the column input cell field -> Okay. The data table populates with all the results.

  9. #9
    Registered User
    Join Date
    07-10-2019
    Location
    Cologne
    MS-Off Ver
    2019
    Posts
    6

    Re: Macro for calculating + plotting various results for all input values in relevant colu

    Hi, thank you so far! You're right, that was my mistake. I was able to execute the operation but I got weird results. All result cells show the value of its row. I've uploaded the file again.
    Attached Files Attached Files

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

    Re: Macro for calculating + plotting various results for all input values in relevant colu

    It looks like you entered J27 as the row input cell rather than the column input cell. Select F1:I305 again, create the data table, and put J27 in the column input cell field.

  11. #11
    Registered User
    Join Date
    07-10-2019
    Location
    Cologne
    MS-Off Ver
    2019
    Posts
    6

    Re: Macro for calculating + plotting various results for all input values in relevant colu

    Thank you so much! Now it just works fine. But I'm not able to apply this only every 82nd row. You said:

    "Columns B and E in the sample sheet/consolidated table template would need INDEX() or other lookup function to pull the data from every 82nd row of the main sheet. Details depend on exactly what you mean by "every 82nd' row. For example, if you mean rows 82, 164, 246, etc., then this INDEX() function could be something like =INDEX(main!B:B,$F2*82) in B2. Copy into column E (and adapt references, if necessary)."

    Why do I need to copy in B2? In this case I would delete the value of the cell which is necessary for my calculation? Sorry but it doesn't make sense to me

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

    Re: Macro for calculating + plotting various results for all input values in relevant colu

    A data table cannot be spread out to be in every 82nd row. It must be a contiguous range. My idea is to pull the data from the main sheet where the data is spread out into this sheet where the data is in a single block, then pull the result back into the main sheet where things are spread out. So this should not delete any data, just make a copy of the data for the data table to work with.

    I've tried to illustrate in the attached. I don't really know what your main spreadsheet looks like, but I have tried to show something where the input data is in every 82nd row (starting in row 82). Formulas in B and E of Before pull the input data into the data table. Then I have formulas in main that pull the results back into main (2 rows below and to the right of the inputs). In practice, you would enter your input data in main, the results will automatically calculate in Before, then the results will automatically appear in main.
    Attached Files Attached Files

+ 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. Macro for Supply Input values & Copy results in Excel from a Redirected Webpage
    By Pks12 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-09-2017, 05:21 AM
  2. Replies: 13
    Last Post: 08-11-2016, 01:02 AM
  3. VBA Macro to input values into web form and then click button and get results back
    By Benisato in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-21-2013, 10:15 AM
  4. Run macro on many cells pull data from web paste results next to relevant cell
    By elpettey in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-08-2012, 01:44 PM
  5. Excel Macro help to copy/paste results using input values from a list box
    By kamila7 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-13-2012, 04:46 PM
  6. Plotting with 3 columns data (X,Y and Values)
    By gundalav in forum Excel General
    Replies: 3
    Last Post: 07-01-2008, 02:13 PM
  7. calculating the results between two values
    By censura in forum Excel Formulas & Functions
    Replies: 14
    Last Post: 06-12-2007, 03:25 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