+ Reply to Thread
Results 1 to 9 of 9

If else, If then statement needed when data doesnt exist ... help

  1. #1
    Registered User
    Join Date
    07-07-2015
    Location
    Wash DC
    MS-Off Ver
    10
    Posts
    57

    If else, If then statement needed when data doesnt exist ... help

    Situation... I have inheirited a fairly involved excel macro and am running into a problem... THis macro imports a number of files as seperate worksheets, then reorganizes the data in the worksheets and copies it to a central sheet and does some calucations... THe problem is that the macro has a section for editing each workstheet... THere is a sample below... Basically, the script runs "Batch1_GETS, Batch2_GETS, and so on till Batch8_GETS, This edits the data on th worksheet and then copies data to another worksheet after the fact (ParaSoft_Data) for additional calculations... IT woks fine, but the problem is that now the number of batches has now started varying. Now alot of the time we only get Batch 1-6, but we still on occation get 8, so I don't want to remove that section of the code in the macro... The problem is that since there is no Batch7/8 worksheet that when it gets to the Batch7_GETS section in the macro, it causes problems.

    Normally, i would a IF NOT EXIST/ GOTO statement... Basically, I would note in the code - that if something didn't exist (ie Batch7/8_GETS worksheets) it would just skip that section and go on... I know that excel has IF/ElSE statements - but I can't get it work... So I figured I would ask... I would think it shouldn't be too difficult, so if anyone if familiar, I would appreciate some help....

    Ultimately, I would prefer to loop the BatchX_GETS statements, so there wasn't a seperate piece for each workbook, but one thing at a time...

    Thansks in advance..


    Please Login or Register  to view this content.
    Moderator's note: Please take the time to review our rules. There aren't many, and they are all important. Rule #3 requires code tags. I have added them for you this time because you are a new member. --6StringJazzer
    Last edited by 6StringJazzer; 09-02-2015 at 08:07 PM.

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,837

    Re: If else, If then statement needed when data doesnt exist ... help

    Could you post a copy of your file with the complete macro included?
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Registered User
    Join Date
    07-07-2015
    Location
    Wash DC
    MS-Off Ver
    10
    Posts
    57

    Re: If else, If then statement needed when data doesnt exist ... help

    Here you go...

    The Macro goes sequentially Batch1-8_GETS... Each step is basically the same ... It deletes and reorderes each BatchX_GETS worksheet, and copies data to a centralized sheet (Parasoft Data). The Code for each step is basically the same with one exception - it copies the data to a different collumn in the centralized sheet (A3 for Batch1, F3 for Batch2, and so on)...

    The sheet works fine if I have a total of 8 batches ... THe problem is that I now doesn't always wind up with batches 1-8, but often get less usually 6 ... THis ultimately means that that a Batch7_GETS and Batch8_GETS worksheets aren't creiated ie doesn't exist - so when my script gets to that point and doesn't find those sheets it causes problems...

    I am looking for a way for teh marco to do a check of whether or not a worksheet exists, if it doesn't to skipt that setp and go on to the end, or next batch (sometimes I get a missing batch in the middle)...



    Please Login or Register  to view this content.
    Moderator's note: Please take the time to review our rules. There aren't many, and they are all important. Rule #3 requires code tags. I have added them for you this time because you are a new member. --6StringJazzer
    Last edited by 6StringJazzer; 09-02-2015 at 08:07 PM.

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: If else, If then statement needed when data doesnt exist ... help

    Try this...

    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  5. #5
    Registered User
    Join Date
    07-07-2015
    Location
    Wash DC
    MS-Off Ver
    10
    Posts
    57

    Re: If else, If then statement needed when data doesnt exist ... help

    didn't work...

    Debugger highlights

    .Columns("A:B").Sort Key1:=Range("B2"), Order1:=xlAscending, Header:=xlYes, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal

    I don't actually think it's an issue - as not columns are populated ..

    I have tried the loop but I wasn't able to get it to work...

    I do have one question concering this line

    If Evaluate("ISREF('" & strTabName & "'!A1)") Then 'Test if worksheet name exists

    what's the A1 for ...

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: If else, If then statement needed when data doesnt exist ... help

    It should be B1
    Please Login or Register  to view this content.

    Quote Originally Posted by Hondahawkrider View Post
    If Evaluate("ISREF('" & strTabName & "'!A1)") Then 'Test if worksheet name exists

    what's the A1 for ...
    The ISREF worksheet function requires cell reference to test if it is a valid reference. It could be any cell as it's purpose is to test the worksheet name.

  7. #7
    Registered User
    Join Date
    07-07-2015
    Location
    Wash DC
    MS-Off Ver
    10
    Posts
    57

    Re: If else, If then statement needed when data doesnt exist ... help

    still didn't work... still 1004 error...

    Debug hightlights the same string...

    I thru in the
    "On Error Resume Next" to see what happened....

    results are odd....

    The Batch1_GETS thru Batch4_GETS Tabs (worksheets) have deleted the C:L columns as it was supposed to do - but the data wasn't sorted prior (b2:B135) prior to the deletion - so the data isn't correct...

    However, Batch6_GETS deleted the C:L columns - sorted the data correctly and then did the delete properly... So the correct data is in that tab (it's the only tab that's right).

    THe data in all the tabs was copied correctly to the Parasoft Data worksheet (however as noted tabs BATCH_GETS1-5 are incorrect - but BATCH6_GETS6 is fine...

    dontgetit

  8. #8
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: If else, If then statement needed when data doesnt exist ... help

    I missed adding a period before Range. Sorry.

    .Columns("A:B").Sort Key1:=.Range("B1"), Order1:=xlAscending, Header:=xlYes, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal

  9. #9
    Registered User
    Join Date
    07-07-2015
    Location
    Wash DC
    MS-Off Ver
    10
    Posts
    57

    Re: If else, If then statement needed when data doesnt exist ... help

    No worries... It worked great... thanks 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. Code to update or insert if doesnt exist
    By bosco2 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 01-26-2015, 03:29 AM
  2. How to add a value to the last blank cell of a column if it doesnt exist there already.
    By yoursamrit2000 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-20-2015, 04:42 AM
  3. [SOLVED] Find Value in all worksheets, if doesnt exist then...
    By bg_enigma1 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-06-2012, 12:30 PM
  4. If named range doesnt exist next k
    By jordan2322 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-13-2012, 12:17 AM
  5. macro to check if a value exist in sheet and add it if it doesnt
    By RetroGun in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-01-2011, 02:35 AM
  6. Delete row if value(array) doesnt exist in column
    By wobaby in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-24-2009, 02:34 AM
  7. find number that doesnt exist among others.
    By TheBean in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-26-2006, 09:28 AM

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