+ Reply to Thread
Results 1 to 10 of 10

macro for selecting scenario and copying results

Hybrid View

  1. #1
    Registered User
    Join Date
    08-16-2011
    Location
    Leeds
    MS-Off Ver
    Excel 2007
    Posts
    33

    macro for selecting scenario and copying results

    Hi,

    I have a drop-down list which looks at a range called scenarioIn with 1,2 and 3 as the possible options. When an option is selected outputs in a range called OutputsW need to be copied and pasted into a ranges called target1, target2 and target3 for the 3 different options. Can anyone suggest some simple code that will help me do this please? I will link it to a macro buton which will loop through when I need to refresh the results.

    Thank you

    Jeff

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: macro for selecting scenario and copying results

    Do you have a sample file that you can upload?

    To Attach a File:

    1. Click on Go Advanced
    2. In the frame Attach Files you will see the button Manage Attachments
    3. Click the button.
    4. A new window will open titled Manage Attachments - Excel Forum.
    5. Click the Browse... button to locate your file for uploading.
    6. This will open a new window File Upload.
    7. Once you have located the file to upload click the Open button. This window will close.
    8. You are now back in the Manage Attachments - Excel Forum window.
    9. Click the Upload button and wait until the file has uploaded.
    10. Close the window and then click Submit.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    08-16-2011
    Location
    Leeds
    MS-Off Ver
    Excel 2007
    Posts
    33

    Re: macro for selecting scenario and copying results

    hi please see attachment

    thanks

    jeff
    Attached Files Attached Files

  4. #4
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: macro for selecting scenario and copying results

    If there is data existing in target1, target2 and target3, it should be cleared out before fresh data is input?

  5. #5
    Registered User
    Join Date
    08-16-2011
    Location
    Leeds
    MS-Off Ver
    Excel 2007
    Posts
    33

    Re: macro for selecting scenario and copying results

    Quote Originally Posted by arlu1201 View Post
    If there is data existing in target1, target2 and target3, it should be cleared out before fresh data is input?
    Hi, yes I would linke to clear out the data and then the macro would re-populate the ranges

  6. #6
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: macro for selecting scenario and copying results

    Try this code. Right click on the sheet, select View code and copy paste it into the code window. Then just change the dropdown and the code will work.
    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$B$9" Then
        Range("B12:B14,E12:E14,H12:H14").ClearContents
        If Target.Value = "1" Then
            Range("F5:F7").Copy
            Range("B12").PasteSpecial (xlPasteValues)
        ElseIf Target.Value = "2" Then
            Range("F5:F7").Copy
            Range("E12").PasteSpecial (xlPasteValues)
        Else
            Range("F5:F7").Copy
            Range("H12").PasteSpecial (xlPasteValues)
        End If
    End If
    End Sub

  7. #7
    Registered User
    Join Date
    08-16-2011
    Location
    Leeds
    MS-Off Ver
    Excel 2007
    Posts
    33

    Re: macro for selecting scenario and copying results

    thanks but i dont think i was clear with my initial thread. I wanted a macro linked to a button that when pressed would loop through all options on dropdown (1,2,3) and then populate all the ranges in one go.

  8. #8
    Registered User
    Join Date
    08-16-2011
    Location
    Leeds
    MS-Off Ver
    Excel 2007
    Posts
    33

    Re: macro for selecting scenario and copying results

    thanks but i dont think i was clear with my initial thread. I wanted a macro linked to a button that when pressed would clear the destination ranges, loop through all options on dropdown (1,2,3) and then populate all the ranges leaving data in all the destination ranges. Can you advise?

    thanks

    jeff

  9. #9
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: macro for selecting scenario and copying results

    Try this
    Option Explicit
    
    Sub copy_range()
    Dim i As Long, a As Long
    
    a = 2
    With Worksheets("Sheet1")
        For i = 2 To 8 Step 3
            .Range(.Cells(12, i), .Cells(14, i)).ClearContents
        Next i
        
        For i = 1 To 3
            .Range("B9").Value = i
            .Range("F5:F7").Copy
            .Cells(12, a).PasteSpecial (xlPasteValues)
            a = a + 3
        Next i
    End With
    
    End Sub
    Put it in a standard module and then you can assign it to a button.

  10. #10
    Registered User
    Join Date
    08-16-2011
    Location
    Leeds
    MS-Off Ver
    Excel 2007
    Posts
    33

    Re: macro for selecting scenario and copying results

    thanks works a treat, regards Jeff

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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