+ Reply to Thread
Results 1 to 5 of 5

Need to modify the Paste function of this VBA Macro from "Paste" to "Paste Special Values"

  1. #1
    Forum Contributor
    Join Date
    05-09-2013
    Location
    Mauritius
    MS-Off Ver
    Excel 2010
    Posts
    116

    Need to modify the Paste function of this VBA Macro from "Paste" to "Paste Special Values"

    Hi all,

    I got hold of this Macro from a website. It fits the job I'm after but it has only one glitch. I want the data to be pasted as "Paste Special Values" rather than the original Paste. Can anyone help on which part of the macro I need to modify for it to do a Paste Special Values?

    Sub CopyMultipleSelection()
    Dim SelAreas() As Range
    Dim PasteRange As Range
    Dim UpperLeft As Range
    Dim NumAreas As Integer, i As Integer
    Dim TopRow As Long, LeftCol As Integer
    Dim RowOffset As Long, ColOffset As Integer
    Dim NonEmptyCellCount As Integer
    ' Exit if a range is not selected
    If TypeName(Selection) <> "Range" Then
    MsgBox "Select the range to be copied. A multiple selection is allowed."
    Exit Sub
    End If
    ' Store the areas as separate Range objects
    NumAreas = Selection.Areas.Count
    ReDim SelAreas(1 To NumAreas)
    For i = 1 To NumAreas
    Set SelAreas(i) = Selection.Areas(i)
    Next
    ' Determine the upper left cell in the multiple selection
    TopRow = ActiveSheet.Rows.Count
    LeftCol = ActiveSheet.Columns.Count
    For i = 1 To NumAreas
    If SelAreas(i).Row < TopRow Then TopRow = SelAreas(i).Row
    If SelAreas(i).Column < LeftCol Then LeftCol = SelAreas(i).Column
    Next
    Set UpperLeft = Cells(TopRow, LeftCol)
    ' Get the paste address
    On Error Resume Next
    Set PasteRange = Application.InputBox _
    (Prompt:="Specify the upper left cell for the paste range:", _
    Title:="Copy Mutliple Selection", _
    Type:=8)
    On Error GoTo 0
    ' Exit if canceled
    If TypeName(PasteRange) <> "Range" Then Exit Sub
    ' Make sure only the upper left cell is used
    Set PasteRange = PasteRange.Range("A1")
    ' Check paste range for existing data
    NonEmptyCellCount = 0
    For i = 1 To NumAreas
    RowOffset = SelAreas(i).Row - TopRow
    ColOffset = SelAreas(i).Column - LeftCol
    NonEmptyCellCount = NonEmptyCellCount + _
    Application.CountA(Range(PasteRange.Offset(RowOffset, ColOffset), _
    PasteRange.Offset(RowOffset + SelAreas(i).Rows.Count - 1, _
    ColOffset + SelAreas(i).Columns.Count - 1)))
    Next i
    ' If paste range is not empty, warn user
    If NonEmptyCellCount <> 0 Then _
    If MsgBox("Overwrite existing data?", vbQuestion + vbYesNo, _
    "Copy Multiple Selection") <> vbYes Then Exit Sub
    ' Copy and paste each area
    For i = 1 To NumAreas
    RowOffset = SelAreas(i).Row - TopRow
    ColOffset = SelAreas(i).Column - LeftCol
    SelAreas(i).Copy PasteRange.Offset(RowOffset, ColOffset)
    Next i
    End Sub

  2. #2
    Forum Expert Debraj Roy's Avatar
    Join Date
    09-27-2012
    Location
    New Delhi,India
    MS-Off Ver
    Excel 2013
    Posts
    1,469

    Re: Need to modify the Paste function of this VBA Macro from "Paste" to "Paste Special Val

    Hi Zicitron!

    I would like to request you to read Forum Rules before posting..
    You post doesnot qualify Rule # 3

    3. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the [#] button at the top of the post window (if you are editing an existing post, press Go Advanced to see the [#] button).
    Its really hard to read code without indentation or formatting..

    Still try to search this line..
    SelAreas(i).Copy PasteRange.Offset(RowOffset, ColOffset)
    and replace it with
    Please Login or Register  to view this content.
    Regards!
    =DEC2HEX(3563)

    If you like someone's answer, click the star to give them a reputation point for that answer...

  3. #3
    Forum Contributor
    Join Date
    05-09-2013
    Location
    Mauritius
    MS-Off Ver
    Excel 2010
    Posts
    116

    Re: Need to modify the Paste function of this VBA Macro from "Paste" to "Paste Special Val

    Thank you Debraj. I'll take into account that rule next time! Sorry for that.

    I modified that line according to your solution and I get a "Compile error...End of Statement" message.

  4. #4
    Forum Expert Debraj Roy's Avatar
    Join Date
    09-27-2012
    Location
    New Delhi,India
    MS-Off Ver
    Excel 2013
    Posts
    1,469

    Re: Need to modify the Paste function of this VBA Macro from "Paste" to "Paste Special Val

    Hi zicitron..

    Please Login or Register  to view this content.
    Courtesy : John Walkenbach

  5. #5
    Forum Contributor
    Join Date
    05-09-2013
    Location
    Mauritius
    MS-Off Ver
    Excel 2010
    Posts
    116

    Re: Need to modify the Paste function of this VBA Macro from "Paste" to "Paste Special Val

    Thank you Debraj! Works perfectly! :-)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1