Copy and paste VBA

jose001

Board Regular
Joined
Mar 27, 2006
Messages
103
Hi guys,

I have this little piece of code which some of you kindly helped me with a few weeks back. As you can see it copies a range from several sheets and pastes it into another called Printout. However, some of the cells contain formulas which are messing up. Is it possible to change the code to reflect a paste special (values) rather than a straight copy? Here is the code...

***************************************************************
Private Sub PrintHC_Click()
Dim i As Long
i = 2

Worksheets("Printout").Select

For Each sh In Worksheets
If sh.Name <> "Begin" And sh.Name <> "Index" And sh.Name <> "Codes" And sh.Name <> "Printout" Then
sh.Range("A2:M12").Copy Worksheets("Printout").Range("A" & i)
i = Worksheets("Printout").Cells(Rows.Count, 1).End(xlUp).Row + 2
End If
Next sh



End Sub

*********************************************************

Many Thanks

Jose
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.

Mike Blackman

Well-known Member
Joined
Jun 8, 2007
Messages
2,494
Try:

Code:
Private Sub PrintHC_Click()
Dim i As Long

i = 2

Worksheets("Printout").Select

For Each sh In Worksheets
    If sh.Name <> "Begin" And sh.Name <> "Index" And sh.Name <> "Codes" And sh.Name <> "Printout" Then
        sh.Range("A2:M12").Copy
             Worksheets("Printout").Range("A" & i).[B][COLOR="Red"]PasteSpecial Paste:=xlValues[/COLOR][/B]
        i = Worksheets("Printout").Cells(Rows.count, 1).End(xlUp).Row + 2
    End If
Next sh

End Sub
 
Last edited:
Upvote 0

Simon Lloyd

Well-known Member
Joined
Sep 10, 2006
Messages
756
Keeping the format you had and not tested
Code:
Private Sub PrintHC_Click()
Dim i As Long
i = 2

Worksheets("Printout").Select

For Each sh In Worksheets
If sh.Name <> "Begin" And sh.Name <> "Index" And sh.Name <> "Codes" And sh.Name <> "Printout" Then
sh.Range("A2:M12").Copy

With Worksheets("Printout").Range("A" & i)
.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
End With
i = Worksheets("Printout").Cells(Rows.Count, 1).End(xlUp).Row + 2
End If
Next sh


End Sub
 
Upvote 0

jose001

Board Regular
Joined
Mar 27, 2006
Messages
103
Hey Mikey,

thanks for the quick response, that works great but.....I just realised that I have a shaded background with borders and now it's obviously just copying the values :( How on earth can I get around copying the whole table and just values?
 
Upvote 0

njimack

Well-known Member
Joined
Jun 17, 2005
Messages
7,772
Code:
Private Sub PrintHC_Click()
Dim i As Long
i = 2

Worksheets("Printout").Select

For Each sh In Worksheets
If sh.Name <> "Begin" And sh.Name <> "Index" And sh.Name <> "Codes" And sh.Name <> "Printout" Then
sh.Range("A2:M12").Copy 

With Worksheets("Printout").Range("A" & i)
.PasteSpecial xlvalues
.PasteSpecial xlFormats
End With

Application.CutCopyMode = False 'clears the clipboard

i = Worksheets("Printout").Cells(Rows.Count, 1).End(xlUp).Row + 2
End If
Next sh

End Sub
 
Upvote 0

Forum statistics

Threads
1,191,690
Messages
5,988,061
Members
440,125
Latest member
vincentchu2369

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
Top