+ Reply to Thread
Results 1 to 5 of 5

If Then Else error

  1. #1
    Forum Expert
    Join Date
    01-05-2013
    Location
    Singapore
    MS-Off Ver
    H&B2016 & H&B2021
    Posts
    2,952

    If Then Else error

    Help! Urgent help needed!

    My macro copy only 1st item even though 2nd item also met the condition.

    Thanks so much!

    --------------------------------------------------------------
    Sub AddNewData()

    Dim lastrow As Long

    lastrow = Sheets("Table1").Range("B1048576").End(xlUp).Row + 1

    If Range("Item1").Value <> "ZZZZ" Then
    Range("ItemLine1").Copy
    Sheets("Table1").Range("B" & lastrow).PasteSpecial xlPasteValues
    Else

    If Range("Item2").Value <> "ZZZZ" Then
    Range("ItemLine2").Copy
    Sheets("Table1").Range("B" & lastrow).PasteSpecial xlPasteValues
    Else

    If Range("Item3").Value <> "ZZZZ" Then
    Range("ItemLine3").Copy
    Sheets("Table1").Range("B" & lastrow).PasteSpecial xlPasteValues
    Else

    End If
    End If
    End If

    End Sub
    --------------------------------------------------------------

  2. #2
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,412

    Re: If Then Else error

    Normal.
    If the first condition is met then it skips the other ones.
    Maybe something like this :

    Please Login or Register  to view this content.
    Note :
    Only keep the first
    Please Login or Register  to view this content.
    if you want to overwrite when you copy...
    GC Excel

    If this post helps, then click the star icon (*) in the bottom left-hand corner of my post to Add reputation.

  3. #3
    Forum Expert
    Join Date
    01-05-2013
    Location
    Singapore
    MS-Off Ver
    H&B2016 & H&B2021
    Posts
    2,952

    Re: If Then Else error

    Thanks, GC Excel!

    It works perfectly! Question: can I not repeat "lastrow =..."?

    Thanks again!

  4. #4
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,412

    Re: If Then Else error

    It depends. If all 3 values are <> "ZZZZ", do you want to write all 3? If yes, then you need to keep it.

  5. #5
    Forum Expert
    Join Date
    01-05-2013
    Location
    Singapore
    MS-Off Ver
    H&B2016 & H&B2021
    Posts
    2,952

    Re: If Then Else error

    Thanks, so much!

+ 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. Excel macro (compile error. syntax error.) error
    By salar_younis in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-06-2014, 06:11 AM
  2. Receiving following error “Complie error : syntax error” Help
    By masond3 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-02-2012, 10:19 AM
  3. Error "run-time Error '1004': General Odbc Error
    By D4WNO77 in forum Access Tables & Databases
    Replies: 2
    Last Post: 07-16-2012, 09:55 AM
  4. Error 75 File/Path access error, sometimes Error 1004
    By smokebreak in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 02-16-2011, 02:35 PM
  5. [SOLVED] Error Handling - On Error GoTo doesn't trap error successfully
    By David in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-16-2006, 02:10 PM

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