Cdoe Excluding specific Tabs

Peggy2015

Board Regular
Joined
Oct 19, 2015
Messages
109
Hello,

I have written code to copy down a formula in one cell into all the other cells in a column. I need to repeat this for several tabs in my workbook, however I want to exclude certain tabs from this task as they do not have the same information in their cells. How do I write this to exclude tabs called 'Analysis', 'Data' and 'Input Data'?

My current code is:

Sub Summary_JobCode_Formulas_CopiedDown2()
'
' Summary_JobCode_Formulas_CopiedDown Macro
' This is to copy the foluma at the top of the column and pull down to the remaining cells which will overwrite any hard keyed numbers that have been typed in.
'
ActiveSheet.Range("$A$6:$Y$288").AutoFilter Field:=1, Criteria1:="<>"
Selection.Copy
Range("P8:P278").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Range("P7").Select
ActiveSheet.Range("$A$6:$Y$281").AutoFilter Field:=1
Range("P7").Select
End Sub

Thanks for your help.

Best wishes,
Peggy
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi,

I would wrap your code with a loop which would go through all sheets and check their names before executing your code:

Dim i as integer

For i = 1 to Sheets.count
if Sheet(i).Name <> "Analysis" and Sheet(i).Name <> "Data" and Sheet(i).Name = "Input Data" Then
'your code here
End if
Next i

Hope this helps.
 
Upvote 0
Here is another structure that you can investigate.
Rich (BB code):
Sub Test()

Dim ws As Worksheet

For Each ws In Worksheets
  Select Case ws.Name
    Case "Analysis", "Data", "Input Data"
      'Do nothing
    Case Else
      With ws
        .Range("$A$6:$Y$288").AutoFilter Field:=1, Criteria1:="<>"
        
        ' Whatever else you want to do
        
  End Select
Next ws


End Sub
 
Upvote 0
Hi Peter,

I tried this code and a compile error comes up 'End Select without Select Case' I can't work out why?

Sub Test()
Dim ws As Worksheet
For Each ws In Worksheets
Select Case ws.Name
Case "Analysis", "Data", "Input Data"
'Do nothing
Case Else
With ws
.Range("$A$6:$Y$288").AutoFilter Field:=1, Criteria1:="<>"
Selection.Copy
Range("P8:P281").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Range("P7").Select
.Range("$A$6:$Y$281").AutoFilter Field:=1
Range("P7").Select
' Whatever else you want to do
End Select
 
Upvote 0
Sorry, my mistake. I omitted the red line
Rich (BB code):
Dim ws As Worksheet

For Each ws In Worksheets
  Select Case ws.Name
    Case "Analysis", "Data", "Input Data"
      'Do nothing
    Case Else
      With ws
        .Range("$A$6:$Y$288").AutoFilter Field:=1, Criteria1:="<>"
        
        ' Whatever else you want to do
      End With
  End Select
Next ws

However, the rest of your code will need modification but I can't do it for you (yet at least) as I don't know what you are trying to do.
You are copying the 'Selection' but when you run the macro, only one sheet (the ActiveSheet) can have something selected on it. So the code makes no sense to work through a number of sheets copying the selection.
Also, any range on the various worksheets that the code is being run on will need to be prefixed with a "." as I did with .Range("$A$6:$Y$288")

If you can't make it do what you want, please explain carefully exactly what you want to happen as the code loops through each sheet.

Also, when posting code, please indent your code and post it using Code Tags. My signature block below explains how to do that.
 
Last edited:
Upvote 0
Hi Peter,

Code still didn't work. I've probably deleted key instructions and just left basic things like .paste and the code doesn't know what to do. Here's what I am trying to achieve:

Goal: clean spreadsheets where each cell that has a formula in it is in place. throughout the month my colleagues will update their workbooks and type over a formula to put a number in so they can update their forecasts. However, I all these spreadsheets in a workbook are dependant on formulas in order to pull in the actual numbers from downloaded information. Ultimately I am trying to write code that will update all these spreadsheets that look like this in each workbook where the period has changed to 'Actual' and just leave the 'Forecast' columns alone until we move into that month where it changes to Actual. Here's an example of what the spreadsheet looks like:

