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:
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Why don't you Save-As immediately instead of copying to a new file first?

You start from your template (which you don't want to change i assume) and then fill out the stuff, then do a save-as under the new name and close. No need to create new file and copy sheet.

What do you mean with locking the file for audits:
  1. do you want to lock the file for access, so it can only be opened with password, or
  2. do you want to protect the sheet against changes?
 
Upvote 0
Hi Sijpie

Let me explain the process I'm using so you understand what I'm trying to achieve.

Basically I have a work book that books in a job. Every job has to have a unique reference number for auditing purposes to comply with government legislation, this number is assigned to cell G26. There are also fields for the name and address cells G6 to G10.

The work book for booking the jobs is actually 10 pages long when it is printed as it contains a lot of other information, the other information is generic and just copies the customers name and address into other cells, so when the cells G6 to G10 are cleared they clear the info from the generic parts of the rest of the work book. This all works fine.

I've locked the whole of the work book except cells that contain information which is specific to each job. This works fine.

The intention is that the operator just fills in the name and address and a few other bits from drop down boxes (by the was I've never managed to get clear contents to work for a cell with a drop down in it and I'm not sure if it is possible) , when this is done the operator just presses a macro "save button" which clears the content ready for the next job and increments the job number by 1 to avoid confusion or any replication of number and the work book is save to a folderf on the desk top, the file that is saved to the desk top should really be uneditable to avoid anyone tampering with it in the future (this is the part that an inspector can audit whenever they want).

The file is saved in the folder with the job reference number say "Job1" this can be altered in the code by using a different prefix in the line "NewFN = "c:\desktop\Job Folder\Job" & Range("g26").Value & ".xlsx" which I understand.

finally here is the bit that I don't understand and I'm not sure if it is even possible

I the folder on the desktop that each individual workbook is being saved to I'd like to have a separate spread sheet that keeps a record of the address that goes with each job number.

The idea i have is that every time a job is saved it creates an additional line in another spread sheet called "Cross Ref" in the job folder that stores the address against job numbers for easy look up.

Hope that explains what I'm up to

Cheers

Paul
 
Upvote 0
... (by the was I've never managed to get clear contents to work for a cell with a drop down in it and I'm not sure if it is possible) ...


Is the dropdown from a validation list? That works fine with my little test:

Code:
Sub tt()
   ' Cell L3 has validation list
    Cells(3, 11).ClearContents
End Sub

Check your setup of the validation list to see if blanks are allowed (ignore blanks)
 
Last edited:
Upvote 0
This will save the current sheet to a new workbook, convert formulas to values, lock all cells and password protect the sheet with random password.

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> SaveNextJobWithNewName()<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 = "c:\desktop\Job Folder\Job" & 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>    MsgBox "Job" & Range("g26").Value & ".xlsx 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><br></FONT>
 
Upvote 0
Hi Sijpie

Many thanks for the latest gem.

Below is as far as I've got so far. I've just added you code for clearing content from cells with drop down boxes and that seems to be working fine, but I'm now stuck. I cant get the code that copies the data to a "Register of jobs" to work. I'd like to fix that problem before I add the code you just sent me. Can you see where I've gone wrong please?

Sub transfertoregister()
Dim Jobnumber As String
Dim jobdate As String
Dim name As String
Dim addressline1 As String
Dim addressline2 As String
Dim addressline3 As String
Dim postcode As String
Dim nameofengineer As String
Dim mydata As Workbook




Worksheets("Entry Sheet").Select
Jobnumber = Range("g26")
Worksheets("Entry Sheet").Select
jobdate = Range("g18")
Worksheets("Entry Sheet").Select
name = Range("G6")
Worksheets("Entry Sheet").Select
addressline1 = Range("G7")
Worksheets("Entry Sheet").Select
addressline2 = Range("G8")
Worksheets("Entry Sheet").Select
addressline3 = Range("G9")
Worksheets("Entry Sheet").Select
postcode = Range("G10")
Worksheets("Entry Sheet").Select
nameofengineer = Range("g20")


Set mydata = Workbooks.Open("C:\Users\Paul Breen\Desktop\Register of jobs.xlsx")




Worksheets("sheet1").Select
Worksheets("sheet1").Range("A1").Select
RowCount = Worksheets("sheet1").Range("A1").CurrentRegion.Rows.Count
With Worksheets("sheet1").Range("A2")
.Offset(RowCount, 0) = Jobnumber
.Offset(RowCount, 1) = jobdate
.Offset(RowCount, 2) = name
.Offset(RowCount, 3) = addressline1
.Offset(RowCount, 4) = addressline2
.Offset(RowCount, 5) = addressline3
.Offset(RowCount, 6) = postcode
.Offset(RowCount, 7) = nameofengineer
End With
mydata.Save


End Sub
Sub nextjob()
Range("g26").Value = Range("g26").Value + 1
Range("g6:g11").ClearContents
Range("g19:g20").ClearContents
Range("g24").ClearContents
Range("g31:g43").ClearContents
' Cell g13:g17 has validation list
Range("G13:G17").ClearContents

End Sub




Sub SaveNextJobWithNewName()
Dim NewFN As Variant
'Copy Job to new workbook
ActiveSheet.Copy
NewFN = "c:\users\Paul Breen\Desktop\Dry House\Dry House Job Management & Records\Job Numbers\Job No " & Range("g26").Value & ".xlsx"
ActiveWorkbook.SaveAs NewFN, FileFormat:=xlOpenXMLWorkbook
ActiveWorkbook.Close
nextjob
End Sub
 
Upvote 0
I need to look at it tomorrow. Two questions:
1. Why are you using your old SaveNextJobWithNewName sub instead of the new one?
2. Can you post code in code tags (see my tagline on how to do it) please. It is very difficult to read otherwise, which means i have to transfer it into VBA Editor, then do all the indenting before i can make sense of it. A lot of folks refuse to answer posts like yours.
 
Upvote 0
Hi Sijpie

Many thanks for your assistance, I'm very new to all this coding.

I haven't incorporated your save next job as i was trying to figure out how it works so that i have a better understanding for the future. Your code for clearing content of cells with drop downs i could how that worked and could be incorporated immediately.

here goes at posting the code in the correct format

Code:

Code:
Sub transfertoregister()
Dim Jobnumber As String
Dim jobdate As String
Dim name As String
Dim addressline1 As String
Dim addressline2 As String
Dim addressline3 As String
Dim postcode As String
Dim nameofengineer As String
Dim mydata As Workbook




Worksheets("Entry Sheet").Select
Jobnumber = Range("g26")
Worksheets("Entry Sheet").Select
jobdate = Range("g18")
Worksheets("Entry Sheet").Select
name = Range("G6")
Worksheets("Entry Sheet").Select
addressline1 = Range("G7")
Worksheets("Entry Sheet").Select
addressline2 = Range("G8")
Worksheets("Entry Sheet").Select
addressline3 = Range("G9")
Worksheets("Entry Sheet").Select
postcode = Range("G10")
Worksheets("Entry Sheet").Select
nameofengineer = Range("g20")


Set mydata = Workbooks.Open("C:\Users\Paul Breen\Desktop\Register of jobs.xlsx")




Worksheets("sheet1").Select
Worksheets("sheet1").Range("A1").Select
RowCount = Worksheets("sheet1").Range("A1").CurrentRegion.Rows.Count
With Worksheets("sheet1").Range("A1")
.Offset(RowCount, 0) = Jobnumber
.Offset(RowCount, 1) = jobdate
.Offset(RowCount, 2) = name
.Offset(RowCount, 3) = addressline1
.Offset(RowCount, 4) = addressline2
.Offset(RowCount, 5) = addressline3
.Offset(RowCount, 6) = postcode
.Offset(RowCount, 7) = nameofengineer
End With
mydata.Save


End Sub
Sub nextjob()
Range("g26").Value = Range("g26").Value + 1
Range("g6:g11").ClearContents
Range("g19:g20").ClearContents
Range("g24").ClearContents
Range("g31:g43").ClearContents
   ' Cell g13:g17 has validation list
    Range("G13:G17").ClearContents
    
End Sub




Sub SaveNextJobWithNewName()
Dim NewFN As Variant
'Copy Job to new workbook
ActiveSheet.Copy
NewFN = "c:\users\Paul Breen\Desktop\Dry House\Dry House Job Management & Records\Job Numbers\Job No " & Range("g26").Value & ".xlsx"
ActiveWorkbook.SaveAs NewFN, FileFormat:=xlOpenXMLWorkbook
ActiveWorkbook.Close
nextjob


End Sub

I hope this helps

Kind regards

Paul
 
Upvote 0
Paul,

here is the complete package. Read through the comments to see what it does.

<font face=Courier New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br><br><br><br><SPAN style="color:#00007F">Const</SPAN> sJobFilePATH = "c:\desktop\Job Folder\Job"<br><SPAN style="color:#00007F">Const</SPAN> RegFilePATH = "C:\Users\Paul Breen\Desktop\"<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">Const</SPAN> sRegFileNAME = "Register of jobs.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(RegFilePATH & 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 = mydata.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>        RowCount = .CurrentRegion.Rows.Count + 1<br>        .Offset(RowCount, 0) = sJobnumber<br>        .Offset(RowCount, 1) = sJobdate<br>        .Offset(RowCount, 2) = sName<br>        .Offset(RowCount, 3) = sAddressline1<br>        .Offset(RowCount, 4) = sAddressline2<br>        .Offset(RowCount, 5) = sAddressline3<br>        .Offset(RowCount, 6) = sPostcode<br>        .Offset(RowCount, 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:=.Offset(RowCount, 0), _<br>                Address:=sJobFilePATH & "Job" & _<br>                .Offset(RowCount, 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,104
Messages
6,170,124
Members
452,303
Latest member
c4cstore

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