Combining string values

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
0
Utf
2/6/2010 12:20:01 PM
word.vba.general 1023 articles. 1 followers. Follow

5 Replies
1299 Views

Similar Articles

[PageSpeed] 54

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

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>



"Eric de Leeuw" <Eric de Leeuw@discussions.microsoft.com> wrote in message 
news:3456A7FE-C503-433E-BE4E-AA01EEF41852@microsoft.com...
> 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 


0
Graham
2/6/2010 12:40:23 PM
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



Eric de Leeuw wrote:
> 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 


0
Greg
2/6/2010 4:09:39 PM
Hi Greg,

the Val( ) argument works just fine

Thank you
'
Eric

"Greg Maxey" wrote:

> 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
> 
> 
> 
> Eric de Leeuw wrote:
> > 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 
> 
> 
> .
> 
0
Utf
2/6/2010 6:50:01 PM
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

"Graham Mayor" wrote:

> 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
> 
> My web site www.gmayor.com
> Word MVP web site http://word.mvps.org
> <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
> 
> 
> 
> "Eric de Leeuw" <Eric de Leeuw@discussions.microsoft.com> wrote in message 
> news:3456A7FE-C503-433E-BE4E-AA01EEF41852@microsoft.com...
> > 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 
> 
> 
> .
> 
0
Utf
2/6/2010 6:51:03 PM
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

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>



"Eric de Leeuw" <EricdeLeeuw@discussions.microsoft.com> wrote in message 
news:3648E537-7C6F-4A31-8EBD-048CC325462C@microsoft.com...
> 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
>
> "Graham Mayor" wrote:
>
>> 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
>>
>> My web site www.gmayor.com
>> Word MVP web site http://word.mvps.org
>> <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
>>
>>
>>
>> "Eric de Leeuw" <Eric de Leeuw@discussions.microsoft.com> wrote in 
>> message
>> news:3456A7FE-C503-433E-BE4E-AA01EEF41852@microsoft.com...
>> > 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
>>
>>
>> .
>> 


0
Graham
2/7/2010 8:11:44 AM
Reply:

Similar Artilces:

Making a set value appear in a cell
Hi, I'm new to excel, I apologize if this question is too basic. I work for an accountant, and I have to enter an account purpose and the account number for that purpose in a cell right next to it. Is there any way to enter the purpose and make the set value of the account number appear next to it automatically? For example if I enter ABC for purpose, can I make a (fake) account number of 50-5025-02 appear next to it automatically? And the same goes for other purposes and their respective account numbers. You can do this using a VLOOKUP formula. First you would need to set up a...

int to string (hex)
How can I convert int to CString, but as hex: int i = 10; CString cNum = IntToStrHex(i); //cNum == "$0A" Thank you. "Oliver Young" <none> wrote in message news:%232jIT$BsDHA.2340@TK2MSFTNGP12.phx.gbl... > > How can I convert int to CString, but as hex: int integer = 9; CString s; s.Format("%x",integer); Cheers Richard. "rf" <making.it.up@the.time> wrote in message news:Cumvb.20426$aT.430@news-server.bigpond.net.au... > > "Oliver Young" <none> wrote in message > news:%232jIT$BsDHA.2340@TK2MSFT...

How to get #value! to return a value of 0?
My formula is like this: =A2+B2+C2. However, some of the values in the B2 column are "--," indicating "not applicable". When I use the formula, it interprets this as text, of course, and returns the error value of #VALUE! I want it to treat these as zeroes. How can I do this? I tried doing a FIND AND REPLACE, but it did not recognize any of the "-- " values in the FIND. Can anybody help? =SUM(A2:C2) would be the easiest way. SUM will ignore text. tj "rhodesv" wrote: > My formula is like this: =A2+B2+C2. However, some of the values i...

