+ Reply to Thread
Results 1 to 8 of 8

IF-THEN-ELSE not working properly

Hybrid View

danex IF-THEN-ELSE not working... 11-02-2013, 10:21 AM
Norie Re: IF-THEN-ELSE not working... 11-02-2013, 10:26 AM
HaHoBe Re: IF-THEN-ELSE not working... 11-02-2013, 10:28 AM
danex Re: IF-THEN-ELSE not working... 11-02-2013, 10:55 AM
stnkynts Re: IF-THEN-ELSE not working... 11-02-2013, 10:28 AM
danex Re: IF-THEN-ELSE not working... 11-02-2013, 11:05 AM
HaHoBe Re: IF-THEN-ELSE not working... 11-02-2013, 11:15 AM
Norie Re: IF-THEN-ELSE not working... 11-02-2013, 10:59 AM
  1. #1
    Registered User
    Join Date
    11-02-2013
    Location
    Cebu, Philippines
    MS-Off Ver
    Excel 2007
    Posts
    21

    Lightbulb IF-THEN-ELSE not working properly

    Hi, this is my first time posting here. I've been searching for a thread that might be helpful for my current issue but could not find one.
    these are the lines im having issues with:

    Dim FNTotQty As String
              FNTotQty = Workbooks(wb).Worksheets(ws).Range("B43")
    
       If UCase(FNTotQty) = "QTY" Then
            For TotReb = 2 To z + 1
                 Cells(TotReb, 26) = Cells(TotReb, 5) * Cells(TotReb, 6).Value
             Next TotReb
       End If
    My goal is if all-caps variable FNTotQty is QTY then it should run the code above but it it is not the case. It does the opposite. If the actual value of FNTotQty <> QTY, the code will run. This is really weird. Help please.

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

    Re: IF-THEN-ELSE not working properly

    Have you stepped through the code with F8 to see what's happening?

    Also, try putting a breakpoint on these 2 lines with F9,
    For TotReb = 2 To z + 1
        Cells(TotReb, 26) = Cells(TotReb, 5) * Cells(TotReb, 6).Value
    then run the code and see what happens.
    If posting code please use code tags, see here.

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

    Re: IF-THEN-ELSE not working properly

    Hi, danex,

    to me sounds like maybe additional trailing blanks behind the value that you compare in your workbook:
    Dim FNTotQty As String
    FNTotQty = Workbooks(wb).Worksheets(ws).Range("B43")
    
    If UCase(Trim(FNTotQty)) = "QTY" Then
      For TotReb = 2 To Z + 1
        Cells(TotReb, 26) = Cells(TotReb, 5) * Cells(TotReb, 6).Value
      Next TotReb
    End If
    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

  4. #4
    Registered User
    Join Date
    11-02-2013
    Location
    Cebu, Philippines
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: IF-THEN-ELSE not working properly

    Quote Originally Posted by HaHoBe View Post
    Hi, danex,

    to me sounds like maybe additional trailing blanks behind the value that you compare in your workbook:
    Dim FNTotQty As String
    FNTotQty = Workbooks(wb).Worksheets(ws).Range("B43")
    
    If UCase(Trim(FNTotQty)) = "QTY" Then
      For TotReb = 2 To Z + 1
        Cells(TotReb, 26) = Cells(TotReb, 5) * Cells(TotReb, 6).Value
      Next TotReb
    End If
    Ciao,
    Holger
    hi holger, i double-checked it. no trailing blanks.

  5. #5
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: IF-THEN-ELSE not working properly

    That is because by saying UCase(FNTotQty) you are changing any value in Range B43 to be uppercase. Example: if the value in B43 is qty then UCase(FNTotQty) will make it QTY. It should already be case specific so just remove the UCase statement. Example

    If FNTotQty = "QTY" Then
    Last edited by stnkynts; 11-02-2013 at 10:32 AM.

  6. #6
    Registered User
    Join Date
    11-02-2013
    Location
    Cebu, Philippines
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: IF-THEN-ELSE not working properly

    hi all.

    thanks for the response. I was probably just pretty overwhelmed with all the programming languages i wanted to learn. I realized I was pointing to the wrong worksheet. the ws in Workbooks(wb).Worksheets(ws).Range("B43") should not have been ws.

    By the way, how do I close this thread?

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

    Re: IF-THEN-ELSE not working properly

    Hi, danex,

    mark the thread Solved via Thread Tools next to the opening Post for this thread. You can´t close any thread unless you are Mod/Admin in this Forum.

    Ciao,
    Holger

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

    Re: IF-THEN-ELSE not working properly

    Which worksheet is Cells meant to reference here?
    Cells(TotReb, 26) = Cells(TotReb, 5) * Cells(TotReb, 6).Value

+ 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] IF formula not working properly
    By argentraven in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 06-04-2013, 01:56 PM
  2. VBA not working properly
    By stevemills04 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-26-2013, 02:56 PM
  3. UDF not working properly
    By demuro1 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-09-2008, 04:36 PM
  4. Why is my tab key not working properly any more in Excel?
    By Steelman46 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 02-23-2006, 12:25 PM
  5. [SOLVED] CNTRL + END is not working properly
    By Dave Peterson in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-06-2005, 07:05 AM

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