Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 586
Default Find Text the Apply a SubTotal Next to Text Address

I have a order sheet that has a list of products and there prices. The
prices are located in Column F. I want to beable to Sum all the products in
a Cell that is next to a cell the words Sub Total in it. So I wrote my code
to search Col. E for "Sub Total" and return the address. But I am getting an
error on my variable LastRow, why? Is there an easier way of doing the
subtotal?

For example, if "Sub Total" is in Cells(20,5) then the pricing sum should be
in Cells(20,6). Note: Below the Sub Total Cell, I have Shipping Costs,
Freight, and then TOTAL.

Sub SubTotal()

Dim LastRow As Long
Dim mySubTotal As Range

Set mySubTotal = Sheets("QUOTE").Columns("E:E").Find(What:="Sub Total", _
After:=Cells(6, 5), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)

LastRow = Sheets("QUOTE").Range(mySubTotal.Address).Offset(-1, 0).Row
<==Error

Sheets("QUOTE").Range(mySubTotal.Address).Offset(0 , 1).Value =
WorksheetFunction.Sum("F6:F" & LastRow - 1)

End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Find Text the Apply a SubTotal Next to Text Address

How about:

Option Explicit
Sub mySubtotal()

Dim LastRow As Long
Dim LastSubTotal As Range

With Sheets("Quote")
With .Range("e:e")
Set LastSubTotal = .Cells.Find(What:="Sub Total", _
After:=.Cells(1), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False, _
SearchFormat:=False)
End With

If LastSubTotal Is Nothing Then
MsgBox "Something bad happened?"
'What should happen here
Else
LastRow = LastSubTotal.Row - 1
'did you mean to subtract 1 again???
With LastSubTotal.Offset(0, 1)
.Formula = "=Sum(F6:F" & LastRow - 1 & ")"
'.Value = .Value 'formula or value???
End With
End If
End With
End Sub

It looks for the last occurrence by starting with the topmost cell (.cells(1))
and then looking for the previous occurrence.

RyanH wrote:

I have a order sheet that has a list of products and there prices. The
prices are located in Column F. I want to beable to Sum all the products in
a Cell that is next to a cell the words Sub Total in it. So I wrote my code
to search Col. E for "Sub Total" and return the address. But I am getting an
error on my variable LastRow, why? Is there an easier way of doing the
subtotal?

For example, if "Sub Total" is in Cells(20,5) then the pricing sum should be
in Cells(20,6). Note: Below the Sub Total Cell, I have Shipping Costs,
Freight, and then TOTAL.

Sub SubTotal()

Dim LastRow As Long
Dim mySubTotal As Range

Set mySubTotal = Sheets("QUOTE").Columns("E:E").Find(What:="Sub Total", _
After:=Cells(6, 5), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)

LastRow = Sheets("QUOTE").Range(mySubTotal.Address).Offset(-1, 0).Row
<==Error

Sheets("QUOTE").Range(mySubTotal.Address).Offset(0 , 1).Value =
WorksheetFunction.Sum("F6:F" & LastRow - 1)

End Sub


--

Dave Peterson
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
Find cell address of last cell in a column with text mike in texas Excel Worksheet Functions 8 October 4th 08 05:41 AM
Search, find or lookup defined text in text string zzxxcc Excel Worksheet Functions 9 September 6th 07 09:37 PM
Find text String and select cell address where it is found? JCIrish Excel Programming 5 April 16th 06 02:38 PM
to find the text and to know that particular cell address cheenu Excel Programming 1 March 8th 06 03:21 PM
open some txt files ,find text , copy the text before that to a single cell gus Excel Programming 2 July 11th 05 05:40 PM


All times are GMT +1. The time now is 11:12 AM.

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"