+ Reply to Thread
Results 1 to 3 of 3

copy paste data

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-16-2010
    Location
    glasgow scothland
    MS-Off Ver
    Excel 2007
    Posts
    112

    copy paste data

    Hello,

    I have a simple example workbook that copies 4 rows and pastes them into cell a2 of the same sheet.
    this works fine because i just want it simple as it is.

    Problem is i have included that if the value of another cell is greater than a set number then the code generates an error
    which it does.
    but problem further is the error generates even when the condition is not met.

    I would appreciate if someone could point me in the correct direction.
    please don't offer more code i would just like to understand what is wrong with what i have.

    i understand it's not well written but i want to understand the flow of the problem.

    Thanks.
    Attached Files Attached Files

  2. #2
    Forum Guru benishiryo's Avatar
    Join Date
    03-25-2011
    Location
    Singapore
    MS-Off Ver
    Excel 2013
    Posts
    5,156

    Re: copy paste data

    hi mdshotgun. i've got to put a disclaimer first as i'm not into VBA. but it seems like you should put your error msg within your IF statement like this:
    Option Explicit
    
    Private Sub CommandButton1_Click()
      
       Range("A2").ClearContents
        Range("d10:e13").Select
        
        If Range("h15").Value > 19 Then
          GoTo err1
    err1:     MsgBox ("Text is too long")
        End If
       
       With Selection
         .Copy
       End With
       
         Call PasteSelect
           Application.CutCopyMode = False
    
    End Sub
    otherwise, your codes skip the "GoTo err1" because H15 is not more than 19. then it runs the whole code until "err1: MsgBox ("Text is too long")" & runs it anyway. coloured the code i cut red

    Thanks, if you have clicked on the * and added our rep.

    If you're satisfied with the answer, click Thread Tools above your first post, select "Mark your thread as Solved".

    "Contentment is not the fulfillment of what you want, but the realization of what you already have."


    Tips & Tutorials I Compiled | How to Get Quick & Good Answers

  3. #3
    Forum Contributor
    Join Date
    05-16-2010
    Location
    glasgow scothland
    MS-Off Ver
    Excel 2007
    Posts
    112

    Re: copy paste data

    Thanks Benishiryo,

    That sorts the error problem.
    For some reason i presumed if you had an goto error,it had to go to the error outside the if statement.
    But i was wrong and learned something new today.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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