Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 38
Default Usage of SUMIF formula in the VBA code

I am trying to use Excel "SUMIF" function in VBA. I was able to do it without
problem by inserting the formula into the cell ( Range(Cells(21, 2), Cells(21
+ RowNumber, 2 + ColumnNumber)).Formula = "=sumIf($a$1:$A$16, $a21,
b$1:b$16)").

But, when I tried to do the same thing using "WorksheetFunction." (
Range(Cells(35, 2), Cells(35 + RowNumber, 2 + ColumnNumber)) =
WorksheetFunction.SumIf(Range(Cells(1, 1), Cells(16, 1)), Range(Cells(21 +
RowNumber, 1), Cells(21 + RowNumber, 1)), Range(Cells(1, 2 + ColumnNumber),
Cells(16, 2 + ColumnNumber)))
) I do not get the same results. It seems to me they are equivalent and
should provide the same results. But, the results says I am wrong and I was
wondering what I am missing here? Thanks.

Here is the entire code:

Sub test()
For RowNumber = 0 To 2
For ColumnNumber = 0 To 3
Range(Cells(21, 2), Cells(21 + RowNumber, 2 + ColumnNumber)).Formula
= "=sumIf($a$1:$A$16, $a21, b$1:b$16)"
Range(Cells(35, 2), Cells(35 + RowNumber, 2 + ColumnNumber)) =
WorksheetFunction.SumIf(Range(Cells(1, 1), Cells(16, 1)), Range(Cells(21 +
RowNumber, 1), Cells(21 + RowNumber, 1)), Range(Cells(1, 2 + ColumnNumber),
Cells(16, 2 + ColumnNumber)))
Next
Next
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,339
Default Usage of SUMIF formula in the VBA code

Try

Cells(35 + RowNumber, 2 + ColumnNumber) = _
WorksheetFunction.SumIf(Range(Cells(1, 1), Cells(16, 1)), Cells(21 + _
RowNumber, 1), Range(Cells(1, 2 + ColumnNumber), Cells(16, 2 + ColumnNumber)))

HTH

"GreenInIowa" wrote:

I am trying to use Excel "SUMIF" function in VBA. I was able to do it without
problem by inserting the formula into the cell ( Range(Cells(21, 2), Cells(21
+ RowNumber, 2 + ColumnNumber)).Formula = "=sumIf($a$1:$A$16, $a21,
b$1:b$16)").

But, when I tried to do the same thing using "WorksheetFunction." (
Range(Cells(35, 2), Cells(35 + RowNumber, 2 + ColumnNumber)) =
WorksheetFunction.SumIf(Range(Cells(1, 1), Cells(16, 1)), Range(Cells(21 +
RowNumber, 1), Cells(21 + RowNumber, 1)), Range(Cells(1, 2 + ColumnNumber),
Cells(16, 2 + ColumnNumber)))
) I do not get the same results. It seems to me they are equivalent and
should provide the same results. But, the results says I am wrong and I was
wondering what I am missing here? Thanks.

Here is the entire code:

Sub test()
For RowNumber = 0 To 2
For ColumnNumber = 0 To 3
Range(Cells(21, 2), Cells(21 + RowNumber, 2 + ColumnNumber)).Formula
= "=sumIf($a$1:$A$16, $a21, b$1:b$16)"
Range(Cells(35, 2), Cells(35 + RowNumber, 2 + ColumnNumber)) =
WorksheetFunction.SumIf(Range(Cells(1, 1), Cells(16, 1)), Range(Cells(21 +
RowNumber, 1), Cells(21 + RowNumber, 1)), Range(Cells(1, 2 + ColumnNumber),
Cells(16, 2 + ColumnNumber)))
Next
Next
End Sub

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 38
Default Usage of SUMIF formula in the VBA code

Thanks, Toppers.

I could not believe this was this simple!

"Toppers" wrote:

Try

Cells(35 + RowNumber, 2 + ColumnNumber) = _
WorksheetFunction.SumIf(Range(Cells(1, 1), Cells(16, 1)), Cells(21 + _
RowNumber, 1), Range(Cells(1, 2 + ColumnNumber), Cells(16, 2 + ColumnNumber)))

HTH

"GreenInIowa" wrote:

I am trying to use Excel "SUMIF" function in VBA. I was able to do it without
problem by inserting the formula into the cell ( Range(Cells(21, 2), Cells(21
+ RowNumber, 2 + ColumnNumber)).Formula = "=sumIf($a$1:$A$16, $a21,
b$1:b$16)").

But, when I tried to do the same thing using "WorksheetFunction." (
Range(Cells(35, 2), Cells(35 + RowNumber, 2 + ColumnNumber)) =
WorksheetFunction.SumIf(Range(Cells(1, 1), Cells(16, 1)), Range(Cells(21 +
RowNumber, 1), Cells(21 + RowNumber, 1)), Range(Cells(1, 2 + ColumnNumber),
Cells(16, 2 + ColumnNumber)))
) I do not get the same results. It seems to me they are equivalent and
should provide the same results. But, the results says I am wrong and I was
wondering what I am missing here? Thanks.

Here is the entire code:

Sub test()
For RowNumber = 0 To 2
For ColumnNumber = 0 To 3
Range(Cells(21, 2), Cells(21 + RowNumber, 2 + ColumnNumber)).Formula
= "=sumIf($a$1:$A$16, $a21, b$1:b$16)"
Range(Cells(35, 2), Cells(35 + RowNumber, 2 + ColumnNumber)) =
WorksheetFunction.SumIf(Range(Cells(1, 1), Cells(16, 1)), Range(Cells(21 +
RowNumber, 1), Cells(21 + RowNumber, 1)), Range(Cells(1, 2 + ColumnNumber),
Cells(16, 2 + ColumnNumber)))
Next
Next
End Sub

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
Question about repeated usage of a formula G.R. Toro Excel Discussion (Misc queries) 5 March 14th 09 08:57 AM
Formula for kanban cycle usage Angie Excel Discussion (Misc queries) 5 September 16th 08 08:42 PM
Week Day formula usage???? Ajay Excel Discussion (Misc queries) 9 March 17th 07 01:58 AM
change SUMIF formula into a vb code ASU Excel Discussion (Misc queries) 1 September 6th 06 07:08 PM
Usage of sumif Charles Excel Discussion (Misc queries) 2 March 8th 05 05:27 AM


All times are GMT +1. The time now is 05:21 PM.

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

About Us

"It's about Microsoft Excel"