Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 21
Default Macro to add numbers in a text string

Hi
I hope this someone can help with this

In column G I have a "Total" column Column I has a text string (Heading of "Result" as below

Total Result


25 Apples 10 Oranges 3 Grapes 12
15 Melons 10 Grapes 5
Oranges 8 Grapes 18

So basically sometimes the value is missing in the "Total" which is the sum of the numbers in the Results column. I would like some code to add the numbers in the result column and only add it to the total column if no value is entered. So the result would be

Total Result


25 Apples 10 Oranges 3 Grapes 12
15 Melons 10 Grapes 5
26 Oranges 8 Grapes 18

Many thanks for any help
  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 3,872
Default Macro to add numbers in a text string

Hi again,

Am Fri, 5 Dec 2014 19:13:25 +0100 schrieb Claus Busch:

End If
rngC = No1 + No2 + No3


the lines above are reversed.
Change the code to:

Sub myTotal()
Dim LRow As Long, mySum As Long, i As Long
Dim No1 As Long, No2 As Long, No3 As Long
Dim rngC As Range
Dim myStr As String

With ActiveSheet
LRow = .Cells(Rows.Count, "H").End(xlUp).Row
For Each rngC In Range("G2:G" & LRow)
No1 = 0: No2 = 0: No3 = 0
If Len(rngC) = 0 Then
myStr = rngC.Offset(, 1)
For i = 65 To 122
myStr = Replace(myStr, Chr(i), "")
Next
Select Case Len(myStr) - Len(Replace(myStr, " ", ""))
Case 3
No1 = Left(myStr, InStr(2, myStr, " ") - 1)
No2 = Mid(myStr, InStr(2, myStr, " ") + 2)
Case 5
No1 = Left(myStr, InStr(2, myStr, " ") - 1)
No2 = Mid(myStr, InStr(3, myStr, " ") + 1, 2)
No3 = Mid(myStr, InStrRev(myStr, " ") + 1)
End Select
rngC = No1 + No2 + No3
End If
Next
End With
End Sub


Regards
Claus B.
--
Vista Ultimate / Windows7
Office 2007 Ultimate / 2010 Professional
  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 3,872
Default Macro to add numbers in a text string

Hi,

Am Fri, 5 Dec 2014 22:25:34 +0100 schrieb Claus Busch:

Sub myTotal2()


and another suggestion:

Sub myTotal3()
Dim LRow As Long, i As Long
Dim rngC As Range
Dim myStr As String

With ActiveSheet
LRow = .Cells(Rows.Count, "H").End(xlUp).Row
For Each rngC In .Range("G2:G" & LRow)
If Len(rngC) = 0 Then
myStr = rngC.Offset(, 1)
For i = 65 To 122
myStr = Replace(myStr, Chr(i), "")
Next
myStr = Replace(Replace(myStr, " ", "+"), " ", "=")
rngC = Evaluate(myStr)
End If
Next
End With
End Sub


Regards
Claus B.
--
Vista Ultimate / Windows7
Office 2007 Ultimate / 2010 Professional


  #6   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2
Default Macro to add numbers in a text string

Hi Claus
Thank you for all your suggestions. I'm not at work at the moment but will try these suggestions when I get back.
Eddie
  #7   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1,045
Default Macro to add numbers in a text string

On Fri, 5 Dec 2014 09:10:04 -0800 (PST), wrote:

Hi
I hope this someone can help with this

In column G I have a "Total" column Column I has a text string (Heading of "Result" as below

Total Result


25 Apples 10 Oranges 3 Grapes 12
15 Melons 10 Grapes 5
Oranges 8 Grapes 18

So basically sometimes the value is missing in the "Total" which is the sum of the numbers in the Results column. I would like some code to add the numbers in the result column and only add it to the total column if no value is entered. So the result would be

Total Result


25 Apples 10 Oranges 3 Grapes 12
15 Melons 10 Grapes 5
26 Oranges 8 Grapes 18

Many thanks for any help


Here's another macro:

==================================
Option Explicit
Sub TotalResults()
Dim R As Range, V As Variant
Dim RE As Object, MC As Object, M As Object
Const sPat As String = "\d+"
Dim I As Long, J As Long

With Worksheets("sheet1") 'Or ActiveSheet or any named worksheet
Set R = .Range("I1", .Cells(.Rows.Count, "I").End(xlUp)).Offset(columnoffset:=-2).Resize(columnsize:=3)
V = R
End With

Set RE = CreateObject("vbscript.regexp")
With RE
.Global = True
.Pattern = sPat
End With

For I = 2 To UBound(V)
If IsEmpty(V(I, 1)) And RE.test(V(I, 3)) = True Then
J = 0
Set MC = RE.Execute(V(I, 3))
For Each M In MC

J = J + M
Next M
V(I, 1) = J
End If
Next I

R = V

End Sub
============================

By the way, it would not be difficult to run this on ALL the rows, not just those with totals missing.
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Extract numbers from a string of text Huber57 Excel Discussion (Misc queries) 0 May 27th 10 09:53 PM
Formula Text String: Formatting Text and Numbers? dj479794 Excel Discussion (Misc queries) 5 June 30th 07 12:19 AM
extracting numbers within text string! via135 Excel Worksheet Functions 6 May 5th 06 06:08 AM
EXTRACT NUMBERS FROM TEXT STRING fiber_doc Excel Worksheet Functions 4 November 28th 05 06:40 PM
Incorporating Numbers into a text string Gary T Excel Worksheet Functions 2 July 26th 05 11:09 AM


All times are GMT +1. The time now is 02:54 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"