Solved

Excel Marco to change data to line row with date

Posted on 2014-09-17
2
356 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

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…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
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 how to use longer labels with horizontal bar charts instead of the vertical column chart.

742 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