• Macro help for Timesheet (Excel 97)

    Author
    Topic
    #371070

    I am relatively new to Excel and am quite pleased with the progress I have made in adapting a timesheet given to me by a friend.

    I have one sheet called the template which I manually copy one by one and edit to create a complete file for a year. Each worksheet is a fortnight. This is then distributed to staff for their use. It seems to work fine.

    The problem is that it is reliant on me setting it up each year. Heavens! what happens if I should leave or get run over by a bus!!

    I would like to automate it through the use of a button on the template that triggers a macro so that they can create a new years version without me. I know very little about VB and Macros and would appreciate some help.

    In a tip I recently came across I found a macro that creates a workbook for each fortnight for a year. I thought hurray just what I need. Hmmm, it worked, creating Blank worksheets but I want to include the routine of copying the template.

    Here it is:-

    Sub YearWorkbook()
    Dim iWeek As Integer
    Dim sht As Variant
    Dim sTemp As String
    Dim dSDate As Date

    sTemp = InputBox(“Date for the first worksheet:”, “End of Week?”)
    dSDate = CDate(sTemp)

    Application.ScreenUpdating = False
    Worksheets.Add After:=Worksheets(Worksheets.Count), _
    Count:=(26 – Worksheets.Count)
    For Each sht In Worksheets
    sht.Name = Format(dSDate, “dd-mmm-yyyy”)
    dSDate = dSDate + 14
    Next sht
    Application.ScreenUpdating = True
    End Sub

    Any assistance would be greatly appreciated. “Please remember I am new to Excel and this group – so be patient with me”. P l e a s e ?

    Kerry
    Australia

    Viewing 0 reply threads
    Author
    Replies
    • #588822

      Assuming that your template sheet is named “Template”, and that it is in the same workbook as the macro and that the new worksheets will be created in this same workbook, I would do it like this:

      Public Sub CreateSheets()
      Dim I As Long
      Dim oNewSheet
      Dim datDate
      Dim strStartDate
          Application.ScreenUpdating = False
          strStartDate = InputBox("Date for the first worksheet:", "End of Week?")
          If strStartDate = "" Then
              Exit Sub
          End If
          datDate = CDate(strStartDate)
          If datDate = 0 Then
              MsgBox "Invalid date entered"
              Exit Sub
          End If
          For I = 1 To 26
              Set oNewSheet = Worksheets.Add(After:=Worksheets(Worksheets.Count))
              oNewSheet.Name = Format(datDate, "dd-mm-yyyy")
              Worksheets("Template").Cells.Copy
              oNewSheet.Paste Destination:=oNewSheet.Range("A1")
              datDate = datDate + 14
          Next I
          Worksheets(1).Activate
          Application.CutCopyMode = False
          Application.ScreenUpdating = True
      End Sub
      
      • #588972

        Lagare thank you for your assistance. This did exactly what I had asked, however, I had not given it a thought until I ran this Macro that each sheet has to have a new fortnight starting date at cell N3. This being the same date as the Worksheet Tab.

        Also a starting amount of hours (Positive or Negative) is carried forward from a sheet called the Master and form there on carried forward to the next worksheet. Is it possible to include these two actions into the same routine?

        I have attached the file in the hope that what I am describing will make more sense.

        I have also considered that perhaps the Master and Template could be one in the same.

        Opinions are welcome.

        • #588993

          this is a file I use for our flexitime system.
          The fomula (+ code) for cells c1 & b10 in period2/3 might help you towards what you want

          • #589006

            Thanks Bryan – I will take a look at your example.

            Kerry

        • #589018

          The attached workbook has the macro modified to insert the start date in cell N3 on each new sheet added.

          I’m not sure I understand your second question. From what cell on the master is the hours carried forward, and to what cell on the first new sheet is it carried forward to? From what cell on each inserted sheet is the hours carried forward from and to what cell on the next sheet? Should the hours be carried forward as a constant amount, or as a formula so that if the previous sheet is changed the change carries forward automatically?

          • #589200

            P15 on Master is carried forward to P26 On Template (example =Master!P15) – There after P26 on each sheet takes forward the total from P27 on the previous sheet. (example =’22 Feb – 7 Mar’!P27).

            This is quite combersome, but I know no better way to do this.

            Bryan – who sent the other response has an interesting formula on his spreadsheet, but unfortunately I am unable to interpret it.

            As I said earlier, I am no whizz with Excel. I just know what I want, but not always how to do it.

            Kerry

            • #589205

              See if this does what you want.

            • #589265

              This is just what I was looking for Lagare. Thankyou very much. I am very excited about finalising this project that has been bugging me for ages. I will be free to work on other tasks on my long list!

              I feel priviledged to have your help and I hope I am not stretching things too much by asking just another question?

              Can you tell me why, when the sheets are copied the new ones are not formatted the same as the template. That is, they show some (not all) gridlines and the zoom is at 100%?

              Also is it possible to protect each sheet in the Macro. If I leave the template protected before running the macro, the dates dont transpose correctly. So if I leave the Template unprotected, then so are all of the copies and they then need to be protected individually. I am reluctant to leave them unprotected as people always want to have a fiddle…

            • #589275

              Here is a new workbook that I think does what you asked. It assumes that the Template sheet is protected with a password of “MyPassword” (without the quotes) and it will protect all of the new sheets with that same password. You will need to change the password in three places in the macro if you want to use a different password.

            • #589438

              I am almost too embarrased to ask, but here goes.

              I have tried to nut this one out myself, but everything I tried didnt work.

              When I looked at the Print Preview I found the copies created are in portrait orientation and the header does not copy over.

              I almost hear the sigh – “oh what next??”

              Kerry

              Australia blush

            • #589460

              OK, the attached sheet should fix those problems. However, one warning. For some reason changing some of the pagesetup properties like page orientation are VERY slow. The macro with these changes runs MUCH longer than the previous versions.

            • #589830

              I just wanted to thank you for your help. I have the timesheet working now and although the Macro runs very slowly it does the job.

              The best part of it all is that I actually learnt something. I feel much more confident about having a go myself and am inspired to do more with Excel.

              Kind Regards
              Kerry

            • #595465

              Hi Lagare

              Bet you thought you were rid of me? I wonder if you can cast your mind back to this problem? I have returned to the original message so you can follow the thread.

              I am still having a problem with this BEAST. The version you returned to me seemed to still run into 4 pages when I printed it, so I thought I would be clever and record a macro of the print set up. Then copy it into the module. This does work, however it takes FOREVER to execute. Are you able to assist me in speeding it up?

              Kind Regards

              Kerry

            • #595482

              Unfortunately, changing the PageSetUp properties is extremely slow and, as far as I know, there is no good way around that. You can use an old Excel4 macro (which I do not know how to write), and I understand that this will run much faster. However, I really do not like doing that.

              I think that the real way to solve your problem is to approach this from a different angle. I think that what should be done is to set up a template workbook that would have all of the worksheets already created and formatted. Then use a worksheet change event on the starting date cell in the first sheet that would rename the sheets when a new starting date was entered. That will take a little while to do, but I will work on it and get back to you.

            • #595512

              Hi Legare,

              accessing the PAGE.SETUP Excel4 macro command is quite easy for a quick fix, as you embed something like this in your VBA …

              ExecuteExcel4Macro (“PAGE.SETUP(“”Nice header””,””Nice footer””)”)

              and this executes really fast compared to the VBA equivalent.

              You can make it more complex, but you might need to refer to the Excel4 macro function help ( can be downloaded from MS site, I believe )

              Here is an example of a more complex invocation of the call …

              header = Chr(34) & “My own header” & Chr(34) ‘ text of header ( in quotes )
              footer = Chr(34) & “My footer” & Chr(34) ‘ text of footer ( in quotes )
              leftm = 1.9 / 2.5 ‘ left margin ( converted to centimeter from inch )
              rightm = 1.9 / 2.5 ‘ right margin
              topm = 2.5 / 2.5 ‘ top margin
              botm = 2.5 / 2.5 ‘ bottom margin
              hdng = True ‘ row and column headings to be shown ( or FALSE if not )
              gridl = True ‘ sheet gridlines to be shown ( or FALSE if not )
              hcent = True ‘ horizontal centering to be done ( or FALSD if not )
              vcent = True ‘ vertical centering to be done ( or FALSE if not )
              orient = 2 ‘ print orientation, 1 = Portrait, 2 = Landscape
              paper_size = 9 ‘ paper size ( see Excel4 macro function help for full list )
              scaling = 100 ‘ scaling as a percent ( see help for other options )
              Pg_Num = 1 ‘ specifies the number of the first page ( see help also )
              Pg_order = 1 ‘ pagination, 1 = Top-to-bottom then right, 2 = Left-to-right then down
              bw_cells = False ‘ print as pure black and white ( or not if FALSE )
              quality = “” ‘ see help
              head_margin = 1 / 2.5 ‘ header margin from edge of page ( converted from inches )
              foot_margin = 1 / 2.5 ‘ footer maergin from edge of page ( converted from inches )
              notes = False ‘ print cell notes/comments with sheet ( or FALSE if not )
              Draft = False ‘ print in daft mode ( or FALSE if not )

              xl4command = “PAGE.SETUP(” & header & “,” & footer & “,” & _
              leftm & “,” & rightm & “,” & topm & “,” & botm & “,” & _
              hdng & “,” & gridl & “,” & hcent & “,” & vcent & “,” & _
              orient & “,” & paper_size & “,” & scaling & “,” & Pg_Num & _
              Pg_order & “,” & bw_cells & “,” & quality & “,” & _
              head_margin & “,” & foot_margin & “,” & notes & “,” & Draft & “)”

              ExecuteExcel4Macro (xl4command)

              Glenn.

            • #595643

              Hi Glenn

              Thanks for your suggestion. See my response to Legare.

              Kerry

            • #595596

              OK, here is my suggestion on how to do this. In the attached workbook, just enter a new start date in the start date cell on the first time sheet (not the master) and see if this gives you what you want. If it does, I would save this workbook as a template and put it in the template directory to use for creating new workbooks. I had to compress the workbook to make it small enough for the lounge to accept.

            • #595642

              I have my eggs in two baskets here. Not sure which way to go.

              I would like to try Glens idea, but not sure I actually understand how to implement what he is suggesting. Ideally I would like to use the version I have been working with because I know it all works except for the speed of the macro.

              I see your point with the way you have set it up and it has merit, however, some things dont seem to work now. For instance, when puting in a code for leave, this doesnt work now.

              Also in the previous version any changes to the template were taken forward to the fortnightly sheets created in the macro. This was handy for me as if I had to make and changes to the sheets, this was carried through. For example – I had someone say today that the gray shading was coming out very dark on their printer. I changed the shading to pale green on the template and it carried forward. In the future I may want to make some formulae amendments to my the original file before issuing it and I dont want to have to make changes to each and every worksheet.

              Hope this makes sense?

            • #595650

              Changing the formatting could be done simply by selecting all of the worksheets at one time and then changing the format on any sheet. That should change all of them. However, this is not going to work if the sheets are all protected. You would need a macro to unprotect all sheets, then you could make the changes, then another macro to protect all of the sheets again.

              Can you tell us where you put this code for leave, and what is supposed to happen that is not happening now?

            • #595673

              Still not the most desirable work around but I will give it a go.

              the Leave codes are part of a Lookup – Not a macro.

              =IF(D19=””,””,VLOOKUP(D19,Master!Leave,4))

              Kerry

            • #595735

              When I recreated the workbook, the Name Leave did not get created on the Master Sheet. That should now work in the attached workbook.

            • #595807

              See if this works for you… runs faster!

            • #596198

              Hi There

              clapping

              This works like a treat – Thankyou very much. Only one small problem the lookup formula in P20 is not following through on all other cells. Not sure why. Are you able to take a peek at this for me please.

              Then it will work perfectly.

              Kerry

              (PS This version looks a little different as I have had to modify the shading colour and put in an instruction sheet for users)

            • #596232

              See if the attached fixes that problem.

              It also looks like your tiemsheet requires that the starting date be a Friday. I also added a check to the macro to require that the starting date entered be a Friday. Is that what you want, or would you rather change the sheet to allow starting on any day of the week?

            • #596255

              Lagare you are truly fantastic! Not only are you very clever, but also extremely patient and obliging.

              It seems to do everything I can think of now. I will put it to the test in the real world of work this week.

              Your help has been nuch appreciated

              Have a great weekend

              Kerry
              wink

            • #597331

              HELP!!

              I tested and tested this darn thing and guess what! A calculation problem! Naturally I am a little stressed at the moment, as I have about 50 people out there trying to get this thing to work.

              I have looked and looked and it appears to this adrenalin filled brain that the formula in Row 22 says add all cells total cells except 21 if the code TOIL or LWOP are present. This doesnt seem to be happening and I dont know WHY?

              Please help before the phones start ringing?

              Kerry

            • #597338

              I think that the formual that you want is:

              =IF(OR(C20="TOIL",C20="LWOP"), C9+C12+C15+C18, C9+C12+C15+C18+C21)
              
            • #597359

              THANKYOU!!!!

              It seems to work. You are a lifesaver.

              Have a wonderful day!

              Kerry

            • #596256

              Between yourself and Lagare Coleman, all of the issues appear to have been sorted out now.

              Thank you again for your help – it is greatly appreciated.

              Regards

              Kerry

            • #595462

              Hi Lagare

              Bet you thought you were rid of me? I wonder if you can cast your mind back to this problem? I have returned to the original message so you can follow the thread.

              I am still having a problem with this BEAST. The version you returned to me seemed to still run into 4 pages when I printed it, so I thought I would be clever and record a macro of the print set up. Then copy it into the module. This does work, however it takes FOREVER to execute. Are you able to assist me in speeding it up?

              Kind Regards

              Kerry

            • #589221

              Sorry Kerry, I should really have put some explanation with the posting.

              If you look at cell B10 in the period 2 worksheet it has the formula
              =INDIRECT(GetPrev(A1)&”!M28″)
              GetPrev is a function I developed (with help from this site if I recall) which returns the name of the previous worksheet – in this case “period 1”. Ignore the A1 the function needs a cell address any one will do. So with that value “period 1” the formula now reads
              =INDIRECT(“period 1!M28”)
              which brings the carrry forward value from the period 1 sheet into period 2. Take a copy of period 2 and call it period 3 and the cell formula is copied but now returns period 2 as the previous worksheet. And so on – each copy you make it returns the previous sheet but the formula stays the same.
              This gives the same result as copying the sheet then manually editing the formula in each one to read period 2, period 3 etc.
              In cell C1 it uses the same fornula but adds 28 to move the start date to the next 4 week period.

              hope this makes it a bit easier to figure out.

            • #589270

              Hi Bryan

              Thankyou kindly for your explanation of the formula in your spreadsheet. It is obviously an option I could have considered.

              You may see that Legare has adjusted the macro to fix this for me and it works very well. You may find it useful too.

              Once again thank you. Isnt this site the best!! I have never come across such a helpful and friendly group. I hope I can offer someone help one day, instead of always asking the questions.

              Kind Regards
              Kerry

    Viewing 0 reply threads
    Reply To: Macro help for Timesheet (Excel 97)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: