Solved

Excel Marco to change data to line row with date

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

: Microsoft Office Collaborate for free and online versions of Microsoft  Word, Excel, Powerpoint, OneNote, Onedrive , Email, Calendar etc. In short we can say that Microsoft office is a suite of servers, applications and services developed by  Micr…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

635 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