PC Review


Reply
Thread Tools Rate Thread

Calculating Date Ranges and Weight Totals

 
 
=?Utf-8?B?Sm9obiBXYWxrZXI=?=
Guest
Posts: n/a
 
      12th Oct 2006
Hi,

I need to write a fairly complicated macro and am hopeful that someone may
have had to do something similar and could give me advice.
The spreadsheet itself is simple, with these columns:

STATE, VENDOR, DATE1, DATE2, WEIGHT.

When the macro is run we would like the user to be able to enter a starting
and ending date. The macro will then go through every row in the worksheet
and figure out how many days between DATE1 and DATE2 fall within the
user-entered starting/ending date range, and will write the results into
column F for each row.

We also need to group the worksheet by STATE/VENDOR, and give a WEIGHT total
at each group break.

Is all this possible to do with a macro? If anyone has any sample code that
would be greatly appreciated.

Thanks in advance,
John

 
Reply With Quote
 
 
 
 
Bill Kuunders
Guest
Posts: n/a
 
      12th Oct 2006
It needn't be a complicated macro.
The number of days is found by subtracting C from D

To get subtotals you could look at <Data><Subtotals>
Where you can get subtotals per group.

use
at each change in ..........state
use function ..................sum
add subtotal to .............weight

If you do the state subtotals first and then the vendor subtotals AND NOT
thick the option to replace current subtotals.....
You should get what you want.

Greetings from NZ
Bill K

"John Walker" <(E-Mail Removed)> wrote in message
news:9314E550-0799-4D64-A2D3-(E-Mail Removed)...
> Hi,
>
> I need to write a fairly complicated macro and am hopeful that someone may
> have had to do something similar and could give me advice.
> The spreadsheet itself is simple, with these columns:
>
> STATE, VENDOR, DATE1, DATE2, WEIGHT.
>
> When the macro is run we would like the user to be able to enter a
> starting
> and ending date. The macro will then go through every row in the
> worksheet
> and figure out how many days between DATE1 and DATE2 fall within the
> user-entered starting/ending date range, and will write the results into
> column F for each row.
>
> We also need to group the worksheet by STATE/VENDOR, and give a WEIGHT
> total
> at each group break.
>
> Is all this possible to do with a macro? If anyone has any sample code
> that
> would be greatly appreciated.
>
> Thanks in advance,
> John
>



 
Reply With Quote
 
somethinglikeant
Guest
Posts: n/a
 
      12th Oct 2006
Hi John,

Just working on this now for you.
I had difficulty coding the 4 cases for the date periods.

This code fulfils your first request

:=====
Sub Macro1()

a = InputBox("Enter Start Date")
a = DateValue(a)

b = InputBox("Enter End Date")
b = DateValue(b)

[F2].Select

Do Until IsEmpty(ActiveCell.Offset(0, -1))
x = DateValue(ActiveCell.Offset(0, -3).Value)
y = DateValue(ActiveCell.Offset(0, -2).Value)

If Application.WorksheetFunction.Max(x, y) < _
Application.WorksheetFunction.Max(a, b) Then ActiveCell.Value =
y - a

If Application.WorksheetFunction.Max(x, y) > _
Application.WorksheetFunction.Max(a, b) Then ActiveCell.Value =
b - x

If Application.WorksheetFunction.Max(x, y) < _
Application.WorksheetFunction.Min(a, b) Then ActiveCell.Value =
0

If Application.WorksheetFunction.Min(x, y) > _
Application.WorksheetFunction.Max(a, b) Then ActiveCell.Value =
0
ActiveCell.Offset(1, 0).Select
Loop

End Sub

:==================

i'll work on the next bit

http://www.excel-ant.co.uk

 
Reply With Quote
 
somethinglikeant
Guest
Posts: n/a
 
      12th Oct 2006
Sub Macro1()

a = InputBox("Enter Start Date")
a = DateValue(a)

b = InputBox("Enter End Date")
b = DateValue(b)

[F2].Select

Do Until IsEmpty(ActiveCell.Offset(0, -1))
x = DateValue(ActiveCell.Offset(0, -3).Value)
y = DateValue(ActiveCell.Offset(0, -2).Value)

If Application.WorksheetFunction.Max(x, y) < _
Application.WorksheetFunction.Max(a, b) Then ActiveCell.Value =
y - a

If Application.WorksheetFunction.Max(x, y) > _
Application.WorksheetFunction.Max(a, b) Then ActiveCell.Value =
b - x

If Application.WorksheetFunction.Max(x, y) < _
Application.WorksheetFunction.Min(a, b) Then ActiveCell.Value =
0

If Application.WorksheetFunction.Min(x, y) > _
Application.WorksheetFunction.Max(a, b) Then ActiveCell.Value =
0
ActiveCell.Offset(1, 0).Select
Loop

[A1].Select
Selection.Sort Key1:=Range("B2"), Order1:=xlAscending, Header:=xlGuess,
_
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

[B2].Select

