Solved

Excel Marco to change data to line row with date

Posted on 2014-09-17
2
350 Views
Last Modified: 2014-09-19
Hello Experts Exchange
I have a Spreadsheet that has days of the week and a week number.

I would like a macro to write the data from the table into data I can import into SQL Server.

I have added a spreadsheet with how the data is on sheet1, and on sheet2 is how I would like the data to be changed to.

The date field should be worked out by taking the month date from cell E1, then get the week number, then get the day of the week.

Thank you for reading.

Regards

SQLSearcher
Worker-Hours.xls
0
Comment
Question by:SQLSearcher
2 Comments
 
LVL 40

Accepted Solution

by:
als315 earned 500 total points
ID: 40328822
Try this macro:
Sub prepare_data()
Dim SN1 As String, SN2 As String
Dim Wd As Integer, Dt As Date
Dim rn As Long, ro As Long, M As Integer
Dim ws1 As Worksheet, ws2 As Worksheet
SN1 = "Sheet1"
SN2 = "Sheet2"
Set ws1 = Worksheets(SN1)
Set ws2 = Worksheets(SN2)
rn = 5
ro = 2
ws2.Cells(ro, 1) = "Location"
ws2.Cells(ro, 2) = "Shift"
ws2.Cells(ro, 3) = "Worker"
ws2.Cells(ro, 4) = "Date"
ws2.Cells(ro, 5) = "Hours"
Dt = DateValue(ws1.Cells(1, 5))
M = Month(Dt)
Wd = Weekday(Dt, vbMonday)
With ws1
    Do While M = Month(Dt)
        If Wd < 6 Then  'Comment this IF if you can have workdays in saturday or sunday
            ws2.Cells(ro, 1) = .Cells(rn, 1)
            ws2.Cells(ro, 2) = .Cells(rn, 2)
            ws2.Cells(ro, 3) = .Cells(rn, 3)
            ws2.Cells(ro, 4) = Dt
            ws2.Cells(ro, 5) = .Cells(rn, Wd + 3)
            If ws2.Cells(ro, 5) = "" Then ws2.Cells(ro, 5) = 0
            ro = ro + 1
        End If
        Dt = DateAdd("d", 1, Dt)
        Wd = Weekday(Dt, vbMonday)
        If Wd = 7 Then rn = rn + 1
    Loop
End With
End Sub

Open in new window

It should work if you have ony one worker in worksheet
0
 

Author Closing Comment

by:SQLSearcher
ID: 40332217
Hello Experts Exchange
I was able to write macro for the code given to me by als315.  Thank you for your help.

Regards

SQLSearcher
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
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.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

830 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