+ Reply to Thread
Results 1 to 9 of 9

If condition raises error if worksheet does not exist

  1. #1
    Registered User
    Join Date
    04-06-2022
    Location
    Dominican Republic
    MS-Off Ver
    Microsoft® Excel® 2019 MSO 2203 Version
    Posts
    15

    Question If condition raises error if worksheet does not exist

    Hi guys, me again.

    This time I have a very simple question and it has to do with an IF statement. I come form web coding, so when it comes to IF statements in web coding the IF will validate true or false depending on what you are trying to confirm, pretty simple. I thought it was like this in VBA but apparently it's not exactly like this, seems like if the variable you are trying to validating does not even exists or can't be pulled up in any way, for some reason the code will stop at that point ... It would not go to the next "Else" as it would in web coding.

    I am so new to VBA, so please be patient with me.

    Maybe I'm missing something, or maybe I'm not using the IF statement as I should. Someone please bring some light to the matter.

    I'll leave a snippet of my code here so you guys can see what I'm talking about. The code is working fine and it is doing exactly what I expect it to do, but the thing is that I really want to cover the situation in which someone would attempt to open a file that does not matches the criteria and, if that happens, I want to show an error message for the user to see it.

    With this code, I'm just looking into a workbook for a specific sheet and validating the content inside a cell on that sheet. If that cell contains what is expected then I proceed executing my code, otherwise you should get a MsgBox. The problem comes when the sheet that I'm looking for does not exists, if this the case, the code stops with an internal error message and will not go to the end of the IF.

    Here is the code:

    Please Login or Register  to view this content.
    I think that instead of looking for an specific sheet name (Which in my case could only be either "3" or "Validador") I could just do a loop trough all the sheets of the workbook and get the name of the first one that meets the criteria (Which in my case is to have the cell E3 = "Validador"). But I have no clue on how to do that, I was never good enough with loops. If someone could help me do that it would be even better than what I'm trying to do with the IF statements.
    Last edited by CjTonos; 04-22-2022 at 01:15 PM. Reason: Solved

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: I need help with an IF statement

    Quote Originally Posted by CjTonos View Post
    when it comes to IF statements in web coding the IF will validate true or false depending on what you are trying to confirm, pretty simple. I thought it was like this in VBA but apparently it's not exactly like this, seems like if the variable you are trying to validating does not even exists or can't be pulled up in any way, for some reason the code will stop at that point
    I know of no language that if you make a reference to an object that doesn't exist, it will happily continue. There are lots of languages that can be used for "wed coding." Can you tell me what language you use that does this?

    To loop through all sheets looking for a specific value you can change the code as shown. But you have two different results, depending on where you find the value. You will have to be more specific about how you want this to work.

    Also you have a dangling If, which I have eliminated.

    Also, you have undeclared variables. I strongly recommend to everyone that they use Option Explicit and declare variables. Doing so prevents a lot of bugs and runtime errors.

    Please Login or Register  to view this content.
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    04-06-2022
    Location
    Dominican Republic
    MS-Off Ver
    Microsoft® Excel® 2019 MSO 2203 Version
    Posts
    15

    Re: I need help with an IF statement

    Quote Originally Posted by 6StringJazzer View Post
    I know of no language that if you make a reference to an object that doesn't exist, it will happily continue. There are lots of languages that can be used for "wed coding." Can you tell me what language you use that does this?

    To loop through all sheets looking for a specific value you can change the code as shown. But you have two different results, depending on where you find the value. You will have to be more specific about how you want this to work.

    Also you have a dangling If, which I have eliminated.

    Also, you have undeclared variables. I strongly recommend to everyone that they use Option Explicit and declare variables[/URL]. Doing so prevents a lot of bugs and runtime errors.
    Hey man, thank you for your response. About the web coding, I think I explained my self in a wrong way, I'm sorry for that, not too good with English but yeah, you're right, that's exactly what would happen in web coding. I'll have to do a research about what you suggested to do with Option Explicit and declaring variables, don't know what you mean by that but I will find it out.

    I've analyzed your code and I think it is just perfect for what I'm looking for but... I tried to run it and it is giving me this error on line 51 --> "object doesn't support this property or method"

    That's when te loop starts.

    Thak u again.

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: I need help with an IF statement

    Quote Originally Posted by CjTonos View Post
    I'll have to do a research about what you suggested to do with Option Explicit and declaring variables
    Click on the link I provided and it will give more detail.

    I'm sorry, I made an error in that line of code. I could not test it without your file. Make the following update:
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    04-06-2022
    Location
    Dominican Republic
    MS-Off Ver
    Microsoft® Excel® 2019 MSO 2203 Version
    Posts
    15

    Re: I need help with an IF statement

    Quote Originally Posted by 6StringJazzer View Post
    Click on the link I provided and it will give more detail.

    I'm sorry, I made an error in that line of code. I could not test it without your file. Make the following update:
    Please Login or Register  to view this content.
    Yeah, tomorrow I will definitely take some time to check that link you sent... thank you for that and for the fix, don't be sorry. You're a hero man xD. I'll test it at work tomorrow and let you know.

  6. #6
    Registered User
    Join Date
    04-06-2022
    Location
    Dominican Republic
    MS-Off Ver
    Microsoft® Excel® 2019 MSO 2203 Version
    Posts
    15

    Re: I need help with an IF statement

    Quote Originally Posted by 6StringJazzer View Post
    Click on the link I provided and it will give more detail.

    I'm sorry, I made an error in that line of code. I could not test it without your file. Make the following update:
    Please Login or Register  to view this content.

    Bro, I just tested the code and something weird is happening. The loop is going thru all the sheets only in certain cases, in some other cases it will not check all the sheets in the workbook. Like, I made a brand new workbook, I opened 4 new sheets and I've run the loop with a MsgBox indicating the name of the sheet it is at. Happens that it will only show me 1 MsgBox with the name of sheet number 1. For some reason it is not going thru all the sheets.

    However I tried the code in another workbook I previously had created and the loop is giving me all the sheets names in that workbook. It's kinda crazy, don't know if maybe now there is some sort of Corp. Settings in my excel that are preventing the code to go thru all the sheets. I have to test it in my personal computer. Right now I'm trying it at work.

    Any thoughts?

  7. #7
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: I need help with an IF statement

    Any chance you can attach your file so I can run the code?

  8. #8
    Registered User
    Join Date
    04-06-2022
    Location
    Dominican Republic
    MS-Off Ver
    Microsoft® Excel® 2019 MSO 2203 Version
    Posts
    15

    Re: I need help with an IF statement

    Quote Originally Posted by 6StringJazzer View Post
    Any chance you can attach your file so I can run the code?
    Nevermind bro, it's working now. I was preparing a clean file and simplified the code to show you the error and then I realized that the code was working fine on the clean file. I then saw where the problem was, and Elseif was being triggered when it wasn't supposed to be triggered. I guess I'll have to learn more about how IF statements works in VBA. Bottom line, it worked perfectly, thank you once again.

  9. #9
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: If condition raises error if worksheet does not exist

    They work the same they do in every other language I've ever used. Hopefully this will help.

    One thing that is not the same in all languages is that VBA has no short-circuit boolean evaluation. Every boolean expression is completely evaluated to determine the boolean value.

    Anyway, here is how the flow works:
    Please Login or Register  to view this content.

+ 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. just new: trying to get/set property of class raises compile error expected: identifier or
    By himyburneraccount in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-15-2018, 04:41 PM
  2. How to output and list instances where a condition does not exist
    By Frodobongins in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-14-2018, 02:46 AM
  3. Import multiple CSV, also show error message when worksheet name already exist
    By Frostyminty in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-18-2014, 07:44 PM
  4. [SOLVED] how to run macro or msgbox when Worksheet does not exist/exist
    By herukuncahyono in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-14-2013, 10:54 PM
  5. Conditional Formatting, if condition does NOT exist
    By okinawarules in forum Excel General
    Replies: 4
    Last Post: 07-02-2012, 08:49 PM
  6. Highlighting A row of Cells if a condition exist
    By gunerstahl in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-29-2011, 09:32 PM
  7. Replies: 4
    Last Post: 06-18-2006, 01: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