Do Until IsEmpty(ActiveCell)
x = ActiveCell.Value
y = 0
Do Until ActiveCell.Value <> x
y = y + ActiveCell.Offset(0, 3).Value
ActiveCell.Offset(1, 0).Select
Loop
ActiveCell.EntireRow.Insert
ActiveCell.Offset(0, 3).Value = y
ActiveCell.Offset(0, 3).Font.Bold = True
ActiveCell.Offset(1, 0).Select
Loop

End Sub

http://www.excel-ant.co.uk

 
Reply With Quote
 
=?Utf-8?B?Sm9obiBXYWxrZXI=?=
Guest
Posts: n/a
 
      12th Oct 2006
Great! Thank you. I will try implementing this code into my sheet right now.
Thanks, John

"somethinglikeant" wrote:

> Hi John,
>
> Just working on this now for you.
> I had difficulty coding the 4 cases for the date periods.
>
> This code fulfils your first request
>
> :=====
> Sub Macro1()
>
> a = InputBox("Enter Start Date")
> a = DateValue(a)
>
> b = InputBox("Enter End Date")
> b = DateValue(b)
>
> [F2].Select
>
> Do Until IsEmpty(ActiveCell.Offset(0, -1))
> x = DateValue(ActiveCell.Offset(0, -3).Value)
> y = DateValue(ActiveCell.Offset(0, -2).Value)
>
> If Application.WorksheetFunction.Max(x, y) < _
> Application.WorksheetFunction.Max(a, b) Then ActiveCell.Value =
> y - a
>
> If Application.WorksheetFunction.Max(x, y) > _
> Application.WorksheetFunction.Max(a, b) Then ActiveCell.Value =
> b - x
>
> If Application.WorksheetFunction.Max(x, y) < _
> Application.WorksheetFunction.Min(a, b) Then ActiveCell.Value =
> 0
>
> If Application.WorksheetFunction.Min(x, y) > _
> Application.WorksheetFunction.Max(a, b) Then ActiveCell.Value =
> 0
> ActiveCell.Offset(1, 0).Select
> Loop
>
> End Sub
>
> :==================
>
> i'll work on the next bit
>
> http://www.excel-ant.co.uk
>
>

 
Reply With Quote
 
=?Utf-8?B?Sm9obiBXYWxrZXI=?=
Guest
Posts: n/a
 
      12th Oct 2006
Hey it works! Great, thank you. I may need to do some minor tweaking
because it doesn't seem to grouping properly, but this is exactly what i
needed.
Thanks!
John

"somethinglikeant" wrote:

> Sub Macro1()
>
> a = InputBox("Enter Start Date")
> a = DateValue(a)
>
> b = InputBox("Enter End Date")
> b = DateValue(b)
>
> [F2].Select
>
> Do Until IsEmpty(ActiveCell.Offset(0, -1))
> x = DateValue(ActiveCell.Offset(0, -3).Value)
> y = DateValue(ActiveCell.Offset(0, -2).Value)
>
> If Application.WorksheetFunction.Max(x, y) < _
> Application.WorksheetFunction.Max(a, b) Then ActiveCell.Value =
> y - a
>
> If Application.WorksheetFunction.Max(x, y) > _
> Application.WorksheetFunction.Max(a, b) Then ActiveCell.Value =
> b - x
>
> If Application.WorksheetFunction.Max(x, y) < _
> Application.WorksheetFunction.Min(a, b) Then ActiveCell.Value =
> 0
>
> If Application.WorksheetFunction.Min(x, y) > _
> Application.WorksheetFunction.Max(a, b) Then ActiveCell.Value =
> 0
> ActiveCell.Offset(1, 0).Select
> Loop
>
> [A1].Select
> Selection.Sort Key1:=Range("B2"), Order1:=xlAscending, Header:=xlGuess,
> _
> OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
>
> [B2].Select
>
> Do Until IsEmpty(ActiveCell)
> x = ActiveCell.Value
> y = 0
> Do Until ActiveCell.Value <> x
> y = y + ActiveCell.Offset(0, 3).Value
> ActiveCell.Offset(1, 0).Select
> Loop
> ActiveCell.EntireRow.Insert
> ActiveCell.Offset(0, 3).Value = y
> ActiveCell.Offset(0, 3).Font.Bold = True
> ActiveCell.Offset(1, 0).Select
> Loop
>
> End Sub
>
> http://www.excel-ant.co.uk
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding Totals from two diff date ranges Dave K. Microsoft Access Forms 5 18th Sep 2009 06:50 PM
Calculating within Date Ranges MB Microsoft Excel Misc 3 9th Oct 2008 10:44 PM
Calculating Date Ranges =?Utf-8?B?Q2hyaXMgSG9mZXI=?= Microsoft Excel Worksheet Functions 4 24th Apr 2007 09:46 PM
Calculating date ranges =?Utf-8?B?VGFueWE=?= Microsoft Excel Worksheet Functions 9 28th Sep 2004 09:21 PM
Calculating date ranges Adam Microsoft Access Form Coding 2 18th Jul 2003 12:59 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:47 AM.