Need help converting a CString to 8 bit Ascii char string
I having a problem passing an 8 bit char string to an SDK api. What I am doing now is: "cs" is CString char cPAGER_CODE[16]; strcpy(cPAGER_CODE, (char *) (LPCSTR) cs); <-------is this converstion correct? The API to this SDK calls for an 8 bit char string to pass to it, but it doesn't like what I do. Any help on my conversion above? Thanks! jt Do you have UNICODE defined? If you do then you will have to convert from Unicode string, if not - you can use cs directly strcpy(cPager_Code, cs); -- - Mark Randall http://zetech.swehli.com "jt" <jtsof...

Zero's after value
I want to be able to type in a value e.g. 10 and excel automatically adds zeros to the end of the value to make it 10000, so when i type in 20 it automatically changes it to 20000 ------------------------------------------------ ~~ Message posted from http://www.ExcelTip.com/ ~~View and post usenet messages directly from http://www.ExcelForum.com/ Do tools>options>edit and select fixed decimal places, put it to -3. Don't forget to uncheck it after you are done. If you need to enter smaller numbers, just put a period after the number and it won't change so if you want to type...

Combine different worksheets
Hi, I am working with Excel 2003. I have separate worksheets in different workbooks, each sheet has various rows (from 80 to 800) and all sheets only use 3 columns.I only use one sheet in each workbook. I want to make a summary sheet every 4 weeks which will have all the information from the four sheets. At the present time I'm copying and pasting it as I want to keep the original weekly sheets. Is there an easier way to do this? I do not know Visual Basic. Thanks Chuck If cannot use VBA then you are pretty well stuck with the manual operation. As you get into more advanced...

Sum up values that falls between two specific dates
Hello :) Please consider this scenario. I have two dates: let's call them receive date and disbursement date. In between them I receive installments and I disburse some as well. Receive Date Received Amount Disbursement Date ------------ --------------- ------------------ 07 May 100000000000 11 May 15 May 100000000000 26 May 22 May 100000000000 26 May Now the question is, how do I sum up the installments I receive between two disbursement dates, that is, between 11th and 26th of May? Thank you for your help. Hi Judicato Assuming your ...

Retrieving Value From Subform Only Works In Break Mode 10-18-07
The code ------------------------------------------------------------- 2550 With Me.subSecurity.Form 2551 If Me.subTrades.Enabled = True Then 2552 Me.subTrades.Form!txtParTotal.Requery 2553 curParTotal = Me.subTrades.Form!txtParTotal.Value 'MsgBox curParTotal 2554 !txtParTotal.Value = curParTotal 2555 !txtParTotal.Requery 2556 End If 2559 End With ------------------------------------------------------------- If I put a break on line 2552 or any line above it, the value gets retrieved and everything works a-ok. But with a break on line 2553 or no break at all...

Text Values #2
I'm tryng to build a spreadsheet to track employee vacations. How do I sum 8 hours if a cell has a "V" entered and 4 hours if a cell has an "X" entered? Assuming you're using column A, and each row is either the number of hours worked, or a V or a X, then use a helper column, B, and insert this in B1 and copy down.......change the A1's to A2's if you have a header in Row 1, and put this in B2. =IF(A1="V",8,IF(A1="X",4,A1)) Vaya con Dios, Chuck, CABGx3 "Brando" <carpenterchsd@earthlink.net> wrote in message news:1113...

Cannot insert the value NULL into column 'BASEUOFM', table 'tempdb
I am getting the same thing as in this thread when we click Item lists. This is after running checklinks over the weekend. So last night I ran an inventory reconcile (takes several hrs) and we still get the error. http://www.microsoft.com/communities/newsgroups/en-us/default.aspx?query=cannot+insert+null&dg=microsoft.public.greatplains&cat=en_US_0b1d9899-b5ae-4a2d-b107-8d45e53db206&lang=en&cr=US&pt=&catlist=&dglist=&ptlist=&exp=&sloc=en-us&mid=359cb47b-9bc5-4077-8297-5ff29998eb18 Thanks. So I noticed checklinks created three new items .....