[TABLE="width: 1231"]
<tbody>[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Actual
[/TD]
[TD]Actual
[/TD]
[TD]Actual
[/TD]
[TD]Actual
[/TD]
[TD]Actual
[/TD]
[TD]Actual
[/TD]
[TD]Actual
[/TD]
[TD]Actual
[/TD]
[TD]Actual
[/TD]
[TD]Actual
[/TD]
[TD]Actual
[/TD]
[TD]Forecast
[/TD]
[TD]Forecast
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Period
[/TD]
[TD]Period
[/TD]
[TD]Period
[/TD]
[TD]Period
[/TD]
[TD]Period
[/TD]
[TD]Period
[/TD]
[TD]Period
[/TD]
[TD]Period
[/TD]
[TD]Period
[/TD]
[TD]Period
[/TD]
[TD]Period
[/TD]
[TD]Period
[/TD]
[TD]Period
[/TD]
[/TR]
[TR]
[TD]Filter
[/TD]
[TD][/TD]
[TD]Acc
[/TD]
[TD]Account Name
[/TD]
[TD]1
[/TD]
[TD]2
[/TD]
[TD]3
[/TD]
[TD]4
[/TD]
[TD]5
[/TD]
[TD]6
[/TD]
[TD]7
[/TD]
[TD]8
[/TD]
[TD]9
[/TD]
[TD]10
[/TD]
[TD]11
[/TD]
[TD]12
[/TD]
[TD]13
[/TD]
[/TR]
[TR]
[TD]'
[/TD]
[TD][/TD]
[TD]1100
[/TD]
[TD]Recurrent Grant - Teaching
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[/TR]
[TR]
[TD]!
[/TD]
[TD][/TD]
[TD]1103
[/TD]
[TD]Joint Info System Grant (JISC)
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]3.00
[/TD]
[TD="align: right"]0.00
[/TD]
[/TR]
[TR]
[TD]'
[/TD]
[TD][/TD]
[TD]1151
[/TD]
[TD]Grant Transfers
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD="align: right"]11
[/TD]
[TD]FUNDING COUNCIL GRANTS
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[/TR]
[TR]
[TD]'
[/TD]
[TD][/TD]
[TD]1260
[/TD]
[TD]Non-CR Bearing & CPD Fees
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[/TR]
[TR]
[TD]!
[/TD]
[TD][/TD]
[TD]1262
[/TD]
[TD]SITS PG ODL Fees
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]5.00
[/TD]
[TD="align: right"]0.00
[/TD]
[/TR]
[TR]
[TD]!
[/TD]
[TD][/TD]
[TD]1290
[/TD]
[TD]Research Overheads
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]10.00
[/TD]
[TD="align: right"]0.00
[/TD]
[/TR]
[TR]
[TD]'
[/TD]
[TD][/TD]
[TD]1292
[/TD]
[TD]Res Ind. HSS Cost - Sustain
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[/TR]
[TR]
[TD]'
[/TD]
[TD][/TD]
[TD]1296
[/TD]
[TD]Res Ind. SCE Cost - Sustain
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[/TR]
[TR]
[TD]'
[/TD]
[TD][/TD]
[TD]1298
[/TD]
[TD]Res PI & CO-PI Dir Cost
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD="align: right"]14
[/TD]
[TD]RESEARCH GRANTS & CONTRACTS
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[/TR]
[TR]
[TD]'
[/TD]
[TD][/TD]
[TD]1401
[/TD]
[TD]Software Royalty Income
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[TD="align: right"]0.00
[/TD]
[/TR]
</tbody>[/TABLE]


The rows labelled '11Funding council Grants' & '14Research Grants & Contracts' have subtotal formulas in them so I've recorded the macro to put the filter on in column A to hide these so I can copy all the information in column Period 12, for example. I got the basics to work for just one spreadsheet. However, when I use the code for multiple spreadsheets it doesn't work. Does this explain things a bit better?

I hope you can help.

Peggy
 
Last edited:
Upvote 0
Does this explain things a bit better?
It is not entirely clear, but see if this points you in the right direction.
I don't know exactly where the formula that you want to re-apply down a column is stored, or whether it is the same formula for each sheet.

