+ Reply to Thread
Results 1 to 10 of 10

Replace multiple "If Then" with "For" loop

  1. #1
    Registered User
    Join Date
    06-17-2014
    Location
    United States
    MS-Off Ver
    2007
    Posts
    9

    Replace multiple "If Then" with "For" loop

    Can anyone please tell me how to use "for" loop for this?

    Please Login or Register  to view this content.
    Last edited by checkltout; 06-19-2014 at 08:13 AM.

  2. #2
    Registered User
    Join Date
    06-17-2014
    Location
    United States
    MS-Off Ver
    2007
    Posts
    9

    re: Replace multiple "If Then" with "For" loop

    This is what I did:

    Please Login or Register  to view this content.
    It only shows error message for prem2, but not for prem3 and prem4.
    Last edited by checkltout; 06-19-2014 at 08:13 AM.

  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: Replace multiple "If Then" with "For" loop

    Hi, checkltout,

    Your posts do not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here



    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)

    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
    06-17-2014
    Location
    United States
    MS-Off Ver
    2007
    Posts
    9

    re: Replace multiple "If Then" with "For" loop

    Sorry bout that. Edited my post already.

  5. #5
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    640

    re: Replace multiple "If Then" with "For" loop

    I'm not sure your logic works??

    If a premium is 50,000 then it fails the <> 50,000 but meets all the other conditions (<> 20,000, <> 25,000 etc).

    You may want to rethink the logic (maybe if premium = 20,000 or 25,000 and so on then "do something positive" else msgbox "Error: Enter a valid premium")???

    You could then loop it with a prem & "i"

    i = 1-4

  6. #6
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    re: Replace multiple "If Then" with "For" loop

    Hi,

    Why have you chosen to use VBA when it at least seems possible that a standard Data validation could probably achieve the same result?
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  7. #7
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    640

    re: Replace multiple "If Then" with "For" loop

    I would try something along the lines of this (haven't a clue if it will work but this is where I would start).

    Dim i as long
    i = 1-4

    If IsMissing (Prem & "i") Then
    Prem & "i" = ""
    Exit Sub

    If Prem & "i" = 20000 or 25000 or 50000 or 100000 or 150000 or 200000 or 250000 Then
    Exit Sub

    Else

    MsgBox "Error: Enter a valid premium"

    Next i

  8. #8
    Registered User
    Join Date
    06-17-2014
    Location
    United States
    MS-Off Ver
    2007
    Posts
    9

    re: Replace multiple "If Then" with "For" loop

    Quote Originally Posted by Sc0tt1e View Post
    If a premium is 50,000 then it fails the <> 50,000 but meets all the other conditions (<> 20,000, <> 25,000 etc).
    Yeah since I'm using AND function, the premium has to meet all conditions in order for the error message to appear. If the premium is 50,000, then it fails one of the conditions. What you're saying is the OR function I think..?

  9. #9
    Registered User
    Join Date
    06-17-2014
    Location
    United States
    MS-Off Ver
    2007
    Posts
    9

    re: Replace multiple "If Then" with "For" loop

    Quote Originally Posted by Richard Buttrey View Post
    Hi,

    Why have you chosen to use VBA when it at least seems possible that a standard Data validation could probably achieve the same result?
    Well I need to create a function in Excel to fix the premium amounts in a string. So if I type in the function a premium amount, it will change the premium amount in the string to what I want. Also I have never used data validation, so maybe data validation could do that as well.

  10. #10
    Registered User
    Join Date
    06-17-2014
    Location
    United States
    MS-Off Ver
    2007
    Posts
    9

    re: Replace multiple "If Then" with "For" loop

    Quote Originally Posted by Sc0tt1e View Post
    If IsMissing (Prem & "i") Then
    Prem & "i" = ""
    Exit Sub
    I tried this but it says Expected: expression when I typed Prem & "i" = "". Sorry I'm a beginner in VBA so please bear with me.

+ 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. using for loop to replace if-then-else
    By bored in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-31-2013, 12:23 AM
  2. Find & replace loop over multiple columns
    By bigcol77 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-01-2012, 02:24 PM
  3. replace multiple ifs in a do.. loop request
    By nicer2011 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-12-2011, 06:14 PM
  4. Best Loop to replace multiple strings
    By ker9 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-22-2011, 04:38 PM
  5. Replace using Do loop
    By Dave B in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-29-2005, 03:40 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