Automatically change list values
Hi, I would like to alter a spreadsheet so that the list values in B1 fo instance would change depending one of two options placed in A1. Th two columns containing the list info would be in column g1 to g20 an f1 to f20. thanks for any assistance -- DKer ----------------------------------------------------------------------- DKerr's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=1308 View this thread: http://www.excelforum.com/showthread.php?threadid=27040 In B1 use this formula: =vlookup(A1,$F$1:$G$20,2) This assumes lookup value is in column F and value t...

Checking values in different fields.
Hello, I was wondering if anyone could help me with something. I currently have a table with stock levels and a re-order level. When the stock level gets below the re-order level I then contact the supplier and order new stock. I was wanting to create a query that checks the values of these two fields and then shows me all the records that need re-ordering. I however, don't know how to compare the values of two fields. Could anyone give me a hand? Many thanks. On Wed, 9 May 2007 15:35:01 -0700, rednikcam wrote: > Hello, I was wondering if anyone could help me with something....

Won't use AFX string resource in user DLL
I have the following situation: -- a DDX_Text, that is used on a double; this should give me a message (AFX_IDP_PARSE_REAL) if the user types in a bad string. -- this code is in an .ocx which is compiled as a non-AFX DLL. -- when I actually run it and try a bad string, I get a debug assertion and message: Error: failed to load message box prompt string 0xf111, followed by a blank message box. The program can't load the AFX_IDP_PARSE_REAL message string. Are there any solutions other than "compile it as an AFX DLL" (which does in fact work, but isn't practical for me)? -- ...

Combining Outlook archives
I have two folders where Outlook archives are stored. Is it possible to merge these files so that I only have one archive file? Regards, John Spivey I'm no expert but I would: 1. open Outlook 2. import each archive 3. re-archive the new Outlook data file into a new location or with a different name, resulting in one combined archive "John Spivey" <spivey@bigpond.net.au> wrote in message news:GVGhh.10212$HU.5311@news-server.bigpond.net.au... >I have two folders where Outlook archives are stored. Is it possible to >merge these files so that I only have one arc...

CONSTRAINT to disable the update of some values
Hello, I have a a table with data from some operations. The status (char(2)) of the operations can be changed. But there are some status that are "final" and should not be changed. Is there a way the create a constraint that checks the previous status? So if the previous is, for example, "fi" (final), won't allow an update? Thanks!!! -- ____________________________ Carlos Sosa Albert You cannot do this with a constraint. You would need a trigger. Tom "Carlos Sosa Albert" <betun (arroba) hotmail> wrote in message news:586FA...

columnar output by group variable values
MS Access allows printing record details by a group value. It begins a new horizontal section on each value in the grouped variable. Is there a way of doing the same thing to produce vertical (columnar) output? That is, on each new value in the group variable it prints a new column of records data. -- Peter Peter wrote: >MS Access allows printing record details by a group value. It begins a new >horizontal section on each value in the grouped variable. Is there a way of >doing the same thing to produce vertical (columnar) output? That is, on each >new value in the group ...

How can I count values in multiple non-consecutive cells?
I'm trying to figure out what formula to use when I want to count how many times a value is found in a specific set of cells. You mention non-consecutive in the subject, but not in the body. Why don't you give more details of the data and the required results? -- HTH Bob "Kevin M." <Kevin M.@discussions.microsoft.com> wrote in message news:1EF850CF-0E69-4370-BF04-3C25E1117F44@microsoft.com... > I'm trying to figure out what formula to use when I want to count how many > times a value is found in a specific set of cells. ...

Error Message with Duplicate Values
I have a table called Patient Data that contains Social Security Numbers. – the field is called SSN I do not want duplicate SSN’s in the database, On my input form, I have the following event procedure on the Before Update line Private Sub textSSN_BeforeUpdate(Cancel As Integer) If SSN = DLookup("SSN", "Patient Data", "[SSN] = '" & textSSN & "'") Then MsgBox "SSN exists. Verify SSN." Cancel = True Me.textSSN.SelStart = 0 Me.textSSN.SelLength = Len(Me.textSSN) End If End Sub The message box with “SSN e...

