Solved

Excel - converting column and row layout

Posted on 2011-09-19
12
307 Views
Last Modified: 2012-05-12
Hi,

I need to convert the attached spreadsheet (tab called "hourly_coweeta_rain_RR06") into the following format and place the results in the tab called "hourly rain".

Column1:  Date (MM/DD/YYYY  HH:MM)  Column 2:  rainfall
Example:   01/01/2007 1:00 AM       0.0
                 01/01/2007  2:00 AM      0.0
                 etc
                 01/01/2007  9:00 AM      0.02
                 etc for all hours, all days in the spreadsheet

As you'll notice, the column headings are currently:  YEAR, NONTH, DAY, 1, 2, 3, 4, ....etc...24 ( corresponding to the 24 hour day).

Thanks! hourly-coweeta-rain.xlsx
0
Comment
Question by:trcamp
  • 5
  • 4
  • 3
12 Comments
 
LVL 50

Expert Comment

by:Ingeborg Hawighorst
ID: 36563957
Hello,

there are gaps in the dates. How do you want to reflect this in the target sheet? Also, I assume that hour 1 means the time between midnight and 1 am, is that correct?

In the attached file, I've added a column to the source data to create a full date for each line. Then used an Index/Match formula on the second sheet.

The second sheet starts with the first date of the source data, incrementing in hourly steps. For the days that are not found in the source list, there will be blank cells.

There are other ways.

cheers, teylyn
hourly-coweeta-rain.xlsx
0
 
LVL 50

Expert Comment

by:Ingeborg Hawighorst
ID: 36563996
One other way is this:

You can use a reverse pivot table to create a condensed version of the data with only the dates of the source table.

Add a new column D with the formula

=date(A2,B2,C2)

Label the column "Date"
Change the column header for the hours from 1 to 0:00 am, 2 to 1:00 am, 3 to 2:00 etc., making sure these are real time values.

Hit Alt-D-P to open the Pivot table wizard
Click "Multiple consolidation ranges" and hit Next
Click "I will create the page fields" and Next
select the range hourly_coweeta_rain_RR06!$D$1:$AB$432 and hit Next
Click Finish
On the new sheet with the Pivot table, drag the Row and the Column field out of the boxes at the bottom, rendering these empty.
In the pivot table you now see one label and one number.

Double click the number.

Bang.

There's your list on a new sheet. You can now delete the pivot table sheet. If required, you can add columns A and B of the new sheet to arrive at a single cell combination value for date and time.

see attached.

cheers, teylyn

hourly-coweeta-rain.xlsx
0
 
LVL 5

Expert Comment

by:slycoder
ID: 36564048
One thing I noticed is that you have 24th hour on each day - technically it would fall over to the next day

(the 24th hour of 1/1/2007 is actually the zero hour of 1/2/2007)

if this is not a major issue, this macro will work with your original spreadsheet:


Public Sub RainSummary()
    Dim myCell As Range
    Dim myRange As Range
    Dim myHour As Integer
    Dim myRow As Integer
   
    ' Position cursor in Sheet2
    Sheets(2).Select
    Range("A2").Select
    myRow = 0
   
    ' Set range
    Set myRange = Sheets(1).Range("C2:C500")
   
    ' Cycle through all dates
    For Each myCell In myRange
        If myCell.Value <> "" Then
   
            ' Loop for each of the 24 hours
            For myHour = 1 To 24
                Sheets(2).Range("A2").Offset(myRow, 0).Value = DateSerial(myCell.Offset(0, -2).Value, myCell.Offset(0, -1).Value, myCell.Value) + TimeSerial(myHour, 0, 0)
                Sheets(2).Range("A2").Offset(myRow, 0).NumberFormat = "mm/dd/yyyy hh:mm am/pm"
                Sheets(2).Range("A2").Offset(myRow, 1).Value = myCell.Offset(0, myHour).Value
                myRow = myRow + 1
            Next myHour
        End If
       
    Next myCell
End Sub
0
 

Author Comment

by:trcamp
ID: 36567157
Teylen,  Your version didn't work properly but I really appreciate your input.
SlyCoder,  I used your macro and everything worked except the following:  (Note that it puts a nonsense date in each of the 1 am slots.)   Please advise, and thanks in advance!

