![]() |
#1
|
|||
|
|||
![]()
I have a column with two numbers in each cell like this...
A1: 64 84 A2: 1000 87 A3: 14 999 A4: 32 65 I want to be able to add up only the first numbers in every cell, like this... 64 1000 14 32 ...I don't want the second numbers in the cell to be included in the calculation. Is there a way I can do this? Thanks Last edited by ewso; 05-22-2017 at 03:39 AM. |
#2
|
||||
|
||||
![]()
It is easy enough with a macro. The following assumes the result will go in the last cell in the column containing the cursor and that there are no merged/split cells in the table.
Code:
Sub CountNums() Dim oTable As Table Dim lngCount As Long: lngCount = 0 Dim oCell As Cell Dim oRng As Range Dim i As Integer Dim iCol As Integer Dim iRow As Integer Set oTable = Selection.Tables(1) iRow = oTable.Rows.Count If Selection.InRange(oTable.Range) = True Then iCol = Selection.Cells(1).ColumnIndex For i = 1 To iRow - 1 lngCount = lngCount + Split(oTable.Columns(iCol).Cells(i).Range.Text, Chr(32))(0) Next i Set oRng = oTable.Columns(iCol).Cells(iRow).Range oRng.End = oRng.End - 1 oRng.Text = lngCount End If lbl_Exit: Set oCell = Nothing Set oRng = Nothing Exit Sub End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
![]() Quote:
Hi, thanks for the code, but I get an error when I run it. It says... Compile error: User-defined type not defined What could be wrong? |
#4
|
||||
|
||||
![]()
You've asked this question in a Word forum but looking at the wording of your question, perhaps you are using Excel. Can you confirm that your data and macro is in Word?
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#5
|
|||
|
|||
![]()
Sorry about that. I'm using Excel, not Word. Thanks for telling me. Can someone please move this question to the right forum?
|
#6
|
||||
|
||||
![]()
I'm not a moderator here so I can't move it but I can answer it.
Rather than adapting Graham's code to work in Excel, I would first suggest you do it without a macro. It is easy enough to separate the two numbers into two columns and then do a normal sum formula on the first column. Select Column A and go to Data > Text to Columns and choose Delimited > Next > tick Space > Finish
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#7
|
|||
|
|||
![]() Quote:
|
#8
|
||||
|
||||
![]()
In that case, try this macro after selecting the cells you want totalled
Code:
Sub CountNums() Dim lngCount As Long: lngCount = 0 Dim oRng As Range For Each oRng In Selection.Areas(1) lngCount = lngCount + Split(oRng.Value, " ")(0) Next oRng MsgBox "The first numbers add up to: " & lngCount End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#9
|
|||
|
|||
![]()
If you want a formula, try
=SUM(--(MID(A1:A4;1;FIND(" ";A1:A4)-1))) which must be committed by holding down Ctrl and Shift before pressing Enter (array formula) |
#10
|
|||
|
|||
![]()
Thanks guys, that works!
|
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
michaelst | Excel | 9 | 04-22-2017 10:51 PM |
Difference between 2 numbers in same cell | Neila1982 | Excel | 1 | 02-12-2017 01:40 AM |
![]() |
Kilch | Excel | 3 | 01-31-2017 12:55 PM |
Search a cell that contains words and numbers and convert the numbers to metric | Carchee | Excel Programming | 36 | 10-08-2014 03:16 PM |
![]() |
jetdude68 | Word Tables | 1 | 07-20-2010 12:30 AM |