Templates

paulsolar

Well-known Member
Joined
Aug 21, 2013
Messages
691
Office Version
  1. 365
Hi All

I'm new to forums, so I'll give it a go

I've written a workbook for one section of PAS 2030 for my friends business to help him out. I can get everything working but am now stuck. I'm not bad with excel as I've been using it for 20 years or so to run small jobs.

Basically would like to do is have a job template that has a macro to:
  • increment the job numbers (done this before and had it working loads of times)
  • clear out the name and address cells each time the number is increased (done this before and had it working loads of times)
  • save the completed spread sheet to a folder on the desk top (never done this before!)
  • the completed sheet needs to be locked for auditing purposes (never done this before!)
Anyway I managed to a Mr Excel pod cast that showed some of this but I haven't managed to get it working

could someone out there stop from going bald please as I'm pulling my hair out. i look at some of the Macros and my mind boggles!

I've been trying the macro below but for some reason it will not work and keeps coming up with an error. This macro doesn't even cover the locking of the file when it's save I know, so if someone could help with that I'd be eternally grateful

Sub nextjob()
Range("g26").Value = Range("g26").Value + 1
Range("g6:g10").ClearContents
End Sub


Sub SaveNextJobWithNewName()
Dim NewFN As Variant
'Copy Job to new workbook
ActiveSheet.Copy
NewFN = "c:\desktop\Job Folder\Job" & Range("g26").Value & ".xlsx"
ActiveWorkbook.SaveAs NewFN, FileFormat:=xlOpenXMLWorkbook
ActiveWorkbook.Close
nextjob
End Sub

Many thanks in anticipation

Cheers

Paul:confused:
 
Thanks very much for you help.

I had found the problem with the Sheet 1 and changed that but I'm still getting the problem.

Like you I think I'm going to call it a night and start again tomorrow

cheers

Paul
 
Upvote 0

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Paul, you are mixing up things. Please, slow down and try to understand and in particular look at what you are doing. First you a re working with code that we made together and nearly worked other than one small error on my part, and now you are going back to old code that you know does not work. It is very frustrating if you start changing things back, like putting the path and filename back into one string inside your macro, when i explicitly pulled it out so it can be used in the other macro as well. There is a further point for doing what i did, because then it is easier to maintain in your code: if at some point you move the files, you only need to change it in one spot. The reason why the file saving did not work initially is because you did not read my instructions to make sure the path ended with a \ and that the filename should be without the path. That is why i say, slow down! Read what i and others are telling you.

and please, start debugging as explained in my guide to better VBA, step through the code, then at least you will understand far better what is happening. Do yourself this favour.

when i get a chance to get behind a pc i will make the correction in the code and post it.
 
Upvote 0
Hi Sijpie

Many thanks for all your help

I've kept a copy of your original code safe. I'm learning as I go along here and finding it a difficult learning curve. The debugging is teaching me a lot as I progress. I've been using some of the old code to see what makes things tick, as a learner I think this is important.

When I originally started I didn't know anything about code or what it did, now I'm getting to understand it and how it moves data around. I think without yours and everyone on this forums help I'd be stumbling in the dark completely, you have all given me a candle to work with. When I've finished this project I am determined to carry on with other projects as the more I learn the more I am enjoying it. I'm sure that over the next few months the light will come on a little brighter as I get to understand it more

I'll leave it with you until later and wont do any more for now.

Just to get you up to date, I found one problem which was that I had Sheet 1 instead of Sheet1 as the tab.

The main workbook that the macro is attached to is:
C:\Users\Paul Breen\Desktop\Dry House\Dry House Job Management & Records\Job Entry Workbook.xlsm and the info is on Sheet1

The Folder that the whole job record needs to be saved into is:
C:\Users\Paul Breen\Desktop\Dry House\Dry House Job Management & Records\Job Numbers

The workbook that the cell details need to be copied to for quick reference is:
C:\Users\Paul Breen\Desktop\Dry House\Dry House Job Management & Records\Job Numbers\Job register.xlsx and the info goes to Sheet1

Once again thanks to everyone for reading my posts and helping.

Kind regards

Paul

ps I wish I had started learning this at sixteen not sixty my mind may have taken it in better then!
 
