Results 1 to 20 of 45

Thread: Python dictionary in VBA

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    VBAX Expert
    Joined
    Aug 2004
    Posts
    818
    Location
    see if this works.... I don't know python so i use ChatGPT to convert your program:
    you will also need to add the rest of your measurements to the Dictionary as well
    you need to add a reference to Microsoft Scripting Runtime as well
    Sub RebarResults()
        Dim crosssection As Single
        crosssection = InputBox("Rebar cross-section value(cm2):")
        
        Dim rebars As Scripting.Dictionary
        Set rebars = New Scripting.Dictionary
        
        ' 1 rebars
        rebars.Add 0.28, "1x6"
        rebars.Add 0.5, "1x8"
        rebars.Add 0.79, "1x10"
        rebars.Add 1.13, "1x12"
        rebars.Add 1.54, "1x14"
        rebars.Add 2.01, "1x16"
        rebars.Add 2.54, "1x18"
        rebars.Add 3.14, "1x20"
        rebars.Add 3.8, "1x22"
        rebars.Add 4.91, "1x25"
        rebars.Add 6.16, "1x28"
        rebars.Add 8.04, "1x32"
        rebars.Add 10.18, "1x36"
        rebars.Add 12.57, "1x40"
    
        ' 2 rebars
        rebars.Add 0.57, "2x6"
        rebars.Add 1.01, "2x8"
        rebars.Add 1.57, "2x10"
        rebars.Add 2.26, "2x12"
        rebars.Add 3.08, "2x14"
        rebars.Add 4.02, "2x16"
        rebars.Add 5.09, "2x18"
        rebars.Add 6.28, "2x20"
        rebars.Add 7.60, "2x22"
        rebars.Add 9.82, "2x25"
        rebars.Add 12.32, "2x28"
        rebars.Add 16.08, "2x32"
        rebars.Add 20.36, "2x36"
        rebars.Add 25.13, "2x40"
        
        ' 3 rebars
        rebars.Add 0.85, "3x6"
        rebars.Add 1.51, "3x8"
        rebars.Add 2.36, "3x10"
        rebars.Add 3.39, "3x12"
        rebars.Add 4.62, "3x14"
        rebars.Add 6.03, "3x16"
        rebars.Add 7.63, "3x18"
        rebars.Add 9.42, "3x20"
        rebars.Add 11.40, "3x22"
        rebars.Add 14.73, "3x25"
        rebars.Add 18.47, "3x28"
        rebars.Add 24.13, "3x32"
        rebars.Add 30.54, "3x36"
        rebars.Add 37.70, "3x40"     
    
        Dim result As Scripting.Dictionary
        Set result = New Scripting.Dictionary
        
        ' Find the smallest closest rebar size
        For Each smalest In rebars
            If smalest < crosssection And smalest > Fix(crosssection) Then
                result.Add smalest, rebars(smalest)
            End If
        Next smalest
        
        ' Find the closest rebar size
        For Each closestRebar In rebars
            If crosssection <= closestRebar And closestRebar < Round(crosssection + 2, 0) Then
                result.Add closestRebar, rebars(closestRebar)
            End If
        Next closestRebar
        
        ' Print the results
        If result.Count > 0 Then
            MsgBox "Rebar results:" & vbCrLf & Join(result.Items, vbCrLf)
        Else
            MsgBox "Rebars not available. Check input"
        End If
    End Sub
    Last edited by JKwan; 03-07-2023 at 11:29 AM.

Posting Permissions

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