+ Reply to Thread
Results 1 to 7 of 7

Loop through drop down and copy/paste values

Hybrid View

dchiburg Loop through drop down and... 08-31-2016, 10:57 AM
Olly Re: Loop through drop down... 08-31-2016, 11:04 AM
dchiburg Re: Loop through drop down... 08-31-2016, 11:15 AM
Olly Re: Loop through drop down... 08-31-2016, 11:42 AM
dchiburg Re: Loop through drop down... 08-31-2016, 11:58 AM
dchiburg Re: Loop through drop down... 08-31-2016, 12:06 PM
Olly Re: Loop through drop down... 08-31-2016, 12:10 PM
  1. #1
    Registered User
    Join Date
    08-31-2016
    Location
    Texas, USA
    MS-Off Ver
    2013
    Posts
    4

    Loop through drop down and copy/paste values

    Hey guys,

    Wanting to create a macro that can loop through a drop down menu (O19), refresh worksheet, and then copy and paste range (B13:AF130) as values to specific worksheets (ex: drop down option 1 pastes to worksheet 1, option 2 pastes to worksheet 2).

    Any help would be greatly appreciated!

    Thank you!
    -J

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Loop through drop down and copy/paste values

    Attach a sample workbook. 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.
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Registered User
    Join Date
    08-31-2016
    Location
    Texas, USA
    MS-Off Ver
    2013
    Posts
    4

    Re: Loop through drop down and copy/paste values

    Hopefully the attached workbook will help.
    Attached Files Attached Files

  4. #4
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Loop through drop down and copy/paste values

    Okay, try this:

    Sub RunDropdownScenarios()
        Dim cDD As Range
        Dim sDD() As String
        Dim i As Integer
        Dim sWS As String
        Dim ws As Worksheet
        
        On Error GoTo Terminate
        
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
        
        With Worksheets("Before")
            Set cDD = .Range("O19")
            sDD = Split(cDD.Validation.Formula1, ",")
            For i = LBound(sDD) To UBound(sDD)
                cDD.Value = sDD(i)
                sWS = "After " & sDD(i)
                If SheetExists(sWS) Then
                    Set ws = Worksheets(sWS)
                Else
                    Set ws = Worksheets.Add(after:=Worksheets(Worksheets.Count))
                    ws.Name = sWS
                End If
                .Range("C13:AF130").Copy
                ws.Range("B13").PasteSpecial xlPasteValuesAndNumberFormats
                ws.Range("B13").PasteSpecial xlPasteFormats
                Application.CutCopyMode = False
            Next i
        End With
    Terminate:
        If Err Then
            Debug.Print "ERROR", Err.Number, Err.Description
            Err.Clear
        End If
        With Application
            .EnableEvents = True
            .ScreenUpdating = True
        End With
    End Sub
    
    Function SheetExists(ByRef sSheetName As String) As Boolean
        Dim ws As Worksheet
        For Each ws In ThisWorkbook.Worksheets
            If ws.Name = sSheetName Then
                SheetExists = True
                Exit Function
            End If
        Next ws
        SheetExists = False
    End Function
    Your source / destination copy and paste ranges don't seem quite consistent, in your sample workbook, so be careful of where you are copying and pasting...

  5. #5
    Registered User
    Join Date
    08-31-2016
    Location
    Texas, USA
    MS-Off Ver
    2013
    Posts
    4

    Re: Loop through drop down and copy/paste values

    Thanks! Is there a way to paste the data into specific worksheets without referencing the data in the drop down though?

    ex:
    Option 1 pastes to specific worksheet (Dummysheet)
    Option 2 pastes to specific worksheet (Finance)

    Thanks!

  6. #6
    Registered User
    Join Date
    08-31-2016
    Location
    Texas, USA
    MS-Off Ver
    2013
    Posts
    4

    Re: Loop through drop down and copy/paste values

    Scratch that, got it! Thanks so much!

  7. #7
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Loop through drop down and copy/paste values

    Sure - you need to map options to sheets. Something like this:

    Sub RunDropdownScenarios()
        Dim cDD As Range
        Dim sDD() As String
        Dim i As Integer
        Dim sWS() As String
        Dim ws As Worksheet
        
        On Error GoTo Terminate
        
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
        
        sWS = Split("Dummysheet,Finance,Third Option,four,CINQ,hexy,LuckySeven,OCT,999,Last", ",")
        
        With Worksheets("Before")
            Set cDD = .Range("O19")
            sDD = Split(cDD.Validation.Formula1, ",")
            For i = LBound(sDD) To UBound(sDD)
                cDD.Value = sDD(i)
                If SheetExists(sWS(i)) Then
                    Set ws = Worksheets(sWS(i))
                Else
                    Set ws = Worksheets.Add(after:=Worksheets(Worksheets.Count))
                    ws.Name = sWS(i)
                End If
                .Range("C13:AF130").Copy
                ws.Range("B13").PasteSpecial xlPasteValuesAndNumberFormats
                ws.Range("B13").PasteSpecial xlPasteFormats
                Application.CutCopyMode = False
            Next i
        End With
    Terminate:
        If Err Then
            Debug.Print "ERROR", Err.Number, Err.Description
            Err.Clear
        End If
        With Application
            .EnableEvents = True
            .ScreenUpdating = True
        End With
    End Sub
    
    Function SheetExists(ByRef sSheetName As String) As Boolean
        Dim ws As Worksheet
        For Each ws In ThisWorkbook.Worksheets
            If ws.Name = sSheetName Then
                SheetExists = True
                Exit Function
            End If
        Next ws
        SheetExists = False
    End Function

+ 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. Loop Find Last instance of Values in another Worksheet - then Copy & Paste
    By smartbuyer in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-14-2016, 07:30 PM
  2. Replies: 1
    Last Post: 05-14-2014, 12:52 AM
  3. [SOLVED] Destination copy and paste (values only) for copy loop
    By mr_mango81 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-01-2013, 08:59 PM
  4. Copy and paste as values in vba loop code
    By Kakkmaddafakka in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-15-2012, 08:24 AM
  5. Loop to copy and paste cell values
    By prontrad in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-08-2011, 09:52 AM
  6. Copy & Paste Values in a Loop
    By rhudgins in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-05-2011, 05:56 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