• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Open Excel template and autosave file with date

Chica9000

New Member
Hello,


My question is 2-part:

I'm looking for help with building an Excel 2007 template that, when opened from the network location, will assign a unique sequential number (i.e. Purchase Order #), and will also auto-save as a new file that can be worked with where the file name includes the date and the assigned number.


I'm not a code-writer or VBA user, but am hoping this will be my first foray into that scary world.


Thank you for any help!
 
Hi, Chica9000!


First of all welcome to Chandoo's website Excel forums. Thank you for your joining us and glad to have you here.


As a starting point I'd recommend you to read the green sticky topics at this forums main page. There you'll find general guidelines about how this site and community operates (introducing yourself, posting files, netiquette rules, and so on).


Among them you're prompted to perform searches within this site before posting, because maybe your question had been answered yet.


Feel free to play with different keywords so as to be led thru a wide variety of articles and posts, and if you don't find anything that solves your problem or guides you towards a solution, you'll always be welcome back here. Tell us what you've done, consider uploading a sample file as recommended, and somebody surely will read your post and help you.


And about questions in general...


If you haven't performed yet the search herein, try going to the topmost right zone of this page (Custom Search), type the keywords used in Tags field when creating the topic or other proper words and press Search button. You'd retrieve many links from this website, like the following one(s) -if any posted below-, maybe you find useful information and even the solution. If not please advise so as people who read it could get back to you as soon as possible.


And about this question in particular...


Give a look at this file:

https://dl.dropboxusercontent.com/u/60558749/Open%20Excel%20template%20and%20autosave%20file%20with%20date%20%28for%20Chica9000%20at%20chandoo.org%29.xlsm


This is the code:

-----

[pre]
Code:
Option Explicit

Private Sub Workbook_Open()
'
' constants
Const ksWSLastNo = "Hoja1"
Const ksLastNo = "ParamLastNo"
Const ksExtensionMacro = ".xlsm"
Const ksExtensionNormal = ".xlsx"
Const ksSeparator = "_"
Const kiDigits = 8
'
' declarations
Dim wb As Workbook, ws As Worksheet, rng As Range
Dim I As Long, A As String
'
' start
With Application
.DisplayAlerts = False
.ScreenUpdating = False
End With
'
' process
'  number
Set rng = Worksheets(ksWSLastNo).Range(ksLastNo)
With rng
I = .Cells(1, 1).Value
I = I + 1
.Cells(1, 1).Value = I
End With
Set rng = Nothing
'  old wb
With ThisWorkbook
' save
.Save
' source
Set ws = .Worksheets(ksWSLastNo)
' new wb
Set wb = Workbooks.Add
' name
A = Left(.Name, Len(.Name) - Len(ksExtensionMacro)) & ksSeparator & _
Format(Now(), "yyyy-mm-dd") & ksSeparator & _
Format(I, String(kiDigits, "0")) & ksExtensionNormal
End With
'  new wb
With wb
' paste in target
ws.Copy .Sheets(1)
' extra worksheets
For I = .Worksheets.Count To 2 Step -1
.Worksheets(I).Delete
Next I
' rename ws
.Worksheets(1).Name = ws.Name
' save
.SaveAs ThisWorkbook.Path & Application.PathSeparator & A, xlWorkbookDefault
End With
'  old wb
With ThisWorkbook
' close
.Close
End With
'
' end
Set ws = Nothing
Set wb = Nothing
With Application
.ScreenUpdating = True
.DisplayAlerts = True
End With
'
End Sub
[/pre]
-----


Just advise if any issue.


Regards!
 
Hi, Chica9000!

Glad you solved it. Thanks for your feedback and for your kind words too. And welcome back whenever needed or wanted.

Regards!
 
Back
Top