Anyway, the code below will work through all the sheets except "Analysis", "Data" & "Input Data" & on each sheet ..
- Filter the range A6:Y288 (assumed headings in row 6) for non-blanks in column A
- Copy whatever is in cell P1 (where I put my formula that I wanted re-populated in the table, but it could be anywhere) on that sheet and paste it in the range P7:P288 (it will miss the rows hidden by the filter, as you know)
- Unfilter column A

Code:
Sub Fix_Formulas()
  Dim ws As Worksheet
  
  For Each ws In Worksheets
    Select Case ws.Name
      Case "Analysis", "Data", "Input Data"
        'Do nothing
      Case Else
        With ws
          .Range("A6:Y288").AutoFilter Field:=1, Criteria1:="<>"
          .Range("P1").Copy Destination:=.Range("P7:P288")
          .Range("$A$6:$Y$288").AutoFilter Field:=1
        End With
    End Select
  Next ws
End Sub

Hopefully you can use that as a basis for getting the code to do what you want it to. :)
 
Upvote 0
Hi Peter,

Thank you for your reply and help with this. The code works really well.

How do I write a code to ask if A4:A15="Actual" then carry out this code and if they are labelled "Forecast" don't carry out the code and do nothing?

Best wishes,
Peggy




It is not entirely clear, but see if this points you in the right direction.
I don't know exactly where the formula that you want to re-apply down a column is stored, or whether it is the same formula for each sheet.

Anyway, the code below will work through all the sheets except "Analysis", "Data" & "Input Data" & on each sheet ..
- Filter the range A6:Y288 (assumed headings in row 6) for non-blanks in column A
- Copy whatever is in cell P1 (where I put my formula that I wanted re-populated in the table, but it could be anywhere) on that sheet and paste it in the range P7:P288 (it will miss the rows hidden by the filter, as you know)
- Unfilter column A

Code:
Sub Fix_Formulas()
  Dim ws As Worksheet
  
  For Each ws In Worksheets
    Select Case ws.Name
      Case "Analysis", "Data", "Input Data"
        'Do nothing
      Case Else
        With ws
          .Range("A6:Y288").AutoFilter Field:=1, Criteria1:="<>"
          .Range("P1").Copy Destination:=.Range("P7:P288")
          .Range("$A$6:$Y$288").AutoFilter Field:=1
        End With
    End Select
  Next ws
End Sub

Hopefully you can use that as a basis for getting the code to do what you want it to. :)
 
Upvote 0
How do I write a code to ask if A4:A15="Actual" then carry out this code and if they are labelled "Forecast" don't carry out the code and do nothing?
That is not specific enough. Remember that I have never seen your sheet, nor am I familiar with just what you are trying to do.

1. Where is A4:A15?
- Is it on one particular sheet & a single decision has to be made whether to process the whole code or not at all? If so, what sheet?
- Is it on each of the sheets being processed and a decision is made sheet-by-sheet whether to process that sheet?
- Somewhere else?

2. What does if A4:A15 ="Actual" mean? A4:A15 is 12 cells.
- Do they all need to be "Actual"?
- Does just one of them need to be "Actual"?
- What should happen if one of those cells is "Actual" and another one is "Forecast"?
- Are they merged cells?
- Something else?
 
Last edited:
Upvote 0
Hi Peter,

Sorry that should be E1:Q1.

Actual is when our accounting period moves from 'Forecast' into the 'Actual' period for spending. They are not merged cells with anything, they are stand alone columns with 13 periods for the financial year.

I hope that makes more sense. Sorry for the confusion.

best wishes,
Peggy




That is not specific enough. Remember that I have never seen your sheet, nor am I familiar with just what you are trying to do.

1. Where is A4:A15?
- Is it on one particular sheet & a single decision has to be made whether to process the whole code or not at all? If so, what sheet?
- Is it on each of the sheets being processed and a decision is made sheet-by-sheet whether to process that sheet?
- Somewhere else?

2. What does if A4:A15 ="Actual" mean? A4:A15 is 12 cells.
- Do they all need to be "Actual"?
- Does just one of them need to be "Actual"?
- What should happen if one of those cells is "Actual" and another one is "Forecast"?
- Are they merged cells?
- Something else?
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
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