Copy same specific cells from only certain worksheets

Corky112

New Member
Joined
Aug 20, 2020
Messages
1
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I am very new and inexperienced at VBA & thank you to anyone and everyone for advice with this problem.

I worked on this macro, but I have a few challenges I'm hoping someone here can help with.

How do I tell he loop to only go though so many worksheets, I have pivot tables at the back of the workbook I don't want or need copied.

Is it possible to start with the first sheet in the workbook end the loop when it 'finds' the "Master" tab so I can keep my support files behind and have them not copied?



Sub CombineData()
Dim ws As Worksheet
Application.ScreenUpdating = False

For Each ws In ActiveWorkbook.Worksheets
If ws.Name <> "Master" Then
ws.Range("A17:N154").Copy
Sheets("Master").Cells(Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial xlPasteValues
End If

Next
Application.ScreenUpdating = True

End Sub
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
You don't want the "Master" sheet included I assume. If you do, delete the "- 1"
Code:
For i = 1 To Worksheets("Master").Index - 1
Sheets(i).Range("A17:N154").Copy
etc etc
 
Upvote 0
Let's say you have 20 sheets in a workbook with the last 5 sheets not being relevant to what you want the code to do, and the master sheet could be anywhere in the order of the first 15 sheets.
VBA Code:
For i = 1 To 15
    If Sheets(i).Name <> "master" Then
        'Code to do stuff here
    End If
Next

Assume same set up with sheets but there are a couple of sheets you do not want to execute on in the first 15, name escept1 and except2.
VBA Code:
For i = 1 To 15
    If Sheets(i).Name <> "master"  And Sheets(i).Name <> "except1" And Sheets(i).Name <> "except2" Then
        'Code to do stuff here
    End If
Next
You would use Sheets(i) to reference the current sheet you are working with.
If you have more than two or three exceptions within the group you want to work, you might consider moving the sheets to a different position simply by dragging them to the right with the mouse pointer. Of course, some companies have restrictions on changing the arrangement of files because it can cause problems with formulas in the sheets.
 
Upvote 0
Try this
Code:
Sub Maybe()
Dim i As Long, shM As Worksheet
Set shM = Worksheets("Master")
    For i = 1 To ThisWorkbook.Worksheets("Master").Index - 1
        shM.Cells(Rows.Count, 1).End(xlUp).Offset(1).Resize(138, 14).Value = Sheets(i).Range("A17:N154").Value
    Next i
End Sub
 
Upvote 0
or if you want to exclude sheets wherever they are located and no matter how many sheets in the workbook
Code:
Sub Maybe_B()
    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets
        Select Case ws.Name
            Case "Master", "Sheet9", "Sheet10"   '<----- here all the sheet names to be excluded
                'do nothing
            Case Else
                Sheets("Master").Cells(Rows.Count, 1).End(xlUp).Offset(1).Resize(138, 14).Value = ws.Range("A17:N154").Value
        End Select
    Next ws
End Sub
 
Upvote 0
None of the suggestions worked? We have not heard anything about it.
 
Upvote 0
I guess I forget about weekends nowadays. Every day is Sunday!!!
Won't be long where every person I know will be a new face I've never met before.
I have this terrible habit of expecting people to be thankful.
Thanks JLGWhiz
 
Upvote 0
I guess I forget about weekends nowadays. Every day is Sunday!!!
Won't be long where every person I know will be a new face I've never met before.
I have this terrible habit of expecting people to be thankful.
Thanks JLGWhiz
Been there, done that. I am now at the stage where I don't know anybody because I have outlived everybody I once knew. Even if it is somebody I know, I probably won't remember their name. But it is nice living in my own world. :giggle:
 
Upvote 0

Forum statistics

Threads
1,214,991
Messages
6,122,628
Members
449,095
Latest member
bsb1122

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