+ Reply to Thread
Results 1 to 12 of 12

macro not to copy zero value

  1. #1
    Registered User
    Join Date
    09-10-2010
    Location
    Bangkok
    MS-Off Ver
    Excel 2003
    Posts
    28

    Smile macro not to copy zero value

    Hi everybody,
    Can anyone help how to copy a data from one cell to another cell in the same sheet.
    Example A3 has a value of 100, I want to copy 100 to D15 to replace the original value
    at D15. However if A3 has a value of zero, macro will not copy the zero to D15. The
    original value (whatever it is) will remain unchanged.
    Please help. Thank you
    Last edited by poitachi; 09-11-2010 at 03:01 PM.

  2. #2
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,318

    Re: macro not to copy zero value

    Hello and Welcome to the Forum...

    How about...

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    09-10-2010
    Location
    Bangkok
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: macro not to copy zero value

    Thank you for the reply but I was unable to fix the problem. My old code is something like this:
    Sub inv()
    Range("M211").Select
    Selection.Copy
    Range("E5:F5").Select
    ActiveSheet.Paste
    Application.CutCopyMode=False
    End Sub
    I change it to:
    Sub inv()
    If Range("M211").Value=0 Then Exit Sub
    Range("M211").Copy Range("E5:F5")
    End Sub

    The value from M211 come from sum of other cell.
    Did I miss out any code?

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: macro not to copy zero value

    If you are copying the formula from M211 it won't work. If you copy the values only it should.

  5. #5
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: macro not to copy zero value

    Hi poitachi

    Please wrap your code in code tags like this
    Please Login or Register  to view this content.
    You'll get hammered otherwise.

    John
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  6. #6
    Registered User
    Join Date
    09-10-2010
    Location
    Bangkok
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: macro not to copy zero value

    Thanks John
    I have tried the code but it didn't work. I
    again the code like this:
    Sub inv()
    If Range("M211").Value=0 Then Exit Sub
    Range("M211").Select
    Selection.Copy
    Range("E5:F5").Select
    ActiveSheet.Paste
    Application.CutCopyMode=False
    End sub

    It works but if I have multiple selection of data to be copied, something went wrong again. Example;
    Sub inv()
    If Range("M211").Value =0 Then Exit Sub
    Range("M211").Select
    Selection.Copy
    Range("E5:F5").Select
    ActiveSheet.Paste
    Application.CutCopyMode=False

    If Range("M311").Value=0 Then Exit Sub
    Range("M311").Select
    Selection.Copy
    Range("E6:F6").Select
    ActiveSheet.Paste
    Application.CutCopyMode=False
    End Sub

    The problem now is that I can only get the data M211 copied to E5:F5. If M211 is zero then I can't get value from M311 (let's say value in M311 is 20) to be copied to E6:F6
    Please help. Thanks

  7. #7
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,318

    Re: macro not to copy zero value

    Give this a try...

    Please Login or Register  to view this content.

  8. #8
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: macro not to copy zero value

    Hi poitachi

    Please wrap your code in code tags. Click on Forum Rules to see how. You'll likely get no further responses until you've done so. (Except perhaps from a Forum Moderator).

    John

  9. #9
    Registered User
    Join Date
    09-10-2010
    Location
    Bangkok
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: macro not to copy zero value

    I have tried the above codes but it show a message box "Block if without end if". May I know what does it mean?

  10. #10
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,318

    Re: macro not to copy zero value

    Try...

    Please Login or Register  to view this content.
    Last edited by jeffreybrown; 09-11-2010 at 01:18 PM.

  11. #11
    Registered User
    Join Date
    09-10-2010
    Location
    Bangkok
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: macro not to copy zero value

    Thanks a lot. It works finally. I really appreciate all the efforts in helping me to overcome the trouble. Thanks again to everybody!

  12. #12
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,318

    Re: macro not to copy zero value

    You're welcome,

    If you are satisfied with the answer provided, please mark the thread as solved.
    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save

+ 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