View Full Version : how to store comments in a string?
antonyjones1
02-05-2012, 02:59 AM
Hi there,
I'm looking to store some comments in a string that I can then call at later points if certain criteria is met. Would the below be along the right track?
Dim Output1 As String
Dim Output2 As String
Dim Output3 As String
Output1 = "Hello"
Output2 = "Hi"
Output 3 = "Hey"
macropod
02-05-2012, 03:15 AM
That's one of many ways. Another might be:
Dim StrCmt as String
StrCmt =",Hello,Hi,Hey,"
(note the commas at eithe end of the string, plus those separating the expressions)
This can be useful if you have many strings and you want to refer to them by number. For example:
Split(StrCmt, ",")(1)
will return 'Hello'.
Also useful if you want to find out if a proposed comment is valid, which you could do with code like:
If Instr(StrCmt, "," & InputBox("Test Comment") & ",") > 0 Then
MsgBox "Valid Comment"
Else
MsgBox "Invalid Comment"
End if
antonyjones1
02-05-2012, 03:44 AM
Thanks for the prompt reply! This looks like the best way forme to do it as I will want to incorporate them into an if/else statement
Paul_Hossler
02-05-2012, 06:41 AM
An alternative to consider in addition to Paul's E.'s approach
If you have a lot of messages, or if they change/grow, and the messages really don't need to be in a string
I'd rather let the computer keep track of some things. Using 'Enum' allows the VBA Intellisense to kick in and makes IMHO makes it easier to pick the message I want
Option Explicit
Enum tMsg
tSmaller = 0
tSameAs = 1
tLarger = 2
tCommas = 3
tQuotes = 4
End Enum
Dim aMsgs As Variant
Sub InitMsgs()
aMsgs = Array( _
"Smaller than", "Same As", "Larger than", _
"Msg, with, commas", _
"Msg with ""QUOTE CHARS"" quotes")
End Sub
Sub ShowMsg(msgnum As tMsg)
If CLng(msgnum) < LBound(aMsgs) Or CLng(msgnum) > UBound(aMsgs) Then
MsgBox "Bad message number passed = " & msgnum
Else
MsgBox aMsgs(msgnum)
End If
End Sub
Sub drv()
Call InitMsgs
If 1 > 0 Then ShowMsg (tLarger)
If 1 < 2 Then ShowMsg (tSmaller)
If 1 = 1 Then ShowMsg (tSameAs)
If True Then ShowMsg (tCommas)
If Not False Then ShowMsg (tQuotes)
If True Then ShowMsg (111)
End Sub
You can really get carried away with this.
For Example, each msg in the array could start with a 'tag' and you could search the array for the msg that starts with that tag. That way you don't need to worry about a lot of housekeepng because the computer is doing it for you, and the message selection would be more intuitive
e.g. Array = "##LT##Less Than", etc. and then Call ShowMsg("lt"), using 'LT" instead of 0
Paul H.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.