+ Reply to Thread
Results 1 to 15 of 15

Need VBA code that applies multiple sets of inputs to model and pastes each set of values

  1. #1
    Registered User
    Join Date
    03-20-2023
    Location
    New York, NY
    MS-Off Ver
    Microsoft? Excel? for Microsoft 365 MSO (Version 2302) 32-bit
    Posts
    22

    Need VBA code that applies multiple sets of inputs to model and pastes each set of values

    I am trying to take a data set which comprises many different sets of inputs, and run those inputs through a model on a separate sheet and return the relevant outputs back to the data set sheet, and then move to the next row and do the same.

    I've attached a sample excel file for context - the inputs to be applied to the "Model" sheet are found on the "Data Tape" sheet - each row in the "Data Tape" sheet represents a different scenario. The outputs to be pasted to columns E:G in "Data Tape" are cells C10:C12 of "Model" sheet.

    The end result should be a fully populated Data Tape sheet that ties with the "checks" to the right. This will need to be stable for a few hundred rows without crashing. Really appreciate any help - thank you.
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Need VBA code that applies multiple sets of inputs to model and pastes each set of val

    Hi, ascheetz1. Try with:

    PHP Code: 
    Sub Macro8()
    Dim aQ&, i&
    '-------->
    With Sheets("Data Tape")
      a = .Range("B4", .[d3].End(xlDown)): Q = UBound(a)
    End With
    '
    -------->
    With Sheets("Model")
      For 
    1 To Q
        
    .[b4:d4] = Array(a(i1), a(i2), a(i3))
        
    a(i1) = .[c10].Valuea(i2) = .[c11].Valuea(i3) = .[c12].Value
      Next
    End With
    '-------->
    Range("'
    Data Tape'!E4:G4").Resize(Q) = a
    End Sub 
    You are always very welcome if you add reputation by clicking the * (bottom left) of each message that has helped you.

  3. #3
    Registered User
    Join Date
    03-20-2023
    Location
    New York, NY
    MS-Off Ver
    Microsoft? Excel? for Microsoft 365 MSO (Version 2302) 32-bit
    Posts
    22

    Re: Need VBA code that applies multiple sets of inputs to model and pastes each set of val

    Thanks beyond_Excel! Curious if you / anyone sees an issue with how I ended up doing it. See attached excel // code below:

    PHP Code: 
    Sub Run_All_Scenarios()
    Dim i As Integer
    Application
    .ScreenUpdating False

    For 1 To Range("Scenario_Total")
    Worksheets("Model").Activate
        Range
    ("Scenario_Nb") = i
        
        Application
    .Goto Reference:="Results"
        
    Selection.Copy
        Worksheets
    ("Data Tape").Activate
        Range
    ("First_Scenario").Offset(10).Select
            Selection
    .PasteSpecial Paste:=xlPasteValuesOperation:=xlNoneSkipBlanks _
            
    :=FalseTranspose:=True
        Next
        
    Application
    .ScreenUpdating True

    End Sub 
    Attached Files Attached Files

  4. #4
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Need VBA code that applies multiple sets of inputs to model and pastes each set of val

    Quote Originally Posted by ascheetz1 View Post
    Thanks beyond_Excel! Curious if you / anyone sees an issue with how I ended up doing it. See attached excel // code below:
    Any problem with the code that I gave you?...

  5. #5
    Registered User
    Join Date
    03-20-2023
    Location
    New York, NY
    MS-Off Ver
    Microsoft? Excel? for Microsoft 365 MSO (Version 2302) 32-bit
    Posts
    22

    Re: Need VBA code that applies multiple sets of inputs to model and pastes each set of val

    Not at all! Planning to use yours - just trying to understand the limits of my original approach. Thanks again.

  6. #6
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Need VBA code that applies multiple sets of inputs to model and pastes each set of val

    What you have programmed delivers the desired result.
    However it is ineffective: you go back and forth between sheets and that is never necessary.

  7. #7
    Registered User
    Join Date
    03-20-2023
    Location
    New York, NY
    MS-Off Ver
    Microsoft? Excel? for Microsoft 365 MSO (Version 2302) 32-bit
    Posts
    22

    Re: Need VBA code that applies multiple sets of inputs to model and pastes each set of val

    Understood, makes sense. In the actual use case I am going to have a much larger array of inputs (more like 30 input cells than 3 input cells). Is there a way to adjust the below section of code so that I don't need to manually assign each individual cell to the array?

    PHP Code: 
    .[b4:d4] = Array(a(i1), a(i2), a(i3))
        
    a(i1) = .[c10].Valuea(i2) = .[c11].Valuea(i3) = .[c12].Value 

  8. #8
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Need VBA code that applies multiple sets of inputs to model and pastes each set of val

    This code snippet is not the only thing you have to adapt.
    Look what follows:

    PHP Code: 
    Sub Macro9()
    Const 
    qCol% = 3
    Dim a
    Q&, i&, j%
    Rem -------->
    With Sheets("Data Tape")
      
    = .Range("B4", .[b3].End(xlDown)).Resize(, qCol): UBound(a)
    End With
    Rem 
    -------->
    With Sheets("Model")
      For 
    1 To Q
        
    .[b4].Resize(, qCol) = Application.Index(ai0)
        For 
    1 To qCol
          a
    (ij) = .Range("C10")(j1)
        
    Next
      Next
    End With
    Rem 
    -------->
    Range("'Data Tape'!B4").Offset(, qCol).Resize(QqCol) = a
    End Sub 
    The variable 'qCol' indicates the number of columns to take into account.

  9. #9
    Registered User
    Join Date
    03-20-2023
    Location
    New York, NY
    MS-Off Ver
    Microsoft? Excel? for Microsoft 365 MSO (Version 2302) 32-bit
    Posts
    22

    Re: Need VBA code that applies multiple sets of inputs to model and pastes each set of val

    I tried adjusting qCol to 15 for a larger example set and it doesn't appear to be working. Do you mind taking a look at the attached file and let me know where I'm going wrong?

    The intended results to be pasted into Q4:Z23 of "Data Tape" are found in columns AB:AK (calculated manually).

    Once I have this example figured out I should be able to adapt for the actual use case - really appreciate all of your help.
    Attached Files Attached Files

  10. #10
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Need VBA code that applies multiple sets of inputs to model and pastes each set of val

    In your initial post: the input data was 3 and the output data -also- was 3.

    In this new model: the input data is 15 and the output data is 10: that difference is very important!

  11. #11
    Registered User
    Join Date
    03-20-2023
    Location
    New York, NY
    MS-Off Ver
    Microsoft? Excel? for Microsoft 365 MSO (Version 2302) 32-bit
    Posts
    22

    Re: Need VBA code that applies multiple sets of inputs to model and pastes each set of val

    Sorry - the new model more accurately reflects the reality. The # of inputs and # of outputs are not going to be the same number.

  12. #12
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Need VBA code that applies multiple sets of inputs to model and pastes each set of val

    And now the variables are: Input% and Output% :
    Attached Files Attached Files

  13. #13
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Need VBA code that applies multiple sets of inputs to model and pastes each set of val

    I am going to make a comment that you should take into account in future consultations in the Forums:

    - It is almost never important how many rows your model has.
    - The exact data in your model is almost never important unless you have to look for something specific in the cells.
    - But the number of columns in the model should always reflect the actual situation.

    Understood?...

  14. #14
    Registered User
    Join Date
    03-20-2023
    Location
    New York, NY
    MS-Off Ver
    Microsoft? Excel? for Microsoft 365 MSO (Version 2302) 32-bit
    Posts
    22

    Re: Need VBA code that applies multiple sets of inputs to model and pastes each set of val

    Yes - thank you for all of your help!

  15. #15
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Need VBA code that applies multiple sets of inputs to model and pastes each set of val

    It was a pleasure helping you and thanks for the +Rep.

+ 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. Suggested vba code that applies to multiple check boxes
    By Romela in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-12-2020, 08:12 PM
  2. Trying to build a model with dates as inputs.
    By misterv in forum Excel General
    Replies: 3
    Last Post: 11-14-2019, 09:02 AM
  3. Obtaining set of outputs for multiple sets of inputs
    By rypalan in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-30-2019, 12:06 PM
  4. VBA Code copies and pastes the formulas? I only need the values.
    By tysonbly in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-14-2015, 11:20 AM
  5. [SOLVED] Addition to code to copy and pastes special values and cell format, but not links.
    By singerbatfink in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-13-2014, 03:10 PM
  6. Replies: 1
    Last Post: 10-03-2012, 07:06 PM
  7. Replies: 2
    Last Post: 09-05-2012, 03:20 PM

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