Consulting

Results 1 to 4 of 4

Thread: VBA Coding Help

  1. #1

    Red face VBA Coding Help

    Hello,

    I am still very new to VBA.

    Could somebody show me how to do the following:

    Write a procedure that shows the smallest positive number in the selected data range. If there are no positive numbers, your procedure should show the message: "There are no positive values".

    I'd like to get a pop-up box that looks like that from another problem we were working on:

    Capture1.jpg

    Thank you!

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Maybe

    Option Explicit
    
    Sub Something()
        Dim c As Range
        Dim n As Long
        
        If Not TypeOf Selection Is Range Then Exit Sub
    
        n = 0
        
        For Each c In Selection.Cells
            If c.Value > 0 Then
                If n = 0 Then
                    n = c.Value
                ElseIf n > c.Value Then
                    n = c.Value
                End If
            End If
        Next
        
        If n = 0 Then
            MsgBox "There are no positive values", vbOKOnly, "Microsoft Excel"
        Else
            MsgBox n, vbOKOnly, "Microsoft Excel"
        End If
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Thank you! That helped me get started. I made a few alterations so that it would work with non-integers as well. And I deleted some of the code related to the message box:

    Sub SmallestPositiveNumber()
    Dim c As Variant
    Dim n As Variant
        
    If Not TypeOf Selection Is Range Then Exit Sub
    
    
    n = 0
        
    For Each c In Selection.Cells
        If c.Value > 0 Then
            If n = 0 Then
                n = c.Value
            ElseIf n > c.Value Then
                n = c.Value
            End If
        End If
    Next
        
        If n = 0 Then
            MsgBox "There are no positive values"
        Else
            MsgBox n
        End If
    End Sub

    Quote Originally Posted by Paul_Hossler View Post
    Maybe

    Option Explicit
    
    Sub Something()
        Dim c As Range
        Dim n As Long
        
        If Not TypeOf Selection Is Range Then Exit Sub
    
        n = 0
        
        For Each c In Selection.Cells
            If c.Value > 0 Then
                If n = 0 Then
                    n = c.Value
                ElseIf n > c.Value Then
                    n = c.Value
                End If
            End If
        Next
        
        If n = 0 Then
            MsgBox "There are no positive values", vbOKOnly, "Microsoft Excel"
        Else
            MsgBox n, vbOKOnly, "Microsoft Excel"
        End If
    End Sub

  4. #4

    Red face

    I ultimately ended up with this code:

    Sub SmallestPositiveNumber()
        
    If Not TypeOf Selection Is Range Then Exit Sub
    
    n = 0
        
    For Each c In Selection.Cells
    
        If c.Value > 0 Then
            If n = 0 Or n > c.Value Then
                n = c.Value
            End If
            
    End If
    Next
        
        If n = 0 Then
            MsgBox ("There are no positive values")
        Else
            MsgBox (n)
        End If
        
    End Sub

    Quote Originally Posted by TheTraveler View Post
    Thank you! That helped me get started. I made a few alterations so that it would work with non-integers as well. And I deleted some of the code related to the message box:

    Sub SmallestPositiveNumber()
    Dim c As Variant
    Dim n As Variant
        
    If Not TypeOf Selection Is Range Then Exit Sub
    
    
    n = 0
        
    For Each c In Selection.Cells
        If c.Value > 0 Then
            If n = 0 Then
                n = c.Value
            ElseIf n > c.Value Then
                n = c.Value
            End If
        End If
    Next
        
        If n = 0 Then
            MsgBox "There are no positive values"
        Else
            MsgBox n
        End If
    End Sub

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •