How to store the results of my for each loop If statement into another worksheet columns?

Michael Ziegler

New Member
Joined
Jul 2, 2019
Messages
8
I need to store the results of my for each If statement in the results worksheet of the active workbook.


My loop works but I need to be able to store the results in column a and c from the results worksheet


This is the code for the loop


Code:
 For Each cel In Range("A4:A85")

                With cel

                   If (.Value Like "boston*" Or .Value Like "manfield*" Or _
                .Value Like "barnes*" Or.Value Like "langley*") _
                And .Offset(0, 2).Value Like "mass*" Then



                     MsgBox cel.Value & Chr(13) & Chr(10) &  cel.Offset(0, 2).Value

I was thinking about something like this

Pseudocode: worksheets worksheetname column.value = cel.value and
cell.offset(0,2).value =cell.offset(0,2).value

Thanks
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
I would always do this type of task using variant arrays because it is much much faster, if you only have few lines then it doesn't matter but if you have 1000 then variant array are at least 1000 time faster and more flexible:
Try this code (untested)
Code:
Sub faster()
inarr = Range("a4:C85") ' Note i have changed this to include column C so that we can test what was offset 2
Dim cola(1 To 81, 1)  ' Note 1 to 81 is the same size as 4 to 85
Dim colc(1 To 81, 1)  ' Note 1 to 81 is the same size as 4 to 85
indi = 1
For i = 1 To 81
                     If (inarr(i, 1) Like "boston*" Or inarr(i, 1) Like "manfield*" Or _
                inarr(i, 1) Like "barnes*" Or inarr(i, 1) Like "langley*") _
                And inarr(i, 3) Like "mass*" Then
                 cola(indi, 1) = inarr(i, 1)
                 colc(indi, 1) = inarr(i, 3)
                 indi = indi + 1 ' increment the out index
                End If
Next i
With Worksheets("Results")
 Range(.Cells(1, 1), .Cells(81, 1)) = cola ' write the varaint array back to column A results worksheet
 Range(.Cells(1, 3), .Cells(81, 3)) = cola ' write the varaint array back to  Column C results worksheet
End With
                 
End Sub
 
Upvote 0
I have just spotted an error in my code ( due to cut and paste):

Code:
With Worksheets("Results")
 Range(.Cells(1, 1), .Cells(81, 1)) = cola ' write the varaint array back to column A results worksheet
 Range(.Cells(1, 3), .Cells(81, 3)) = [COLOR=#ff0000]colc[/COLOR] ' write the varaint array back to  Column C results worksheet
End With
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,632
Messages
6,120,649
Members
448,975
Latest member
sweeberry

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