Consulting

Results 1 to 2 of 2

Thread: Decipher Macro

  1. #1
    VBAX Newbie
    Joined
    May 2020
    Posts
    1
    Location

    Decipher Macro

    Hi there, I am currently stuck with some calculation project. And I have gotten an excel sheet which can helps but I never decode a Macro before. Is there anyone that can help me decode it and let me know what is the formula for the calculation?


    Sub Button2_Click()
    Dim speedstr As String
    Dim DesSpeed As Single
    
    
    DesSpeed = Sheet2.Cells(47, 5)
    
    
    speedstr = InputBox("Please enter the vessel speeds in m/s separated by commas")
    
    
    If speedstr = "" Then MsgBox "Please enter at least one value": Exit Sub
    
    
    Dim arr
    
    
    arr = Split(speedstr, ",")
    
    
    Dim i As Integer
    
    
    Sheet12.range("B4:C34").ClearContents
    
    
    
    
    Dim count As Integer
    
    
    count = 0
    
    
    If UBound(arr) > 20 Then MsgBox "Maximum 20 values allowed": Exit Sub
    
    
    For i = 0 To UBound(arr)
    
    
        'MsgBox "Speed No. " & i + 1 & " = " & arr(i)
        Dim currspeed As Single
        If arr(i) = "" Then Exit Sub
        
        If i > 0 Then
        If Val(arr(i)) < Val(arr(i - 1)) Then
        
        MsgBox "Please input the values in ascending order"
        
        Sheets("Speeds").Activate
        
        Exit Sub
        End If
        
        
        End If
        
        
        currspeed = arr(i)
        If Val(arr(i)) = 0 Then MsgBox "Please input a non-zero value for speed": Exit Sub
        
        Dim range As Excel.range
        Dim resistance As Double
        'range.Cells(2, i).Value = arr(i)
        
    
    
    
    
        If i > 0 Then
        If DesSpeed < Val(arr(i)) And DesSpeed > Val(arr(i - 1)) Then
        
                Sheets("Inputs - Vessel").Cells(47, 5).Value = DesSpeed
                Sheets("Graph").Cells(4 + count, 2).Value = DesSpeed
                resistance = Sheets("Final Forces").Cells(24, 4).Value
                Sheets("Graph").Cells(4 + count, 3).Value = Format(resistance, "#0.00")
                Sheet12.range(Sheet12.Cells(4 + count, 2), Sheet12.Cells(4 + count, 4)).Interior.Color = vbBlue
                count = count + 1
        End If
        End If
        
        Sheets("Inputs - Vessel").Cells(47, 5).Value = arr(i)
        
        resistance = Sheets("Final Forces").Cells(24, 4).Value
        
        Sheets("Graph").Cells(4 + count, 2).Value = arr(i)
        Sheets("Graph").Cells(4 + count, 3).Value = Format(resistance, "#0.00")
    
    
        Sheets("Graph").Activate
        
        count = count + 1
        
    Next i
    
    
    
    
    '''''''''Displaying the resistance at design speed
    Sheets("Inputs - Vessel").Cells(47, 5).Value = DesSpeed
    
    
    End Sub
    Last edited by Paul_Hossler; 05-20-2020 at 07:21 AM. Reason: Added CODE tags

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,727
    Location
    There's a number of sheets referenced in the macro that I have no idea

    It's easier to trace a macro if it's indented and structured a little differently

    See if you can trace this since you have a better idea of what it's supposed to do

    Option Explicit
    
    
    Sub Button2_Click()
    
    
        Dim speedstr As String
        Dim DesSpeed As Single
        Dim arr As Variant
        Dim i As Integer
        Dim count As Integer
        Dim currspeed As Single
        Dim range As range
        Dim resistance As Double
    
    
        DesSpeed = Sheet2.Cells(47, 5)
        
        speedstr = InputBox("Please enter the vessel speeds in m/s separated by commas")
    
    
        If speedstr = "" Then
            MsgBox "Please enter at least one value"
            Exit Sub
        End If
    
    
        arr = Split(speedstr, ",")
        Sheet12.range("B4:C34").ClearContents
        
        count = 0
    
    
        If UBound(arr) > 20 Then
            MsgBox "Maximum 20 values allowed"
            Exit Sub
        End If
    
    
        For i = 0 To UBound(arr)
        
            'MsgBox "Speed No. " & i + 1 & " = " & arr(i)
            If arr(i) = "" Then Exit Sub
        
            If i > 0 Then
                If Val(arr(i)) < Val(arr(i - 1)) Then
                    MsgBox "Please input the values in ascending order"
                    Sheets("Speeds").Activate
                    Exit Sub
                End If
            End If
        
            currspeed = arr(i)
            
            If Val(arr(i)) = 0 Then
                MsgBox "Please input a non-zero value for speed"
                Exit Sub
            End If
            
            'range.Cells(2, i).Value = arr(i)
            
            If i > 0 Then
                
                If DesSpeed < Val(arr(i)) And DesSpeed > Val(arr(i - 1)) Then
                    Sheets("Inputs - Vessel").Cells(47, 5).Value = DesSpeed
                    Sheets("Graph").Cells(4 + count, 2).Value = DesSpeed
                    resistance = Sheets("Final Forces").Cells(24, 4).Value
                    Sheets("Graph").Cells(4 + count, 3).Value = Format(resistance, "#0.00")
                    Sheet12.range(Sheet12.Cells(4 + count, 2), Sheet12.Cells(4 + count, 4)).Interior.Color = vbBlue
                    count = count + 1
                End If
            End If
        
            Sheets("Inputs - Vessel").Cells(47, 5).Value = arr(i)
            
            resistance = Sheets("Final Forces").Cells(24, 4).Value
            
            Sheets("Graph").Cells(4 + count, 2).Value = arr(i)
            Sheets("Graph").Cells(4 + count, 3).Value = Format(resistance, "#0.00")
            Sheets("Graph").Activate
            
            count = count + 1
        Next i
    
    
    
    
        '''''''''Displaying the resistance at design speed
        Sheets("Inputs - Vessel").Cells(47, 5).Value = DesSpeed
    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

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
  •