Solved

Excel - converting column and row layout

Posted on 2011-09-19
12
306 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:teylyn
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:teylyn
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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:teylyn
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:teylyn
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

A2 = A1 That kind of cell reference is relative.  If you copy it from A2 to B2, then B2 will get this: B2 = B1 That's all fine and good, but if you then insert a new row above row 2, you'll find: A3 = A1 B3 = B1 This is intentional. …
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

744 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

12 Experts available now in Live!

Get 1:1 Help Now