Loop Through Validation List and Copy Values Given for Each Item

mjgcancio

New Member
Joined
Jun 8, 2017
Messages
17
Hi all.

I've been struggling with VBA (my knowledge is a bit short for this) and I could use some help.

I need that the current macro can run through all items of the data validation list "Data List" in Sheet1, and then item by item, copy the dynamic values in "Rows to Copy" to the correspondent row in "Rows to Paste".
Note: the formulas in "Rows to Copy" are:

Column D =(VLOOKUP(B2,H3:J28,2,FALSE))*(VLOOKUP(B2,H3:J28,3,FALSE))
Column E =(VLOOKUP(B2,H3:J28,2,FALSE))*(VLOOKUP(B2,H3:J28,3,FALSE))*D3
Column F =(VLOOKUP(B2,H3:J28,2,FALSE))*(VLOOKUP(B2,H3:J28,3,FALSE))*E33

So far I, and after some research and trial and error, I could only make it copy one by one, but repeating all rows and I'm stuck in here, through this code:

VBA Code:
Sub FillRows()
      
Dim rng As Range
Dim dataValidationArray As Variant
Dim i As Integer
Dim rows As Integer


'Set the cell which contains the Data Validation list
Set rng = ActiveSheet.Range("B2")

'If Data Validation list is not a range, ignore errors
On Error Resume Next


'Create an array from the Data Validation formula, without creating
'a multi-dimensional array from the range
rows = Range(Replace(rng.Validation.Formula1, "=", "")).rows.Count
ReDim dataValidationArray(1 To rows)

For i = 1 To rows
    dataValidationArray(i) = _
        Range(Replace(rng.Validation.Formula1, "=", "")).Cells(i, 1)
Next i


'If not a range, then try splitting a string
If Err.Number <> 0 Then
    Err.Clear
    dataValidationArray = Split(rng.Validation.Formula1, ",")
End If


'Some other error has occured so exit sub
If Err.Number <> 0 Then Exit Sub

'Reinstate error checking
On Error GoTo 0

    'Loop through all the values in the Data Validation Array
    For i = LBound(dataValidationArray) To UBound(dataValidationArray)

        'Change the value in the data validation cell
        rng.Value = dataValidationArray(i)

        'Force the sheet to recalculate
        Application.Calculate

''''''''''''''''''''''''''''''''''''''''''
'Start the code here''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''

        Dim z As Range
        
        For Each z In Range("K3:M28").rows

                Range("D3").Copy
                z.Cells(1).PasteSpecial Paste:=xlPasteValues
                Range("E3").Copy
                z.Cells(2).PasteSpecial Paste:=xlPasteValues
                Range("F3").Copy
                z.Cells(3).PasteSpecial Paste:=xlPasteValues

         Next
        
''''''''''''''''''''''''''''''''''''''''''
'End code here''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''

    Next i


'-----------------------------------------------------------------

  MsgBox ("Done")
 
End Sub



Thanks in advanced,

MC
 

Attachments

  • Sheet1.PNG
    Sheet1.PNG
    16.6 KB · Views: 19

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Ok so I found the solution.

Hope it might help someone.

VBA Code:
Sub Iteration_Loop()
Dim Rng As Range
Dim c As Range
Dim DestRow As Long

     'Set Rng to the list of values in the validation list
'Set Rng = Sheets("Analysis").Range(Sheets("Analysis").Range("B6").Validation.Formula1)
Set Rng = ActiveSheet.Range(ActiveSheet.Range("B2").Validation.Formula1)
DestRow = 0

     For Each c In Rng.Cells
     'Sheets("Analysis").Range("B6").Value = c.Value
     ActiveSheet.Range("B2").Value = c.Value
     Application.Calculate
     'Sheets("Analysis").Range("B10:N25").Copy
     ActiveSheet.Range("D3:F3").Copy
     'Sheets("Output Sheet").Range("C" & DestRow + 5).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
     ActiveSheet.Range("K" & DestRow + 3).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
     DestRow = DestRow + 1
     Next c
    
     Range("B2").Select

End Sub

Best regards
MC
 
Upvote 0

Forum statistics

Threads
1,214,990
Messages
6,122,625
Members
449,093
Latest member
catterz66

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top