Solved

Excel Marco to change data to line row with date

Posted on 2014-09-17
2
338 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 39

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

707 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

11 Experts available now in Live!

Get 1:1 Help Now