For Each Issues

Th3Hovster

New Member
Joined
Jul 7, 2017
Messages
3
Hello,

I am having a few issues with a for each macro I am working on. I am trying to compile information from forms in multiple worksheets into one list.

The information I need is in the same cells on all the forms and I have recorded the macro I need from one of the sheets and I am trying to apply that for all sheets in the workbook. My master is called HIT List and the way it sits now, it just pulls from HIT List instead of the other tabs and I'm also not sure if my paste in next empty cell portion is correct either.

Here is a portion of what I have so far, the rest of the code repeats this with just the cells changing:

Sub HITListCompile()
'
' HITListCompile Macro
'
For Each ws In ThisWorbook.Worksheets​
If ws.Name <> "HIT List" Then​
Range("C2").Select​
Application.CutCopyMode = False
Selection.Copy
Sheets("HIT List").Select
Range("A3").End(xlUp).Offset(1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("C3").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("HIT List").Select
Range("B3").End(xlUp).Offset(1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False​


Thanks in advance for taking a look
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
The issue is that looping through the worksheets like that does not actually select/activate them.
So, you either need to activate each one in your loop with a line under your "For each..." line like this:
Code:
ws.Activate
or you need to use worksheet references in your range references.
I did that, and rather than use Copy/Paste, I just set the value equal to one another.
And there is an issue with how you are trying to find the first available rows in column A and B.
Try this:
Code:
Sub HITListCompile()
'
' HITListCompile Macro

    Dim ms As Worksheet
    Dim ws As Worksheet

    Application.ScreenUpdating = False
    
    Set ms = Sheets("HIT List")

    For Each ws In ActiveWorkbook.Worksheets
        If ws.Name <> "HIT List" Then
            ms.Cells(Rows.Count, "A").End(xlUp).Offset(1) = ws.Range("C2")
            ms.Cells(Rows.Count, "B").End(xlUp).Offset(1) = ws.Range("C3")
        End If
    Next ws

    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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