1

I am aware I'm not the first to have this question. Still I cannot find the solution. I have some vba code for my solidworks, that works fine. However, I want to use a string that I defined in one sub, in another sub. This seems harder than I thought.

My code - simplified:

Public FolderName As String
Public Sub SaveFiles()
   FolderName = "SOLID"  
End Sub

Public Sub UserInput(Parameter1, Parameter2, Parameter3...as string) 'A lot of input from a userform
  For i = LBound(ArrayList) To UBound(ArrayList)
    Debug.Print "FolderName = ", FolderName 'THIS PRINTS EMPTY AND I WANT IT TO PRINT "SOLID"
  Next
End Sub

I use public subs and a public string. I cannot add FolderName to Sub UserInput(FolderName), because then I get the compile error "Argument not optional" for my Call UserInput() that is written in my Userform

What can I do to solve this?

4
  • SaveFiles() must run before UserInput() in order for FolderName to be populated with "SOLID". We need to see the part of your code that calls these subs. Commented Oct 8, 2024 at 12:46
  • @BartMcEndree I'm not sure if I explain it correctly. SaveFiles opens my userform with UserForm.Show. Then the end of my Userform calls Call UserInput(...) Commented Oct 8, 2024 at 12:49
  • this might help stackoverflow.com/questions/40073952/… Commented Oct 8, 2024 at 12:56
  • 1
    ArrayList is VB.Net. In VBA Parameter Array Commented Oct 8, 2024 at 15:15

2 Answers 2

1
Public Sub UserInput(ParameterX As String, Optional FolderName As String = "a better default")

  doTheThings
End Sub
Sign up to request clarification or add additional context in comments.

2 Comments

Optional argument can get a default value in the declaration part. like this Optional FolderName As String = "some default" no need the IF...End If
Although this code might answer the question, I recommend that you also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes.
0

Passing Strings to Another Sub

The Calling Procedure

Public Sub SaveFiles()
    
    Dim FolderName As String: FolderName = "SOLID"
    
    ' Using a Parameter Array (Multiple Arguments)
    UserInput FolderName
    UserInput "Solid1", "Solid2", "Solid3"
    
    ' Using an Array (Allowing a Simple Value)
    UserInputArray "Solid4"
    UserInputArray Array("Solid5", "Solid6", "Solid7")
    
    ' Using a Delimited String
    UserInputString "Solid8" ' , ", " ' default
    UserInputString "Solid9, Solid10, Solid11" ' , ", " ' default
 
End Sub

Using a Parameter Array (Multiple Arguments)

Public Sub UserInput(ParamArray FolderNames() As Variant)
    Dim i As Long
    For i = LBound(FolderNames) To UBound(FolderNames)
        Debug.Print "FolderName = " & FolderNames(i)
    Next i
End Sub

Using an Array (Allowing a Simple Value)

Public Sub UserInputArray(FolderNames As Variant)
    If IsArray(FolderNames) Then
        Dim i As Long
        For i = LBound(FolderNames) To UBound(FolderNames)
            Debug.Print "FolderName = " & FolderNames(i)
        Next i
    Else
        Debug.Print "FolderName = " & FolderNames
    End If
End Sub

Using a Delimited String

Public Sub UserInputString( _
        ByVal FoldersString As String, _
        Optional ByVal Delimiter As String = ", ")
    Dim FolderNames() As String: FolderNames = Split(FoldersString, Delimiter)
    Dim i As Long
    For i = 0 To UBound(FolderNames)
        Debug.Print "FolderName = " & FolderNames(i)
    Next i
End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.