01/01/2007 01:00 AM      1/0/1900
01/01/2007 02:00 AM      0
01/01/2007 03:00 AM      0
01/01/2007 04:00 AM      0
01/01/2007 05:00 AM      0
01/01/2007 06:00 AM      0
01/01/2007 07:00 AM      0
01/01/2007 08:00 AM      0
01/01/2007 09:00 AM      0.02
01/01/2007 10:00 AM      0
01/01/2007 11:00 AM      0
01/01/2007 12:00 PM      0
01/01/2007 01:00 PM      0
01/01/2007 02:00 PM      0
01/01/2007 03:00 PM      0
01/01/2007 04:00 PM      0
01/01/2007 05:00 PM      0
01/01/2007 06:00 PM      0
01/01/2007 07:00 PM      0
01/01/2007 08:00 PM      0
01/01/2007 09:00 PM      0
01/01/2007 10:00 PM      0
01/01/2007 11:00 PM      0
01/02/2007 12:00 AM      0
01/04/2007 01:00 AM      1/0/1900
01/04/2007 02:00 AM      0
01/04/2007 03:00 AM      0
01/04/2007 04:00 AM      0


0
 
LVL 5

Expert Comment

by:slycoder
ID: 36568999
the cells somehow got formatted to a date.  This new code will clear the format in column B using:
                Sheets(2).Range("A2").Offset(myRow, 1).NumberFormat = ""


sorry for the delay, try this -



Public Sub RainSummary()
    Dim myCell As Range
    Dim myRange As Range
    Dim myHour As Integer
    Dim myRow As Integer
   
    ' Position cursor in Sheet2
    Sheets(2).Select
    Range("A2").Select
    myRow = 0
   
    ' Set range
    Set myRange = Sheets(1).Range("C2:C500")
   
    ' Cycle through all dates
    For Each myCell In myRange
        If myCell.Value <> "" Then
   
            ' Loop for each of the 24 hours
            For myHour = 1 To 24
                Sheets(2).Range("A2").Offset(myRow, 0).Value = DateSerial(myCell.Offset(0, -2).Value, myCell.Offset(0, -1).Value, myCell.Value) + TimeSerial(myHour, 0, 0)
                Sheets(2).Range("A2").Offset(myRow, 0).NumberFormat = "mm/dd/yyyy hh:mm am/pm"
                Sheets(2).Range("A2").Offset(myRow, 1).Value = myCell.Offset(0, myHour).Value
                Sheets(2).Range("A2").Offset(myRow, 1).NumberFormat = ""
                myRow = myRow + 1
            Next myHour
        End If
       
    Next myCell
End Sub
0
 

Author Comment

by:trcamp
ID: 36569058
I got a run-time error 1004:  application-defined or object-defined error.

Debugged and got this line in error:

Sheets(2).Range("A2").Offset(myRow, 0).Value = DateSerial(myCell.Offset(0, -2).Value, myCell.Offset(0, -1).Value, myCell.Value) + TimeSerial(myHour, 0, 0)

Please advise.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Accepted Solution

by:
slycoder earned 300 total points
ID: 36569171
I'll post the spreadsheet, please let me know if you still get an error.

Thanks

Copy-of-hourly-coweeta-rain-1.xlsm
0
 

Author Closing Comment

by:trcamp
ID: 36569194
Excellent!  Beautiful macro.

Thanks very much.

TC
0
 
LVL 50

Expert Comment

by:Ingeborg Hawighorst
ID: 36569916
>> Teylen,  Your version didn't work properly but I really appreciate your input.

glad you found a solution that works for you. I created the attachment with the exact steps I outlined in my post. It works, but the steps need to be followed exactly.

cheers, teylyn
0
 

Author Comment

by:trcamp
ID: 36569947
I apologize Teylyn.  I tried it and could not get it to work.  I would like to assign you some new points for your effort if that's possible - not sure how.  A sincere thanks for your help, and if you know how I can get you some points I'd like to.
0
 
LVL 50

Expert Comment

by:Ingeborg Hawighorst
ID: 36570519
no worries, trcamp. I appreciate the thought. Most important, though, is that you found a solution that works for you. The expert who provided that deserves the full points.

cheers, teylyn
0
 

Author Comment

by:trcamp
ID: 36573501
Sure nice to have folks like you on the EE boards.  Thanks again, Tetlyn.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now