0
votes

I'm using VBA in Word 2013 to score and return some basic info that the end user will input in the document in some legacy form fields. My code, which works fine if I have it all in a single sub, is:

Option Explicit

Sub ScoreLevel()

Dim a As Integer
Dim b As Integer
Dim Score as Integer
Dim Level as String

If ActiveDocument.FormFields("Text37").Result < "33" Then
    a = 2
Else
    a = 0
End If

If ActiveDocument.FormFields("Text40").Result = "0" Then
    b = 0
ElseIf ActiveDocument.FormFields("Text40").Result = "1" Then
    b = 1
ElseIf ActiveDocument.FormFields("Text40").Result > "1" Then
    b = 2
Else
    b = 0
End If

Score = a + b

Select Case Score
    Case 0 To 1
        Level = "Low"
    Case 2 To 3
        Level = "Moderate"
    Case Is > 3
        Level = "High"
End Select

MsgBox "Score = " & Score & vbNewLine & "Level: " & Level 

End Sub

Now I'm trying to separate the a variable and the b variable routines into their own (individual) sub routines, but the variables did not pass down. Any help how to do this?

1

1 Answers

0
votes

There are two ways to do this, either you declare the separate subroutines e.g. sub mySub(myVar as integer) then call the sub within your code with: mySub(b) The 2nd way is to declare the variables outside any subroutines like: public b as integer then you can simply use them in all subroutines.