VBA Macro: Calling Multiple Lines of Code

Hi all

I've been designing a macro since the beginning of the year to automate drawings. Depending on my parts, they have options that I have set and some of its options are pairs of two options. I was recently told that rather than copying / pasting for the latter it was possible to do like calls to make my macro more understandable but I have absolutely no idea how to go about it.

I give you as an example option n°1 that I have set as below, moreover the selection of each option is made according to the value of "U" example: U=1 then make option n°1.

  ElseIf U = 1 Then

boolstatus = Part.Extension.SelectByID2("Right Plan", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
If O < 6 / 1000 Then
Set SkCircle = Part.SketchManager.CreateCircle(-Length / 1000 - M + 0.0035, 0, 0, -Length / 1000 - M + 0.0035, -0.001, 0)
ElseIf O >= 6 / 1000 Then
Set SkCircle = Part.SketchManager.CreateCircle(-Length / 1000 - M + 0.0035, 0, 0, -Length / 1000 - M + 0.0035, -0.001, 0)
End If
boolstatus = Part.Extension.SelectByID2("Arc1", "SKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)
Part.FeatureManager.FeatureCut False, False, False, 1, 0.04, 0.04, False, False, False, 0.01745329251994, 0.01745329251994, False, False, False, False, 0, 1, 1
Part.SelectionManager.EnableContourSelection = 0

The option I'm trying to set more briefly is n°5 which merges option n°1 and n°2.

Thank you in advance for your attention

1 Like

Select case U

Box 1, 2, 5

if U = 1 OR U=5 then

'Thing to do

end if

if U = 2 OR U=5 then

'Thing to do

end if

Box 3

Box 4

Case Else

End Select

1 Like

Hello

The advice you were given to lighten your code is to use the "call" function

Like what:

In your main module

Sub macro 1

if foo = 1

Call function 1

if foo =2

Call function 2

if foo = 3

Call function 3

End sub

On other modules

Sub function 1

Lots of line
Lots of line
Lots of line
Lots of line

End sub

Sub function 2

Lots of line
Lots of line
Lots of line
Lots of line

End sub

Sub function 3

Lots of line
Lots of line
Lots of line
Lots of line

End sub

This allows you to have several small and readable blocks rather than a single illegible block where you struggle to find your little ones.

In your case, you will have as many functions as options, each in their "Sub" and then a "Sub" that does the tests and calls the corresponding functions.

I hope I helped you.

a+

Rémi

Thank you both very much for your answers, I'll put it all into practice now

a+