ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   how to combine each record into a range? (https://www.excelbanter.com/excel-discussion-misc-queries/259671-how-combine-each-record-into-range.html)

pemt

how to combine each record into a range?
 
how to combine each record into a range?
Table1
Day Value
1 0
2 0
3 0
4 1
5 1
6 1
7 0
8 0
9 0
10 0
11 1
12 1
13 0
14 1
15 0
16 0
17 1
18 1
19 0
20 1
.. .
.. .
.. .
1000 1

how to covert the above table into:
DayStart DayEnd Value
1 3 0
4 6 1
7 10 0
11 12 1
13 13 0
14 14 1
15 16 0
17 18 1
19 19 0
....

thanks,

pemt




Chip Pearson

how to combine each record into a range?
 
Try some code like the following.


Sub AAA()

Dim R As Range
Dim Dest As Range
Dim CurrVal As Long
Dim Upper As Long
Dim Lower As Long
Dim LastRow As Long

Set Dest = Sheets(2).Range("A1") '<<<< CHANGE
Set R = Worksheets(1).Range("A1") '<<< CHANGE
CurrVal = R(1, 2).Value
Upper = R(1, 1).Value
Lower = R(1, 1).Value

For Each R In Worksheets(1).Range("A1:A26") '<<< CHANGE
If R(1, 2).Value < CurrVal Then
Dest(1, 1).Value = Lower
Dest(1, 2).Value = Upper
Dest(1, 3).Value = CurrVal
Lower = R(1, 1).Value
CurrVal = R(1, 2).Value
Set Dest = Dest(2, 1)
End If
Upper = R(1, 1).Value
Next R

End Sub

Change the values marked with <<<< to the appropriate values. R should
be set to the first cell that is to be examined. The For Each loop
should be set to the entire range that will be examined. Dest should
be set to the destination of the new records.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional,
Excel, 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com





On Tue, 23 Mar 2010 11:13:01 -0700, pemt
wrote:

how to combine each record into a range?
Table1
Day Value
1 0
2 0
3 0
4 1
5 1
6 1
7 0
8 0
9 0
10 0
11 1
12 1
13 0
14 1
15 0
16 0
17 1
18 1
19 0
20 1
. .
. .
. .
1000 1

how to covert the above table into:
DayStart DayEnd Value
1 3 0
4 6 1
7 10 0
11 12 1
13 13 0
14 14 1
15 16 0
17 18 1
19 19 0
...

thanks,

pemt



pemt

how to combine each record into a range?
 
Hi Chip,
Thanks a lot!
pemt

"Chip Pearson" wrote:

Try some code like the following.


Sub AAA()

Dim R As Range
Dim Dest As Range
Dim CurrVal As Long
Dim Upper As Long
Dim Lower As Long
Dim LastRow As Long

Set Dest = Sheets(2).Range("A1") '<<<< CHANGE
Set R = Worksheets(1).Range("A1") '<<< CHANGE
CurrVal = R(1, 2).Value
Upper = R(1, 1).Value
Lower = R(1, 1).Value

For Each R In Worksheets(1).Range("A1:A26") '<<< CHANGE
If R(1, 2).Value < CurrVal Then
Dest(1, 1).Value = Lower
Dest(1, 2).Value = Upper
Dest(1, 3).Value = CurrVal
Lower = R(1, 1).Value
CurrVal = R(1, 2).Value
Set Dest = Dest(2, 1)
End If
Upper = R(1, 1).Value
Next R

End Sub

Change the values marked with <<<< to the appropriate values. R should
be set to the first cell that is to be examined. The For Each loop
should be set to the entire range that will be examined. Dest should
be set to the destination of the new records.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional,
Excel, 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com





On Tue, 23 Mar 2010 11:13:01 -0700, pemt
wrote:

how to combine each record into a range?
Table1
Day Value
1 0
2 0
3 0
4 1
5 1
6 1
7 0
8 0
9 0
10 0
11 1
12 1
13 0
14 1
15 0
16 0
17 1
18 1
19 0
20 1
. .
. .
. .
1000 1

how to covert the above table into:
DayStart DayEnd Value
1 3 0
4 6 1
7 10 0
11 12 1
13 13 0
14 14 1
15 16 0
17 18 1
19 19 0
...

thanks,

pemt


.



All times are GMT +1. The time now is 04:59 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com