3

I'm quite new to Excel and completely new to this forum.

I have taken the code from the below forum and modified it to my need.

http://pressf1.pcworld.co.nz/showthread.php?90122-Creating-Macro-to-copy-and-paste-data-into-the-next-empty-column.

Sub copyTotals()
    Dim TargetSht As Worksheet, SourceSht As Worksheet, SourceRow As Integer, SourceCells As Range
    Set SourceSht = ThisWorkbook.Sheets("DUN - Jan")
    Set TargetSht = ThisWorkbook.Sheets("DUN Jan - Jan,Feb,Mar,Apr")
    Set SourceCells = SourceSht.Range("L36,N36")
    If TargetSht.Range("C11").Value = "" Then
        SourceRow = 1
    ElseIf TargetSht.Range("C41") <> "" Then
        MsgBox ("The sheet is full, you need to create a new sheet")
    Else
        SourceRow = TargetSht.Range("C41").End(xlUp).Row + 1
    End If

    SourceCells.Copy TargetSht.Cells(SourceRow, 3)
End Sub

The problem is that the values pasted have the formating of the source and i only want to paste the values.

Can someone please help me with this.

Scimonster
  • 32,893
  • 9
  • 77
  • 89
maldivianGeek
  • 89
  • 1
  • 2
  • 10

4 Answers4

4

Use .Copy together with .PasteSpecial. Instead of:

SourceCells.Copy TargetSht.Cells(2, SourceCol)

Do this:

SourceCells.Copy
TargetSht.Cells(2, SourceCol).PasteSpecial xlPasteValues
Dan
  • 10,480
  • 23
  • 49
2

Using the macro recorder yields this kind of thing. I use it a lot when stuck.

Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
2

You don't need to copy and paste to do this (at least in Excel 2010 and above, I think). You just set the .Value of the range to equal itself. eg:

rng.Value = rng.Value

or

Sheet1.Range("A1:A10").Value = Sheet1.Range("A1:A10").Value

since .Value "Returns or sets a Variant value that represents the value of the specified range."

Note that this just works for values, not formats.

http://msdn.microsoft.com/en-us/library/office/ff195193(v=office.15).aspx

SJC
  • 652
  • 6
  • 14
0

right click in the cell and select paste special then you can choose values only

edit: for macros its the same principle use .PasteSpecial xlPasteValues like here

Set rng6 = .Range("A3").End(xlDown).Offset(0, 41)
rng6.Copy
ActiveSheet.Paste Destination:=Worksheets("Positions").Range("W2")
ufosnowcat
  • 558
  • 2
  • 13