Combining string values

E

Eric de Leeuw

Hello,

in my project I have a couple of strings that function as a counter. The
count the number of loops that have been made

Now I want to make a total counter that adds the values of the seperate
counters

eg.
Dim strCounter1 as String 'eg value = 5
Dim strCounter2 as String 'eg value = 5
Dim strCounterTotal as String 'I want this counter to show 10

strCounterTotal = strCounter1 + strCounter2

The last line now returns 55 instead of 10
How do I do this trick

Thanx in advance
 
G

Graham Mayor

If you look up the use of the + operator in vba help, you will see that in
order for the plus sign to sum values, they must be numeric data values and
not strings i.e. (Byte, Boolean, Integer, Long, Single, Double, Date,
Currency, or Decimal)
Change the Dim statements from String to Long

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Greg Maxey

Eric,

Using strings the "+" simply joins the two strings "5" + "5" becomes "55"

You need to turn the strings into numbers. Something like:

Sub ScratchMaco()
Dim strCounter1 As String
Dim strCounter2 As String
Dim strCounterTotal As String
strCounter1 = 5
strCounter2 = 5
strCounterTotal = Val(strCounter1) + Val(strCounter2)
MsgBox strCounterTotal
End Sub
 
E

Eric de Leeuw

Hi Graham,

seting the strings to Long didnt work as you already mentioned yourself.
The solution as pointed out bij Greg does works okay for me

Thank you for your reply

Eric
 
G

Graham Mayor

While Greg's method works on the same principle - that you can only add
numeric values - are you saying that

Dim strCounter1 As Long 'eg value = 5
Dim strCounter2 As Long 'eg value = 5
Dim strCounterTotal As String 'I want this counter to show 10
strCounter1 = 5
strCounter2 = 5

strCounterTotal = strCounter1 + strCounter2
MsgBox strCounterTotal

doesn't give the result 10?

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top