Using variables within a predefined function

RhodeRider

New Member
Joined
May 16, 2011
Messages
6
There is probably a more elegant way to do this, but what I am trying to do is take counts of bicycles in 15-minute intervals from one sheet and sum them into hourly totals and place them onto another sheet. Within the while-loop, the first two lines take the date and the hour (for instance, hour is listed as 0:00, 0:15, 0:30 etc. so I take every four so that my final column reads 0:00, 1:00 etc.). They work properly. The third line returns a #name error. When I click on the cells in the third column of the "hour" sheet, I see this: =SUM('2010 15min'!Cells(8,18):Cells(11,18)). So it seems to be reading c and b properly. Any ideas on the #name error?

Thanks in advance. Let me know if my description isn't clear enough.

Code:
Sub Aggregate()

'variables
Dim c As Integer
Dim v As Integer
Dim b As Integer

v = 2
c = 8
b = 11

While IsEmpty(Worksheets("2010 15min").Cells(c, 18)) = False

   Worksheets("Hour").Cells(v, 1) = Worksheets("2010 15min").Cells((c), 1)
   Worksheets("Hour").Cells(v, 2) = Worksheets("2010 15min").Cells((c), 2)
   [COLOR=Red]Worksheets("Hour").Cells(v, 3).Formula = "=SUM('2010 15min'!Cells(" & c & ",18):Cells(" & b & ",18))"[/COLOR]

   c = c + 4
   b = b + 4
   v = v + 1
Wend

End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
"I see this: =SUM('2010 15min'!Cells(8,18):Cells(11,18)). "

Cells() is vba and won't be recognized in a worksheet function/formula. Try stringing together the formula string using the range's .Address
 
Upvote 0
Try

Code:
Worksheets("Hour").Cells(v, 3).Formula = "=SUM('2010 15min'!" & Range(Cells(c, 18), Cells(b, 18)).Address & ")"
 
Upvote 0
Ah GTO that makes sense. ANd VoG, thanks, your code worked perfectly. I have a tough time with syntax in VBA. Do you guys happen to know of a good resource? I usually just google my problem and read forums until I've found somethin.
 
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,904
Members
452,948
Latest member
Dupuhini

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top