Creating a Nested loop (copy between worksheet) within loop of auto-filtered criteria

winrow

New Member
Joined
Feb 2, 2018
Messages
23
Hello,

I am newly registered but not new to search in this forum for new approaches of better structuring my vba code. Still, now I am facing a wall with which I think the solution would be a nested loop (but I might be mistaken:confused:).

Document: 1 workbook, 13 sheets (the first is called "sheet1", the last one is called "compilation", in between sheets named "1" till "11")
Datasheet: "sheet1", data structured in columns A (question with number 01 till 11), columns B (contributors' name alphabetically ordered), column C (answer). The number of rows is the same in between the 3 columns.

What I have: a VBA code that runs a loop that will autofilter through the questions numbers and copy the filtered results to a worksheet.

Code:
Sub fun()
 
Dim o_cell As Range
Dim wkD As Worksheet
Dim LR As Variant
Dim i As Integer

'source data
Set o_cell = ThisWorkbook.Worksheets("<wbr>Sheet1").Range("A1").<wbr>CurrentRegion

'Destination sheet that is set 
Set wkD = ThisWorkbook.Worksheets("1")
 
'set Lastrow for column A. Columns A,B & C have the same number of rows
LR = o_cell.Cells(Rows.Count, 1).End(xlUp).Row
 
    For i = 1 To 11
   
        o_cell.AutoFilter field:=1, Criteria1:="=" & i & ".*", Operator:=xlAnd
        Range("A1:C" & LR).Copy
            wkD.Range("A1").PasteSpecial Paste:=xlPasteValues
        o_cell.AutoFilter
       
    Next i
 
End Sub

What I would like to do: the above code successively autofilter results from question 01 to 11 and copy-paste the results into another sheet that remains worksheet ("1"). Is it possible to modify this code to copy filtered results from question 1 to sheet "1", from question 2 to sheet "2" etc. till question 11 to sheet "11"? In the affirmative, how would you do it?

Many thanks in advance for your help!
 
Hello Georgiboy and Fluff. You made me switch on my computer again as I was curious to test your suggestions. I tested Fluff's code but I got a run time error 9 - subscript out of range for the line "set wkD".
 
Upvote 0

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Do you have a sheet called "01"?
 
Upvote 0
*facepalm*. I renamed my destination sheets accordingly. It is just perfect now. Thank you for your quick feedbacks and this elegant solution!
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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