Find String in Cell then Paste Sum in Offset Cell

R

RyanH

I am new to VBA and having an issue. I have a worksheet that contains
products and there prices (Note: Prices located in Col. F). My users add
products and remove products via UserForm when quoting prices to customers.
This causes the Cell that contains the SubTotal to move up and down as
products are added and removed.

I have tried to write my code to search down Col. E and find the cell
location of the String "Sub Total". Once the String Address is located, I
want to use Offset to show the Sum of the product prices next to the String
Cell in Col. F. I am getting an error "Object variable or block variable not
set" indicated below. Why?

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

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

End Sub

Thanks in Advance,
Ryan
 
D

Dave Peterson

The replies to your earlier post didn't work?
I am new to VBA and having an issue. I have a worksheet that contains
products and there prices (Note: Prices located in Col. F). My users add
products and remove products via UserForm when quoting prices to customers.
This causes the Cell that contains the SubTotal to move up and down as
products are added and removed.

I have tried to write my code to search down Col. E and find the cell
location of the String "Sub Total". Once the String Address is located, I
want to use Offset to show the Sum of the product prices next to the String
Cell in Col. F. I am getting an error "Object variable or block variable not
set" indicated below. Why?

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

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

End Sub

Thanks in Advance,
Ryan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top