Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-21-2017, 11:43 PM
ewso ewso is offline Add only first numbers in each cell Windows 10 Add only first numbers in each cell Office 2016
Advanced Beginner
Add only first numbers in each cell
 
Join Date: Nov 2016
Posts: 80
ewso is on a distinguished road
Default Add only first numbers in each cell

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.
Reply With Quote
  #2  
Old 05-22-2017, 12:48 AM
gmayor's Avatar
gmayor gmayor is offline Add only first numbers in each cell Windows 10 Add only first numbers in each cell Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #3  
Old 05-22-2017, 01:08 AM
ewso ewso is offline Add only first numbers in each cell Windows 10 Add only first numbers in each cell Office 2016
Advanced Beginner
Add only first numbers in each cell
 
Join Date: Nov 2016
Posts: 80
ewso is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
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

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?
Reply With Quote
  #4  
Old 05-22-2017, 02:41 AM
Guessed's Avatar
Guessed Guessed is offline Add only first numbers in each cell Windows 10 Add only first numbers in each cell Office 2013
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,158
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #5  
Old 05-22-2017, 02:57 AM
ewso ewso is offline Add only first numbers in each cell Windows 10 Add only first numbers in each cell Office 2016
Advanced Beginner
Add only first numbers in each cell
 
Join Date: Nov 2016
Posts: 80
ewso is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
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?
Sorry about that. I'm using Excel, not Word. Thanks for telling me. Can someone please move this question to the right forum?
Reply With Quote
  #6  
Old 05-22-2017, 03:12 AM
Guessed's Avatar
Guessed Guessed is offline Add only first numbers in each cell Windows 10 Add only first numbers in each cell Office 2013
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,158
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #7  
Old 05-22-2017, 03:19 AM
ewso ewso is offline Add only first numbers in each cell Windows 10 Add only first numbers in each cell Office 2016
Advanced Beginner
Add only first numbers in each cell
 
Join Date: Nov 2016
Posts: 80
ewso is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
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
Thanks guessed, that's a solution, but I actually need to keep them in one column because of other things going on in the spreadsheet, so hopefully I can get a macro that works in Excel.
Reply With Quote
  #8  
Old 05-22-2017, 03:45 AM
Guessed's Avatar
Guessed Guessed is offline Add only first numbers in each cell Windows 10 Add only first numbers in each cell Office 2013
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,158
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #9  
Old 05-22-2017, 04:15 AM
xor xor is offline Add only first numbers in each cell Windows 10 Add only first numbers in each cell Office 2016
Expert
 
Join Date: Oct 2015
Posts: 1,100
xor is a name known to allxor is a name known to allxor is a name known to allxor is a name known to allxor is a name known to allxor is a name known to all
Default

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)
Reply With Quote
  #10  
Old 05-24-2017, 09:43 AM
ewso ewso is offline Add only first numbers in each cell Windows 10 Add only first numbers in each cell Office 2016
Advanced Beginner
Add only first numbers in each cell
 
Join Date: Nov 2016
Posts: 80
ewso is on a distinguished road
Default

Thanks guys, that works!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Add only first numbers in each cell Sorting numbers within cell 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
Add only first numbers in each cell Add Numbers from One or Another Cell 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
Add only first numbers in each cell adding numbers in same cell jetdude68 Word Tables 1 07-20-2010 12:30 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:13 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft