+ Reply to Thread
Results 1 to 2 of 2

Copy paste - cute paste macro almost working

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-21-2007
    MS-Off Ver
    Microsoft 365 Apps for enterprise
    Posts
    389

    Copy paste - cute paste macro almost working

    Hello,

    I am using the 2 below macro one to CUT and PASTE and the other one to COPY and PASTE.

    The COPY PASTE macro works fine with my protected workbook but that macro allows me to select various rows or cells and gives me the following error:
    HTML Code: 
    --> So my question is would it be possible to only allow one row copy instead of various as workaround as the error doesn't occurred when selecting only one row?? Believe me I have tried to sort that issue out and check hat problem on many forums but I've never been able to find solution

    Sub COPYPASTE()
        ActiveSheet.Unprotect Password:="p@ssw0rd!"
        Dim rngSource As Range, rngDestination As Range
        
        On Error Resume Next
        Application.DisplayAlerts = False
        
        Set rngSource = Application.InputBox("Select the entire row to copy. ", "Select Cells", Type:=8)
        If rngSource Is Nothing Then GoTo Cleanup   'User canceled
        
        Set rngDestination = Application.InputBox("Select the destination cell to paste to. ", "Select Paste Destination", Type:=8)
        If rngDestination Is Nothing Then GoTo Cleanup  'User canceled
        
        On Error GoTo 0
    
        ActiveSheet.Unprotect Password:="p@ssw0rd!"
        
        rngSource.Copy
    '    rngDestination(1).PasteSpecial xlPasteValues
        rngDestination(1).PasteSpecial xlPasteValues
        Application.CutCopyMode = False
        Application.Goto rngDestination
        
    Cleanup:
        Application.DisplayAlerts = True
        ActiveSheet.Protect Password:="p@ssw0rd!", AllowFormattingCells:=True
    End Sub
    Sub CUTPASTE()
        Dim rngSource As Range, rngDestination As Range
        
        Set rngSource = ActiveCell.EntireRow
        
        On Error Resume Next
        Set rngDestination = Application.InputBox("The active cell's entire row will be cut. " & vbLf & _
                                                  "Select the destination cell to insert the cut row.", _
                                                  "Move Row", Type:=8)
        On Error GoTo 0
        
        If rngDestination Is Nothing Then Exit Sub 'User canceled
        
        ActiveSheet.Unprotect Password:="p@ssw0rd!"
            rngSource.Cut
            rngDestination.EntireRow.Insert Shift:=xlDown
        ActiveSheet.Protect Password:="p@ssw0rd!", AllowFormattingCells:=True
    End Sub
    Many Thanks,
    Graig

  2. #2
    Forum Contributor Gregor y's Avatar
    Join Date
    10-24-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2010 32-Bit
    Posts
    280

    Re: Copy paste - cute paste macro almost working

    my guess is you're looking for something similar to this
    Sub COPYPASTE()
        Dim ws As Worksheet
        Set ws = ActiveSheet
        ws.Unprotect Password:="p@ssw0rd!"
        Dim rngSource As Range, rngDestination As Range, rngArea As Range
        Dim lRowOffset As Long
        
        On Error Resume Next
        Application.DisplayAlerts = False
        
        Set rngSource = Application.InputBox("Select the entire row to copy. ", "Select Cells", Type:=8)
        If rngSource Is Nothing Then GoTo Cleanup   'User canceled
        
        Set rngDestination = Application.InputBox("Select the destination cell to paste to. ", "Select Paste Destination", Type:=8)
        If rngDestination Is Nothing Then GoTo Cleanup  'User canceled
        
        On Error GoTo 0
    
        ws.Unprotect Password:="p@ssw0rd!"
        
        lRowOffset = 0
        For Each rngArea In rngSource.Areas
            rngArea.Copy
            rngDestination(1).EntireRow(1).Offset(lRowOffset, 0).PasteSpecial xlPasteValues
            lRowOffset = lRowOffset + rngArea.Rows.Count
        Next rngArea
        'rngSource.Copy
        'rngDestination(1).PasteSpecial xlPasteValues
        Application.CutCopyMode = False
        Application.Goto rngDestination
        
    Cleanup:
        Application.DisplayAlerts = True
        ws.Protect Password:="p@ssw0rd!", AllowFormattingCells:=True
    End Sub
    
    Sub CUTPASTE()
        Dim ws As Worksheet: Set ws = ActiveSheet
        Dim rngSource As Range, rngDestination As Range, rngArea As Range
        Dim lRowOffset As Long
        
        Set rngSource = Selection.EntireRow
        Selection(1).Select
        
        On Error Resume Next
        Set rngDestination = Application.InputBox("The active cell's entire row will be cut. " & vbLf & _
                                                  "Select the destination cell to insert the cut row.", _
                                                  "Move Row", Type:=8)
        On Error GoTo 0
        
        If rngDestination Is Nothing Then Exit Sub 'User canceled
        Set rngDestination = rngDestination(1).EntireRow
        ws.Unprotect Password:="p@ssw0rd!"
    
        For Each rngArea In rngSource.Areas
            rngArea.Copy
            rngDestination.Insert xlShiftDown
            rngArea.Delete xlShiftUp
        Next rngArea
        Application.CutCopyMode = False
        ws.Protect Password:="p@ssw0rd!", AllowFormattingCells:=True
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Find, then copy and paste macro not working
    By ufopilot in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-13-2014, 06:46 AM
  2. [SOLVED] Macro to copy, then paste row in multiple sheets not working
    By dystopianprotagonist in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-17-2013, 09:37 PM
  3. Replies: 2
    Last Post: 03-27-2012, 02:49 PM
  4. copy paste macro not working
    By nicko54 in forum Excel General
    Replies: 1
    Last Post: 01-05-2011, 05:25 PM
  5. Copy/Paste Merged Cells via Macro is not working
    By Peter Bassett in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-27-2005, 04:06 PM

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