0

I'm working on an AutoCAD VBA program that creates a drawing. But i have a slight problem. If "Case 1" is True Then "Case 2" must be false. Here is my code :

Sub Pumps()

'Option for type of pump piping
 ans = InputBox("1 = STD Piping" & vbCrLf & _
                "2 = Omit Pump", "Pump Piping")

Select Case ans

Case "1":

: Set layerObj = ThisDrawing.Layers.Add("PUMP-PIPING STD -" & Size)
            layerObj.LayerOn = True
Case "2":

: Set layerObj = ThisDrawing.Layers.Add("OMIT PUMP -" & Size)
            layerObj.LayerOn = True

Case Else: MsgBox "Wrong Input Dude.", vbCritical, MSG: Exit Sub

End Select

End Sub

Please Assist

Community
  • 1
  • 1
K Bizzy
  • 85
  • 1
  • 13
  • 1
    what is your actual problem? – Jeremy Jul 14 '17 at 11:51
  • @Jeremy If Case1 is true, I want Case 2 to be false and if Case 2 is True , I want Case1 to be false – K Bizzy Jul 14 '17 at 11:59
  • 1
    *"i have a slight problem. If "Case 1" is True Then "Case 2" must be false."* - Yes, that is true. I don't see why that's a problem. The user can only input "1" or "2", not both at the same time. Your code works, as far as I can tell. – CLR Jul 14 '17 at 11:59
  • well, what you said is true. Is your code not executing as expected? – Jeremy Jul 14 '17 at 12:01
  • If you enter `1`, then your code will **only** add the `PUMP-PIPING STD` object. If you enter `2`, **only** the `OMIT PUMP` object. You're using `Select Case` properly. What makes you think that this is not the case? – CLR Jul 14 '17 at 12:02
  • If you found one of the answers to be helpful or answered your question, please mark it as the answer. – Adam Vincent Jul 31 '17 at 16:37

2 Answers2

1

So I'm not sure what the end goal was, but hopefully this gets you started.

What I've done I've isolated the "decision making" to the switch block, and that sets the toggle variable which is needed for the "work" to be done later. (and I would consider moving that "work" of actually setting the layerObj out to another Sub

Sub Pumps()
    Dim ans As String

    'Option for type of pump piping
     ans = InputBox("1 = STD Piping" & vbCrLf & _
                    "2 = Omit Pump", "Pump Piping")
    Dim toggle As Boolean

    Select Case ans
        Case "1": toggle = True
        Case "2": toggle = False
        Case Else: MsgBox "Wrong Input Dude.", vbCritical, MSG: Exit Sub
    End Select

    Set layerObj = ThisDrawing.Layers.Add("PUMP-PIPING STD -" & Size)
        layerObj.LayerOn = toggle

    Set layerObj = ThisDrawing.Layers.Add("OMIT PUMP -" & Size)
        layerObj.LayerOn = Not (toggle)
End Sub
Adam Vincent
  • 3,281
  • 14
  • 38
1

Remove the : from your code all over. Every single one of them.

It makes plenty of problems, especially with Conditions. You really cannot follow what is happening.

In general, the : means that you want the next line to stay on the similar line. It is useful only when you want to assign values to a newly declared variable like this: Dim k as long: k = 5 and its idea is to save space.

Take a look at this topic, you would understand what I mean: VBA - How the colon `:` works in VBA code with condition

Vityata
  • 42,633
  • 8
  • 55
  • 100