View Full Version : error when puting more than 1 string
brendaburgos
07-28-2012, 10:44 PM
Hello, I'm a newbie in Excel VBA, this may sound stupid but I encounter error with the below code, whats wrong?
Public Sub callDisplayTest()
displayTest ("First", "Second")
End Sub
Public Sub displayTest(firstStr as String, secondStr as String)
MsgBox (firstStr)
MsgBox (secondStr)
End Sub
HaHoBe
07-28-2012, 11:15 PM
Hi, brendaburgos,
donīt use brackets in callDisplayTest:
Public Sub callDisplayTest()
displayTest "First", "Second"
End Sub Ciao,
Holger
Just to add to Holger's comments, I would ditch the parenthesis for the MsgBox arguments as well. If you are returning a value from the MsgBox, then they get used.
Option Explicit
Public Sub callDisplayTest()
displayTest "First", "Second"
If MsgBox("Go again?", vbQuestion Or vbYesNo Or vbDefaultButton2, vbNullString) = vbYes Then
Call callDisplayTest
End If
End Sub
Public Sub displayTest(ByVal firstStr As String, ByVal secondStr As String)
MsgBox firstStr
MsgBox secondStr
End Sub
Just in case it helps, you could also use a ParamArray, if you are not sure how many strings there are to pass.
Sub DisplayArray()
Dim str1 As String
Dim str2 As String
Dim str3 As String
str1 = "First"
str2 = "Second"
str3 = "Third"
Call Test(str1, str2, str3)
End Sub
Sub Test(ParamArray MyArray() As Variant)
Dim i As Long
If Not UBound(MyArray) < LBound(MyArray) Then
For i = LBound(MyArray) To UBound(MyArray)
MsgBox MyArray(i), vbOKOnly, vbNullString
Next
End If
End Sub
Hope that helps,
Mark
Teeroy
07-29-2012, 01:13 AM
And just to round out the discussion (because there's more than one way to do almost everything in VBA) your first try only needed a "Call" in front of it.
Call displayTest ("First", "Second")
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.