+ Reply to Thread
Results 1 to 4 of 4

End vba if nothing to paste

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-21-2017
    Location
    England
    MS-Off Ver
    2007
    Posts
    409

    End vba if nothing to paste

    Hi I have the following code, but I don't want it to carry on after line 4 if there is nothing in the clipboard to paste instead I want it to come up with the following message and then end sub:

    MsgBox "Nothing to paste!"

    Sub Button4_Click()
        Dim last_row As Integer
    
        
        Cells(Rows.Count, 3).End(xlUp).Offset(1, -1).PasteSpecial Paste:=xlPasteValues
     
    
        
        'Selection.PasteSpecial Paste:=xlPasteValues'
       
        
        ActiveCell.Copy
        
        
         ActiveCell.Offset(0, 23).Select
         
         Selection.PasteSpecial Paste:=xlPasteValues
    
    
    
    End Sub
    many thanks

  2. #2
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,935

    Re: End vba if nothing to paste

    Without a sample workbook it's difficult to know if this is what you mean, but perhaps it will work...

    Sub Button4_Click()
        Dim last_row As Integer
    
        Cells(Rows.Count, 3).End(xlUp).Offset(1, -1).PasteSpecial Paste:=xlPasteValues
     
        If ActiveCell <> "" Then
            ActiveCell.Copy
            ActiveCell.Offset(0, 23).PasteSpecial Paste:=xlPasteValues
        Else
            MsgBox "Nothing to paste!",,""
        End If
    
    End Sub
    BSB

  3. #3
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: End vba if nothing to paste

    You can also use the

    Exit sub
    line to end the sub from any line.

  4. #4
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,935

    Re: End vba if nothing to paste

    Quote Originally Posted by maniacb View Post
    You can also use the

    Exit sub
    line to end the sub from any line.
    Correct, however, I would consider this bad programming practice and to be avoided wherever possible.
    A single exit point means code is easier to follow and less likely to cause headache further down the line.

    BSB

+ 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. copy formula and paste for new data added and autofill.....and paste special values
    By prabhuduraraj09 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-13-2014, 07:40 AM
  2. Replies: 7
    Last Post: 02-04-2014, 06:42 PM
  3. Trap Catch Differentiate Disable Paste Button from Paste Special Options
    By m3atball in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-23-2013, 07:28 PM
  4. [SOLVED] Need to modify the Paste function of this VBA Macro from "Paste" to "Paste Special Values"
    By zicitron in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-04-2013, 03:44 AM
  5. Replies: 1
    Last Post: 12-05-2012, 11:37 AM
  6. [SOLVED] how can I paste text using paste special, without clicking paste button?
    By Exxcel Noob in forum Excel General
    Replies: 6
    Last Post: 05-14-2012, 08:21 PM
  7. Replies: 3
    Last Post: 04-13-2012, 12:26 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