Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Newb VBA sub...

I'm looking to create a sub that will pull information from a
different sheet ("sheet2"), and sum it into a cell on "sheet1". Sheet
2 contains a list of product #'s in column B, and I want it to
basically act as a function "SUMIF", where it sums all the costs
(column F) associated with that product if that correct product code
is in column B.

I know I can do this via a function (SUMIF), but I'm trying to learn
VBA, and thought I might as well start now.

Thanks!

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 48
Default Newb VBA sub...

Hi,

For what you are trying to do you'll probably find it is simplest to use the
same function but within VBA.

Say you have your products listed in cells A1-A8, the costs are in B1-B8,
and you want to use SumIf to some the costs for products that match a product
listed in cell E1. You want the sum to be in cell D1.

In the spreadsheet you would type directly in cell D1:
=SUMIF(A1:A8,E1,B1:B8)

Whereas in VBA you could type:
Cells(1, 4) = Application.WorksheetFunction.SumIf(Range(Cells(1, 1),
Cells(8, 1)), Cells(1, 5), Range(Cells(1, 2), Cells(8, 2)))

You can place Worksheets("Sheet1") infront of each Cells() reference if you
wish to use ranges or results that are on different (or specific) sheets.

It is often simpler and faster to use a worksheet function in this way,
where it is available.

An alternative way to do this would be to create a variable to store your
sum, go to check each cell one by one in your products range, and sum the
value on that row in the costs range, and finally placing the result in your
destination cell:

Public Sub My_SumIf()
Dim rngProducts As Range
Dim rngCells As Range
Dim intCostsColumn As Integer
Dim dblSumIfTotal As Double
Dim strSearchProduct As String
'SET PRODUCT RANGE
Set rngProducts = Worksheets("Sheet1").Range(Cells(1, 1), Cells(8, 1))
'SET COST COLUMN
intCostsColumn = 2
'INITIALISE TOTAL (NOT ALWAYS NECESSARY, BUT CAN BE GOOD PRACTICE)
dblSumIfTotal = 0
'GET THE PRODUCT I WANT TO SUM FOR
strSearchProduct = Worksheets("Sheet1").Cells(1, 5)
'LOOP THROUGH ALL CELLS IN MY PRODUCT RANGE
For Each rngCells In rngProducts
'IS THE PRODUCT TO BE SUMMED
If rngCells.Value = strSearchProduct Then
'CHECK FOR NUMERIC VALUE IN COSTS
If IsNumeric(Worksheets("Sheet1").Cells(rngCells.Row, intCostsColumn)) Then
'ADD TO RUNNING TOTAL
dblSumIfTotal = dblSumIfTotal + Worksheets("Sheet1").Cells(rngCells.Row,
intCostsColumn)
Else
MsgBox ("Non numeric found in costs")
End If
End If
Next
'PLACE RESULT IN MY RESULT CELL
Worksheets("Sheet1").Cells(1, 4) = dblSumIfTotal
End Sub

So there's some code to get you started anyway, but as I say, the worksheet
functions will generally be quicker than trying to rewrite them in VBA code.

Hope this helps,

Sean.


--
(please remember to click yes if replies you receive are helpful to you)


" wrote:

I'm looking to create a sub that will pull information from a
different sheet ("sheet2"), and sum it into a cell on "sheet1". Sheet
2 contains a list of product #'s in column B, and I want it to
basically act as a function "SUMIF", where it sums all the costs
(column F) associated with that product if that correct product code
is in column B.

I know I can do this via a function (SUMIF), but I'm trying to learn
VBA, and thought I might as well start now.

Thanks!


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Newb VBA sub...

Anyhelp would be greatly appreciated. Thanks.


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Newb VBA sub...


dblSumIfTotal = dblSumIfTotal + Worksheets("Sheet1").Cells(rngCells.Row,
intCostsColumn)


Everything seems to work, except the statement above. Any ideas?



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
Newb Q: XLL and XLA? Maury Markowitz Excel Programming 0 October 16th 06 05:25 PM
Help a newb.... gibson00 Excel Discussion (Misc queries) 6 July 11th 06 02:21 PM
Can someone please tell this newb what he's doing wrong? SanFranGuy06 Excel Programming 1 May 16th 06 12:05 AM
Complete newb, I know! grahamhurlburt Excel Programming 2 December 23rd 05 09:15 PM
i know this has to be so easy -newb chris_ Excel Discussion (Misc queries) 6 July 11th 05 09:57 PM


All times are GMT +1. The time now is 10:38 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"