Can anyone help a newbie with a simple macro?

Jo-AnneC

New Member
Joined
Jan 25, 2005
Messages
13
I hope someone can help me to get this to work. It only works on the first sheet in the workbook, so I must have done something really stupid.... I have been trying to get it to work for hours and hours....ughhh!

Sub TestFile()
For Each Sh In ThisWorkbook.Worksheets
With Sh
Range("B3:K12").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues
Range("N3").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues
Range("M8:M12").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues
Range("B19:N23").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
End With
Next Sh
End Sub

Jo-Anne
 

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
In your with block, you then need to reference your sheet with a dot (.) i.e.

With Sh
.Range("B3:K12")
'etc....
End With

HTH.
 
Upvote 0
I don't think you can select anything on a worksheet that isn't currently active. Try doing this:

Code:
Sub TestFile()
For Each Sh In ThisWorkbook.Worksheets
Worksheets(Sh).Select
Range("B3:K12").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues
Range("N3").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues
Range("M8:M12").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues
Range("B19:N23").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Next Sh
End Sub
 
Upvote 0
Sh is probably declared as a worksheet, so you probably have to do something like:

Worksheets(Sh.Name).Activate

Or more simply:

Sh.Activate

HTH
 
Upvote 0
Sub TestFile()
For Each Sh In ThisWorkbook.Worksheets
With Sh
.Range("N3:W12").Value = Range("B3:K12").Value
.Range("M8:M12").Copy
.Range("M8:M12").PasteSpecial Paste:=xlPasteValues
.Range("B19:N23").Copy
.Range("B19:N23").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
End With
Next Sh
End Sub
 
Upvote 0
Welcome to the Board

How about:

Code:
Sub TestFile()
    Dim Sh As Worksheet
        For Each Sh In ThisWorkbook.Worksheets
            Sh.Activate
            With Range("B3:K12")
                .Copy
                .PasteSpecial Paste:=xlPasteValues
            End With
            With Range("N3")
                .Copy
                .PasteSpecial Paste:=xlPasteValues
            End With
            With Range("M8:M12")
                .Copy
                .PasteSpecial Paste:=xlPasteValues
            End With
            With Range("B19:N23")
                .Copy
                .PasteSpecial Paste:=xlPasteValues
            End With
    Next Sh
    Application.CutCopyMode = False
End Sub
Hope that helps,

Smitty
 
Upvote 0
martinee said:
In your with block, you then need to reference your sheet with a dot (.) i.e.

With Sh
.Range("B3:K12")
'etc....
End With

HTH.
Thank you. I have tried this now, but still could not get it to work. Maybe have dodgy fingers...
 
Upvote 0
Whooo ha! It works and I've got a bit giddy
Thank you soooooooooooo much Smitty!

What a crew!
Will be playing about with the other things that people so kindly sent to me. Very grateful for all of your help.
 
Upvote 0
Since you're just copying and "paste special-ing" as values, you don't need to actually copy at all... as in:
Code:
Sub TestFile()
Dim Sh As Worksheet
For Each Sh In ThisWorkbook.Worksheets
  With Sh
    .Range("B3:K12").Value = .Range("B3:K12").Value
    .Range("N3").Value = .Range("N3").Value
    .Range("M8:M12").Value = .Range("M8:M12").Value
    .Range("B19:N23").Value = .Range("B19:N23").Value
  End With
Next Sh
End Sub

Hope it helps,
Dan

Hiya Smitty! How's things treatin' you? :biggrin:
 
Upvote 0

Forum statistics

Threads
1,214,624
Messages
6,120,591
Members
448,973
Latest member
ksonnia

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