Last edited:
Upvote 0
Hey there is still hope! I'm only just behind youin age (although i have been prgramming for a long time). And there is a lot of online training material.

Also I can recommend a book which is a bit higher level stuff, but excellent explanations, and one of the authors happens to be Bill Jelen, or Mr Excel (ie this website's owner...)
[h=1]VBA and Macros: Microsoft Excel 2010 VBA and Macros: Microsoft Excel 2010 (MrExcel Library): Bill Jelen, Tracy Syrstad: 9780789743145: Amazon.com: Books[/h][h=1]which you also cen get digitally


Here goes the next version of the code I have put your paths in the right places and changed the name of the register file.
[/h]
<font face=Courier New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br><br><br><SPAN style="color:#007F00">' following constants hold the PATH to files to be used _<br>  do NOT include file names. End with \</SPAN><br><SPAN style="color:#00007F">Const</SPAN> sJobFilePATH = "C:\Users\Paul Breen\Desktop\Dry House\Dry House Job Management & Records\Job Numbers\"<br><SPAN style="color:#00007F">Const</SPAN> sRegFilePATH = "C:\Users\Paul Breen\Desktop\Dry House\Dry House Job Management & Records\Job Numbers\"<br><br><SPAN style="color:#007F00">'----------------------------------------------</SPAN><br><SPAN style="color:#00007F">Sub</SPAN> TransfertoRegister()<br><SPAN style="color:#007F00">'</SPAN><br><SPAN style="color:#007F00">' Sub to transfer job details to register file</SPAN><br><SPAN style="color:#007F00">'----------------------------------------------</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> sJobnumber <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, sJobdate <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, sName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, _<br>        sAddressline1 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, sAddressline2 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, _<br>        sAddressline3 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, sPostcode <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, _<br>        sNameofEngineer <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> wbMyData <SPAN style="color:#00007F">As</SPAN> Workbook<br>    <SPAN style="color:#00007F">Dim</SPAN> wsOut <SPAN style="color:#00007F">As</SPAN> Worksheet<br>    <SPAN style="color:#00007F">Dim</SPAN> bCloseFlag <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> lRowCount <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#007F00">' in following constant File name only, no path!</SPAN><br>    <SPAN style="color:#00007F">Const</SPAN> sRegFileNAME = "Job Register.xlsx"<br><br><br><br><br>    <SPAN style="color:#00007F">With</SPAN> Worksheets("Entry Sheet")<br>        sJobnumber = .Range("g26")<br>        sJobdate = .Range("g18")<br>        sName = .Range("G6")<br>        sAddressline1 = .Range("G7")<br>        sAddressline2 = .Range("G8")<br>        sAddressline3 = .Range("G9")<br>        sPostcode = .Range("G10")<br>        sNameofEngineer = .Range("g20")<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    <br>    <SPAN style="color:#007F00">' Check if Registry file is open</SPAN><br>    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN><br>        <SPAN style="color:#00007F">Set</SPAN> wbMyData = Workbooks(sRegFileNAME)<br>    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> 0<br>    <br>    <SPAN style="color:#00007F">If</SPAN> wbMyData <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN>      <SPAN style="color:#007F00">' file is not open yet</SPAN><br>        <SPAN style="color:#007F00">' open file and set flag to remember to close again</SPAN><br>        <SPAN style="color:#00007F">Set</SPAN> wbMyData = Workbooks.Open(sRegFilePATH & sRegFileNAME)<br>        bCloseFlag = <SPAN style="color:#00007F">True</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <br>    <SPAN style="color:#00007F">Set</SPAN> wsOut = wbMyData.Sheets("sheet1")<br>    <SPAN style="color:#007F00">' transfer details to Job Registry file, add to end</SPAN><br>    <SPAN style="color:#00007F">With</SPAN> wsOut.Range("A1")<br>        lRowCount = .CurrentRegion.Rows.Count + 1<br>        .Offset(lRowCount, 0) = sJobnumber<br>        .Offset(lRowCount, 1) = sJobdate<br>        .Offset(lRowCount, 2) = sName<br>        .Offset(lRowCount, 3) = sAddressline1<br>        .Offset(lRowCount, 4) = sAddressline2<br>        .Offset(lRowCount, 5) = sAddressline3<br>        .Offset(lRowCount, 6) = sPostcode<br>        .Offset(lRowCount, 7) = sNameofEngineer<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    <SPAN style="color:#007F00">' add hyperlink to the jobfile for easy access</SPAN><br>    <SPAN style="color:#00007F">With</SPAN> wsOut<br>        .Hyperlinks.Add anchor:=.Range("A1").Offset(lRowCount, 0), _<br>                Address:=sJobFilePATH & "Job" & _<br>                .Range("A1").Offset(lRowCount, 0).Value & ".xlsx"<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    <br>    <SPAN style="color:#007F00">' save registry and close if it wasn't already open</SPAN><br>    <SPAN style="color:#00007F">If</SPAN> bCloseFlag <SPAN style="color:#00007F">Then</SPAN><br>        wbMyData.Close savechanges:=<SPAN style="color:#00007F">True</SPAN><br>    <SPAN style="color:#00007F">Else</SPAN><br>        wbMyData.Save<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><br>    <SPAN style="color:#007F00">'clean up</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> wbMyData = <SPAN style="color:#00007F">Nothing</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> wsOut = <SPAN style="color:#00007F">Nothing</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><br><SPAN style="color:#007F00">'----------------------------------------------</SPAN><br><SPAN style="color:#00007F">Sub</SPAN> NextJob()<br><SPAN style="color:#007F00">'</SPAN><br><SPAN style="color:#007F00">' Macro to clear form, ready for next entry</SPAN><br><SPAN style="color:#007F00">'----------------------------------------------</SPAN><br>    Range("g26").Value = Range("g26").Value + 1<br>    Range("g6:g11").ClearContents<br>    Range("g19:g20").ClearContents<br>    Range("g24").ClearContents<br>    Range("g31:g43").ClearContents<br>       <SPAN style="color:#007F00">' Cell g13:g17 has validation list</SPAN><br>    Range("G13:G17").ClearContents<br>    <br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><br><br><SPAN style="color:#007F00">'----------------------------------------------</SPAN><br><SPAN style="color:#00007F">Sub</SPAN> SaveNextJobWithNewName()<br><SPAN style="color:#007F00">'</SPAN><br><SPAN style="color:#007F00">' macro to save entry form to new file for _<br>  archiving. Sheet is password protected _<br>  with random password to protect against _<br>  changes</SPAN><br><SPAN style="color:#007F00">'----------------------------------------------</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> sPW <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> iArr(1 <SPAN style="color:#00007F">To</SPAN> 10) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>, i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> NewFN <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <br>    <SPAN style="color:#007F00">' set file name for job</SPAN><br>    NewFN = sJobFilePATH & Range("g26").Value & ".xlsx"<br>    <br>    <SPAN style="color:#007F00">' fill password string with 10 random chars > 20 (exclude problem chars)</SPAN><br>    <SPAN style="color:#00007F">For</SPAN> i = 1 <SPAN style="color:#00007F">To</SPAN> 10<br>        sPW = sPW & Chr(CLng(Rnd(Time) * 200 + 20))<br>    <SPAN style="color:#00007F">Next</SPAN> i<br>    <br>    <SPAN style="color:#007F00">'copy job sheet to new workbook</SPAN><br>    ActiveSheet.Copy<br>    <SPAN style="color:#00007F">With</SPAN> ActiveSheet<br>        <SPAN style="color:#00007F">With</SPAN> .UsedRange<br>            <SPAN style="color:#007F00">' transform formulas to values</SPAN><br>            .Value = .Value<br>            <SPAN style="color:#007F00">' set cells to locked</SPAN><br>            .Locked = <SPAN style="color:#00007F">True</SPAN><br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>        <SPAN style="color:#007F00">' protect with random password</SPAN><br>        .Protect sPW<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    ActiveWorkbook.SaveAs NewFN, FileFormat:=xlOpenXMLWorkbook<br>    ActiveWorkbook.Close<br>    <br>    <SPAN style="color:#007F00">'transfer data to register</SPAN><br>    TransfertoRegister<br>    MsgBox "Job" & Range("g26").Value & ".xlsx registered & saved"<br>    <br>    <SPAN style="color:#007F00">'clear form for next job</SPAN><br>    NextJob<br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0

Forum statistics

Threads
1,223,105
Messages
6,170,128
Members
452,304
Latest member
Thelingly95

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top