+ Reply to Thread
Results 1 to 4 of 4

Simple: State an If Block more concisely

Hybrid View

  1. #1
    Registered User
    Join Date
    02-10-2014
    Location
    United States of America
    MS-Off Ver
    Excel 2013
    Posts
    14

    Simple: State an If Block more concisely

    Dear Excel Forum,

    First, I would like to note that technically this is two questions within a single thread, but it is the same problem just two possible solutions. I hope that is acceptable.


    I have some statements here and could not figure out a neat way to combine them. What would be an effective way of conveying the code below?

        If deleteStr = "" And timeB < 59 Then
            MsgBox "The data tables have been cleaned." & vbCrLf & vbCrLf & _
                "Total Time Taken: " & (timeB Mod 60) & _
                " second(s)", vbInformation, "Log Summary"
        End If
    
        If deleteStr = "" And timeB >= 60 Then
            MsgBox "The data tables have been cleaned." & vbCrLf & vbCrLf & _
                "Total Time Taken: " & Round((timeB / 60), 0) & " minute(s) and " & (timeB Mod 60) & _
                " second(s)", vbInformation, "Log Summary"
        End If
    
        If deleteStr <> "" And timeB < 59 Then
            MsgBox "The data tables have been cleaned." & vbCrLf & vbCrLf & _
                "Respondent answers matching '" & deleteStr & "' have been removed." & vbCrLf & vbCrLf & _
                "Total Time Taken: " & (timeB Mod 60) & _
                " second(s)", vbInformation, "Log Summary"
        End If
    
        If deleteStr <> "" And timeB >= 60 Then
            MsgBox "The data tables have been cleaned." & vbCrLf & vbCrLf & _
                "Respondent answers matching '" & deleteStr & "' have been removed." & vbCrLf & vbCrLf & _
                "Total Time Taken: " & Round((timeB / 60), 0) & " minute(s) and " & (timeB Mod 60) & _
                " second(s)", vbInformation, "Log Summary"
        End If

    An alternative route would of course be: How do I fix the syntax for my MsgBox Timer so that it displays run time properly when run time is less than 60 seconds? What happens currently is for times between 30 and 59, it displays "1 minute(s) and x second(s)" instead of "0 minute(s) and x second(s)".

    "Total Time Taken: " & Round((timeB / 60), 0) & " minute(s) and " & (timeB Mod 60) & " second(s)"

    Best,
    Doug

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Simple: State an If Block more concisely

    Try:
    "Total Time Taken: " & format(timeB,"m "" Minutes and "" s "" Seconds""")
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Simple: State an If Block more concisely

    Hello Doug Nguyen,

        Dim Msg         As String
        Dim TimeTaken   As String
        
            Msg = ""
        
            If timeB < 59 Then TimeTaken = (timeB Mod 60) & " second(s)"
            If timeB >= 60 Then TimeTaken = Round((timeB / 60), 0) & " minute(s) and " & (timeB Mod 60) & " second(s)"
    
            If deleteStr = "" Then
                Msg = "The data tables have been cleaned." & vbCrLf & vbCrLf & "Total Time Taken: "
            Else
                Msg = "The data tables have been cleaned." & vbCrLf & vbCrLf & _
                      "Respondent answers matching '" & deleteStr & "' have been removed." & vbCrLf & vbCrLf & _
                      "Total Time Taken: "
            End If
        
            If Msg <> "" Then MsgBox Msg , vbInformation, "Log Summary"
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Registered User
    Join Date
    02-10-2014
    Location
    United States of America
    MS-Off Ver
    Excel 2013
    Posts
    14

    Re: Simple: State an If Block more concisely

    Dear Leith Ross,

    That is wonderful. I really liked how you approached the logic.


    Dear OllyXLS,

    I just tried your format option and it also worked great.


    Thank you both for responding so quickly. Thread solved and +rep.


    EDIT 1: Spoke too soon. I tried the format option again and I am still getting the rounding problem. (Leith Ross' logic is of course, unaffected and therefore still sound)

    EDIT 2: For future reference, I amended Leith Ross' code and went with this.

    Dim timeTaken As String
    
        If timeB < 59 Then
                timeTaken = (timeB Mod 60) & " second(s)"
            Else
                timeTaken = Round((timeB / 60), 0) & " minute(s) and " & (timeB Mod 60) & " second(s)"
        End If
    
        If deleteStr = "" Then
                MsgBox "The data tables have been cleaned." & vbCrLf & vbCrLf & _
                    "Total Time Taken: " & timeTaken, vbInformation, "Log Summary"
            Else
                MsgBox "The data tables have been cleaned." & vbCrLf & vbCrLf & _
                    "Respondent answers matching '" & deleteStr & "' have been removed." & vbCrLf & vbCrLf & _
                    "Total Time Taken: " & timeTaken, vbInformation, "Log Summary"
        End If
    Last edited by Doug Nguyen; 02-21-2014 at 03:36 PM.

+ 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. Replies: 5
    Last Post: 10-09-2012, 10:44 AM
  2. [SOLVED] Can VB Save hidden/grouped columns state, unhide all, then restore columns to saved state
    By JTwrk in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-27-2012, 02:54 PM
  3. Replies: 2
    Last Post: 06-22-2010, 07:55 AM
  4. Simple IF Satement Road Block Please HELP
    By needtolearn in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-08-2006, 04:20 AM
  5. Simple if statement road block
    By needtolearn in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-08-2006, 02:11 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