+ Reply to Thread
Results 1 to 25 of 25

Creating IF statement

  1. #1
    Registered User
    Join Date
    02-26-2014
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    15

    Creating IF statement

    Hi everyone

    I am relatively new to VBA and am working on a piece of existing code after modifying an excel 2010 spreadsheet slightly. I have thrown this question out to Mr Excel however had not yet had an answer (and unfortunately I am working to a deadline at work with this sheet) and therefore I am hoping I may be able to locate an expert on this forum. http://www.mrexcel.com/forum/excel-q...statement.html If someone would be able to assist me, I would greatly appreciate it.


    The macro I'm using is throwing up an error; this is the code:

    Please Login or Register  to view this content.
    Occassionally there will be instances where there is no data to select (no rows at all - because any blanks have been deleted and there are no additional rows of data) and the macro throws up an object error at the CurrClaims.EntireRow.Select (line 7)

    Please Login or Register  to view this content.

    It was suggested to me that I put IF statements in the code to say if there is nothing in that range then do this (after delete old claims):
    Please Login or Register  to view this content.

    But IF there are claims then:
    Please Login or Register  to view this content.

    Would someone please be able to assist me with the code to make this happen? I am not experienced enough to work this out. I have tried various things and it seems to ignore what I am putting in there.

    Thanks very much
    Last edited by anoctua; 02-26-2014 at 06:09 PM.

  2. #2
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Creating IF statement

    Hi anoctua,

    Welcome to the forum and a fellow Ozzie!!

    Let us know how this goes:

    Please Login or Register  to view this content.
    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  3. #3
    Registered User
    Join Date
    02-26-2014
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Creating IF statement

    Hi Robert, my fellow Aussie!!

    I put in your code (in the way I understand I was supposed to... not very experienced here!) but it still throws up an "object required" error on the CurrClaims.EntireRow.Select line... Here's what I did - and by the way, I put the "end if" just before "'delete all blank rows from "current claims" tab" seeing as though if there are no claims, it only needs to do the actions from that line down... does that make sense?:


    Please Login or Register  to view this content.

  4. #4
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Creating IF statement

    What variable are you using to define CurrClaims?

  5. #5
    Registered User
    Join Date
    02-26-2014
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Creating IF statement

    Because I am not really awesome with this stuff - here's my whole code... maybe this will help!

    Please Login or Register  to view this content.

  6. #6
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Creating IF statement

    Defining your variables that way results in all but the last being defined as variant. This means that as CurrClaims is not a range my IF statement doesn't work on it.

    Use these and try again (note too that I use a long variable over an interger as the former can avoids the 32,767 limitation that the later has):

    Please Login or Register  to view this content.
    HTH

    Robert

  7. #7
    Registered User
    Join Date
    02-26-2014
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Creating IF statement

    Thanks Robert - hope I did this right, as unfortunately it's still throwing the object required error at the same line..

    Please Login or Register  to view this content.

  8. #8
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Creating IF statement

    You'll have to attach the workbook.

  9. #9
    Registered User
    Join Date
    02-26-2014
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Creating IF statement

    Okay - will just have to see if I am allowed given it's an internal document. I'll see if I can provide it if I blank out any identifying information... just give me a moment and I'll find out!

  10. #10
    Registered User
    Join Date
    02-26-2014
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Creating IF statement

    Alrighty, here it is, all blanked out but hopefully you will get the drift...
    Attached Files Attached Files

  11. #11
    Registered User
    Join Date
    02-26-2014
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Creating IF statement

    and here's what it does when there are actually claims to move:
    Attached Files Attached Files

  12. #12
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Creating IF statement

    I'm a little confused as the second file runs fine once, then errors out on a different part of the code

  13. #13
    Registered User
    Join Date
    02-26-2014
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Creating IF statement

    I think that would be because (I am presuming) the second time the macro is run, there is no data to select in "new claims". This won't ever be an issue because there will always be data in "new claims" to import into "current claims"; there just won't always be data to select from "current claims" to move into "old claims".

    That seems to be the issue, when there's claims to move from current to old, it works okay but as soon as there is nothing and no range to select, it gets upset.

  14. #14
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Creating IF statement

    OK - assuming there will always be at least headings in Row 1, try putting this code as the very first lines of your code, i.e. immediatley under the AddNewClaims macro heading:

    Please Login or Register  to view this content.
    Last edited by Trebor76; 02-26-2014 at 10:18 PM.

  15. #15
    Registered User
    Join Date
    02-26-2014
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Creating IF statement

    I presume this is what you meant, but it did the same thing and threw an error on the same line (object required error)

    Please Login or Register  to view this content.


    Please Login or Register  to view this content.

  16. #16
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Creating IF statement

    It's do with the 'CurrClaims' range playing up. Try the following where I simply reset that named range (I've also changed the way the last row if found as your method is based on Excel 2003 where you could get caught out):

    Please Login or Register  to view this content.
    Regards,

    Robert

  17. #17
    Registered User
    Join Date
    02-26-2014
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Creating IF statement

    Thanks Rob, I wish I could say it worked! I can see what you've done but unfortunately now it moves all the claims to "old claims" when it should only move the claims which are on the current claims but not in the new claims when they're imported.

    So essentially the sheet does this when the macro is run:
    - pastes all new claims into current claims
    - looks for duplicates and with those gets the data from the old current (duplicated) claims in columns from AG- AH and assigns them to the new duplicates and does the same fr column H but applies that to the respective claim in column I
    - it then clears the old duplicates
    - it also pastes any new, non duplicated claims into current claims

    I'm then left with two ranges - new duplicates and new claims down the bottom and any "old claims" up the top. The old claims are any left over claims that were not on the new claims list. The new claims are any duplicates plus any claims that were not already in current claims.

    - it then removes any blank lines from the old claims range and puts all that data into the old claims tab.

    However when there are no old claims that range does not exist and so the macro has an error (from my understanding).

    I thought if I could tell it that if the old current claims range was not there then to just sort by dwc and so on and a forth but if it was there then to execute the process as per the macro. But my if statement attempts never worked.

    I really wish I could solve this

  18. #18
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Creating IF statement

    OK last try - this will only run the problematic section if there's no error in trying to select the row(s) from the 'CurrClaims' range:

    Please Login or Register  to view this content.
    Robert
    Last edited by Trebor76; 02-27-2014 at 01:32 AM.

  19. #19
    Registered User
    Join Date
    02-26-2014
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Creating IF statement

    Were you able to run that macro? I've put your code in there and the first time it did something odd - instead of adding the dates when the data went into "old claims" it added the dates in the new claims. I don't know why this was, so I thought I'd run it again into a new sheet. I stepped into the macro and note that it appears not to be selecting the old claims sheet now.

    Sheets("Old Claims").Select
    Range("A" & Rows.Count).End(xlUp).Offset(1).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    End If
    On Error Resume Next

    I'm suspecting it has something to do with the error code??

  20. #20
    Registered User
    Join Date
    02-26-2014
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Creating IF statement

    Actually ignore what I said, it's not supposed to select old claims, so what you did was great however if there is no range to select then it should skip to this bit (because it won't need to execute any processes in the "old claims" tab as it won't be adding any old claims...

    Please Login or Register  to view this content.

  21. #21
    Registered User
    Join Date
    02-26-2014
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Creating IF statement

    Okay I have changed it slightly and now it seems to run... touch wood. Did you want to have a look and see what you think?

    Please Login or Register  to view this content.
    And yes I am not sure about this part either, I didn't design the code, I've just modified it to accomodate tweaks I've made to the sheet.
    Please Login or Register  to view this content.
    would you suggest something different?


    Thank you so much for your help, I appreciate it a lot!

  22. #22
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Creating IF statement

    Okay I have changed it slightly and now it seems to run... touch wood. Did you want to have a look and see what you think?
    You know better than I if the expectyed results are what they should be so hopefully we're on the right track. Just bear in mind that the statement On Error Resume Next doesn't fix anything - it merely stops error messages from appearing that's why I turn it back on via On Error Goto 0 which you've removed.

    would you suggest something different?
    Yes, try this which finds the last row used regardles of what column it resides in:

    Please Login or Register  to view this content.
    If you could mark the thread as solved it would be appreciated.

    Regards,

    Robert
    Last edited by Trebor76; 02-27-2014 at 06:14 PM.

  23. #23
    Registered User
    Join Date
    02-26-2014
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Creating IF statement

    fantastic thank you Robert. I guess I should use that type of code wherever it's trying to select entire rows?

  24. #24
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Creating IF statement

    I guess I should use that type of code wherever it's trying to select entire rows?
    Normally I check if the range is empty or not, but yes what I've written is another way of achieving the same result.

    I'm glad we were able to get it sorted for you

    Robert

  25. #25
    Registered User
    Join Date
    02-26-2014
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Creating IF statement

    Thank you so much, you've been such a champ! I am really glad we resolved it, I was starting to get grey hair!

+ 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] Creating an if statement
    By brianjones00 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 03-30-2013, 04:30 PM
  2. Creating an If/then statement in Excel
    By IheartIUP in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 11-12-2012, 01:40 PM
  3. Help Creating a IF Statement
    By markhuges in forum Excel General
    Replies: 7
    Last Post: 10-30-2011, 04:00 AM
  4. Creating an If statement
    By twatkins513 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-23-2010, 02:40 PM
  5. Creating a .bmp file with Put statement
    By incjourn in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-17-2009, 01:12 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