+ Reply to Thread
Results 1 to 14 of 14

If - Then - Else statement being ignored when running code

  1. #1
    Registered User
    Join Date
    09-25-2011
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    45

    If - Then - Else statement being ignored when running code

    Hi,

    I have the following code:

    Please Login or Register  to view this content.
    I'm trying to define and execute two cases : if a cell in column A contains "FTT" then extract a string of text using mid in column O, else if no "FTT" is found then extract the string starting with the second With range after Else.

    The problem is the code is ignoring the If statement and going straight to the second With .Range piece of code (the "=mid(a1,89,19)") and using that for all the rows in the sheet.

    What am I doing wrong?

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: If - Then - Else statement being ignored when running code

    Hi, Seraph,

    what is rnum and where is it assigned?

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Registered User
    Join Date
    09-25-2011
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    45

    Re: If - Then - Else statement being ignored when running code

    rnum = 1, the first row of the worksheet, I have it defined as Long:

    Please Login or Register  to view this content.

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: If - Then - Else statement being ignored when running code

    Hi, Seraph84,

    please check if Range A1 in Sheets MySheet only holds FTT or maybe an additional trailing blank and that itīs all capitals as this check is case sensitive.

    Ciao,
    Hiolger

  5. #5
    Registered User
    Join Date
    09-25-2011
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    45

    Re: If - Then - Else statement being ignored when running code

    Hi Hiolger

    FFT is all caps, however it is part of a string with spaces and dashes eg. Filename - FTT - text

    Some cells will have FTT in the string while others will not.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: If - Then - Else statement being ignored when running code

    Try this.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  7. #7
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: If - Then - Else statement being ignored when running code

    Hi, Seraph84,

    either use the textfunction Left(theCell, 3) to check the first three characters to match or
    Please Login or Register  to view this content.
    if FTT may be located anywhere else in the cell.

    Ciao,
    Holger

  8. #8
    Registered User
    Join Date
    09-25-2011
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    45

    Re: If - Then - Else statement being ignored when running code

    Thanks for the suggestions, but I just cant get it to work. it still skipping the IF statement and going straight to the second With.

    Maybe I'm explaining it wrong or my code is not right.

    I've attached a sample of the file with the end result I'm looking for in Column B using normal excel formulas, for this specific section of the macro. The code should check Column A and if it contains FTT, extract the text starting as per the MID formula, and if not found extract using the second MID formulas as in Column B.

    I've included the code in Macro1 for this part of the sub, the rest of it runs fine. I cant seem to remove this workbook, please ignore this one and see the one in my post further down below.

    Thanks
    Attached Files Attached Files
    Last edited by Seraph84; 02-23-2015 at 03:45 AM. Reason: Workbook amended in post below

  9. #9
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: If - Then - Else statement being ignored when running code

    Your code is looking for FTT but the cells contain FFT
    Remember what the dormouse said
    Feed your head

  10. #10
    Registered User
    Join Date
    09-25-2011
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    45

    Re: If - Then - Else statement being ignored when running code

    Quote Originally Posted by romperstomper View Post
    Your code is looking for FTT but the cells contain FFT
    Sorry that's a typo in the attached workbook. My code in my macro is consistent though (FTT), and I still can't get it to work.

  11. #11
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: If - Then - Else statement being ignored when running code

    Then please post a workbook which:
    1. Is actually representative of your data; and
    2. Contains the code you are using.

  12. #12
    Registered User
    Join Date
    09-25-2011
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    45

    Re: If - Then - Else statement being ignored when running code

    Amended workbook with code attached.
    Attached Files Attached Files
    Last edited by Seraph84; 02-23-2015 at 03:47 AM.

  13. #13
    Valued Forum Contributor
    Join Date
    08-22-2011
    Location
    Auckland
    MS-Off Ver
    Excel 2019
    Posts
    716

    Re: If - Then - Else statement being ignored when running code

    Hi
    You can change
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    It looks like you're testing A1 to see if it's got FTT in it somewhere & entering ts.xls in C26

    Cheers

  14. #14
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: If - Then - Else statement being ignored when running code

    Hi, Seraph84,

    Norie suggested top use asteriks around FTT to search while you compare the string to only FTT.

    Please Login or Register  to view this content.
    Ciao,
    Holger

+ 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] How to prevent worksheet code from running while Module Macro is running:
    By Chad Bateman in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-11-2015, 09:26 AM
  2. [SOLVED] Excel VB code. Message pops up while code running asking question. Code must not wait.
    By Heinrich Venter in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-28-2014, 06:10 AM
  3. If Statement to stop Dim code from running
    By rlh in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-30-2014, 09:30 AM
  4. If statement to test value in cell before running code
    By Motox in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-23-2012, 04:49 AM
  5. Code the saves filter info to reapply after running other code
    By Wedge120 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-25-2011, 05:49 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