Referencing a Row value from within a VLOOKUP
Hello group, I've worked with excel for many years, but I could use some help throwing this together. The background: I have two sheets within the same workbook. One sheet has check numbers and values that have been issued by the company. The other sheet has that same information, but only what has been reported back to us by the bank. I working on streamlining the comparison between the two sheets to more easily see what checks are outstanding (have not been cashed at the bank). Also, if the check has been cashed, I want to compare the value that the bank recorded with the v...

Converting a Cell Value to Cell Reference...
Here's the deal: I've set up a MAX formula that displays resultant value, and I need to figure out how to "tell" a particular function what cell that value is located in. I am working with the OFFSET function, and I need a cell reference as the first parameter, and all that I can provide right now is the value for a MAX from a chosen range. Any advice is greatly appreciated. ** Posted via: http://www.ozgrid.com Excel Templates, Training, Add-ins & Business Software Galore! Free Excel Forum http://www.ozgrid.com/forum *** I suppose what I'm asking is... Is there ...

converting formula values into just numbers?
I need to manipulate with a macro, as absolute numbers, the numerical values returned from a formula. Instead of say.....1,3,5,8, etc. the macro sees the corresponding formula when referred to these cells and can't work with it. How can I return just straight numbers from the formula calculations? Thanks One way: Dim myValue As Double myValue = Range("A1").Value In article <OPPdt4k0DHA.2396@TK2MSFTNGP09.phx.gbl>, "DNunley" <dnunley@earthlink.net> wrote: > I need to manipulate with a macro, as absolute numbers, the numerical values > re...

Set Textbox value by selecting item from combobox but it must be editable
Hi, >I have one projrct in MS Access. >I have one form in which there is one Combobx(PalletNo_combo) and one Textbox(ShippedQuantity). >Now when I select an item from Combobox then it will automatically set the value of Textbox. >For that I am using code in control source properties of Textbox =(DLookUp("val([QTYONHAND])","PRODUCTION","PalletNo=[Forms]! [ShippingEntry]![PalletNo_Combo]")) >Uptil here its fine. >But after that, in some scenario,I have to change the value of that Textbox. >But when I click on the Textbox and enter something ...

Can I set an audio alert that triggers as Excel cell value chgs?
I would like to add an audio alert to a spreadsheet cell whenever it changes to a specific value? Can this be done? ....I'm not familiar with any audio alerts but, you can use conditional formatting to the cell's format change when it value changes. Go to Format...Conditional Formatting. You can then either set a value or use a formula and set the format (what it will look like when the cell is the value that you set). "SellUnHi" wrote: > I would like to add an audio alert to a spreadsheet cell whenever it changes > to a specific value? Can this be done? L...

Trim out last seven characters from a string...
Is this easy to do? The last seven characters are constants, I just want what precedes them... DubboPete give this a try =MID(A1,1,LEN(A1)-7) -- Gary Keramidas Excel 2003 "DubboPete" <osnabruc@tpg.com.au> wrote in message news:a1eb7259-21b8-406b-baea-e65c0669aaec@k2g2000pro.googlegroups.com... > Is this easy to do? The last seven characters are constants, I just > want what precedes them... > > DubboPete On Feb 2, 3:04=A0pm, "Gary Keramidas" <gkerami...@XXMSN.com> wrote: > give this a try > > =3DMID(...

Graph values in ascending order
I would like my graph to shows values in ascending order on the x-axis. I know how to do this manually however as the values change on a weekly basis I would like the graph to autoimatically do this for. regards Ant Ant A graph represents the data as you have laid it out on the data sheet. If you want the data in ascending order, you will have to sort on a weekly basis. To do it automatically would require VBA, the graph will not do it for you. Gord Dibben Excel MVP On Thu, 6 Jan 2005 04:01:19 -0800, "Ant anthony.greening@bisley.com" <anonymous@discussions.microsoft.com...