VBA Case Error

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,570
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Consider this code.

Rich (BB code):
Sub Fix_Properties()
Dim fix_name As String
Dim len_fn As Long
Dim db_fix_type As Double
Dim txt_fix_type As String
Dim fix_type As String
Dim response
Dim NavUpper As Integer, NavLower As Integer
Dim txt_freq As String
Dim freq As Integer
Dim freqlen As Double
Dim freqval As Boolean

fix_name = ""
len_fn = 0
NavLower = 108
NavUpper = 117.975
CHspac = 25 'khz
freqlen = 0
freqval = True

Do
    fix_name = InputBox("Enter the fix name: ", "FIX NAME")
    len_fn = Len(fix_name)
    If len_fn < 2 Then
        response = MsgBox("Fix names must be 2 or more characters in length. Please Try again.", 17, "ERROR: Fix Name")
        If response = 2 Then Exit Sub
    End If
Loop Until len_fn > 1

Do
    txt_fix_type = InputBox("Enter the fix type: " & Chr(13) & "1 - NDB" & Chr(13) & "2 - VOR" & Chr(13) & "3 - SID Fix" & Chr(13) & "4 - ARR Fix" & Chr(13) & "5 - Aerodrome" & Chr(13) & "6 - Airway Fix" & Chr(13) & "7 - Fix", "FIX NAME")
    db_fix_type = txt_fix_type * 1
    If db_fix_type < 1 Or db_fix_type > 7 Then
        response = MsgBox("Please select one of the 7 recognized fix types. Please Try again.", 17, "ERROR: Fix Type")
        If response = 2 Then Exit Sub
    End If
Loop Until db_fix_type > 0 And db_fix_type < 8

Select Case db_fix_type
    Case 1
        fix_type = "NDB"
        Do
            txt_freq = InputBox("Enter the NDB frequency: ", "NDB Frequency", "000.0000")
        'check valid numeric value
            If IsError(txt_freq * 1) Then
                freqval = False
                MsgBox "Invalid frequency (Non numeric)"
            End If
            int_freq = txt_freq * 1
        '    length
            freqlen = Len(freq)
            If freqlen <> 8 Then
                freqval = False
                MsgBox "Improper length."
            End If
        '     has decimal character
            If IsError(decloc = InStr(freq, ".", vbTextCompare)) Then
                freqval = False
                MsgBox "Improper frequency format (no decimal)"
            End If
            decloc = InStr(freq, ".", vbTextCompare)
        '    decimal location
            If decloc <> 4 Then
                freqval = False
                MsgBox "Improper frequency format (decimal)"
            End If
        '  MHz
            If decloc - 1 <> 3 Then
                freqval = False
                MsgBox "Improper frequency format (MHz 000.)"
            End If
        '   KHz
            If decloc - 1 <> 3 Then
                freqval = False
                MsgBox "Improper freq format (KHz .0000)"
            End If
        '   freq within aeronautical range
            If int_freq < NavLower Or int_freq > NavUpper Then
                freqval = False
                MsgBox "Invalid frequency (Out of range 108.0000MHz - 117.9750 MHz)"
            End If
        '   freq fit within channel spacing
            khzfreq = int_freq * 1000
            If khzfreq Mod CHspac <> 0 Then
                freqval = False
                MsgBox "Invalid frequency (Channel Spacing 25KHz)"
            End If
        Do Until freqval = True
        int_freq = Round(int_freq, 4)
        MsgBox "Valid NDB Frequency" & Chr(13) & int_freq & " MHz"
        
    Case 2
        fix_type = "VOR"
    Case 3
        fix_type = "SID Fix"
    Case 4
        fix_type = "ARR Fix"
    Case 5
        fix_type = "Aerodrome"
    Case 6
        fix_type = "irway Fix"
    Case Else
        fix_type = "Fix"
End Select
MsgBox "Fix Type: " & fix_type


End Sub

I am getting an error ("Case without Select Case") with the line in red. I'm unable to test the rest of my code until I can correct this error, but I don't understand what is causing it. The variable db_fix_type should be a number between 1 and 7 as calculated.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hi
quick glance at your code and appears you are missing Loop in both of your Do loops

Rich (BB code):
       Do Until freqval = True
        int_freq = Round(int_freq, 4)
        MsgBox "Valid NDB Frequency" & Chr(13) & int_freq & " MHz"
        Loop
    Loop

If this is the issue - Make sure you you can exit them both.:)

Dave
 
Upvote 0
Solution
Thank you Dave, that appears to have been the problem. Some aditional errors exist preventing me from testing fully.
 
Upvote 0

Forum statistics

Threads
1,223,952
Messages
6,175,594
Members
452,655
Latest member
goranzoric

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top