Solved

Excel Marco to change data to line row with date

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

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

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…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

920 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

18 Experts available now in Live!

Get 1:1 Help Now