Solved

Excel Macro Programming, saving in another worksheet

Posted on 2011-02-28
6
308 Views
Last Modified: 2012-05-11
I am new to programing in excel, I actually had a member to this for me.  Initially we have that when the excel document is opened (xslm document) It generates a number to a cell, from another worksheet, where it stores a number and keeps adding one, whenever the document is opened.  Is there a way instead of receiving the number from a worksheet, we can receive it from another document and do the incrementing there? Also, is there a way to automatically save as to another file with the extension xlsx as soon as they open it with the different number.  The reason why I want this is because multiple people are going to open this document and it is going to generate a number for them, but some of them might save the actually document, and reopen it to find another number has been generated, so if it saves it to another file that doesnt support macros, it wont regenerate another number, and keep the initial template in tact.  I hope I am not confusing anyone
Dim Numb As Long

Private Sub Workbook_Open()
    Dim lastRow As Long
    
    lastRow = Sheets("Temp").Range("A" & Rows.Count).End(xlUp).Row
    
    Numb = Sheets("Temp").Range("A" & lastRow).Value + 1
    
    Sheets("EXPENSE FORM").Range("N2").Value = Numb
    
    Sheets("Temp").Range("A" & lastRow + 1).Value = Numb
    ActiveWorkbook.Save
End Sub

Open in new window

0
Comment
Question by:falimorad
  • 4
  • 2
6 Comments
 
LVL 50

Accepted Solution

by:
Dave Brett earned 500 total points
ID: 34999770
You could use a log file (text file) saved in the same directory (or elsewhere) where your file is opened.

The code below generates a sequential number, plus it stores the window logon name plus time the Excel file was opened. So you get a detailed access record by who when

*****sample output ****
1,HABRED,28/02/2011 10:26:07 AM
2,HABRED,28/02/2011 10:26:14 AM
3,HABRED,28/02/2011 10:26:34 AM
4,HABRED,28/02/2011 10:26:40 AM

As soon as the file is opened you want it resaved as xlsx? leaving the original file as is?

Cheers

Dave



Private Sub Workbook_Open()
    Const ForReading = 1
    Const ForAppending = 8
    Dim objFSO As Object
    Dim objFil As Object
    Dim strFilePath As String
    Dim strLast As String
    Dim strNum As String
    Dim strContents As String
    Dim arrLines
    strFilePath = ThisWorkbook.Path & "\log.txt"
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    On Error Resume Next
    Set objFil = objFSO.getfile(strFilePath)
    On Error GoTo 0
    If objFil Is Nothing Then
        Set objFil = objFSO.createTextFile(strFilePath, ForWriting)
        objFil.writeline "1," & Environ("username") & "," & Now()
    Else
        Set objFil = objFSO.OpenTextFile(strFilePath, ForReading)
        strContents = objFil.ReadAll
        objFil.Close
        Set objFil = objFSO.OpenTextFile(strFilePath, ForAppending)
        arrLines = Split(strContents, vbCrLf)
        strLast = arrLines(UBound(arrLines) - 1)
        strNum = Val(Left(strLast, InStr(strLast, ",") - 1)) + 1
        objFil.writeline strNum & "," & Environ("username") & "," & Now()
    End If
    objFil.Close
    Sheets("EXPENSE FORM").Range("N2").Value = strNum
End Sub

Open in new window

0
 

Author Comment

by:falimorad
ID: 35008048
Yes as soon as its opened, I want it to be saved as an xlsx, and the original left untouched, but next time when the original is touched, it just increments the number
0
 

Author Comment

by:falimorad
ID: 35008114
and so that the files do not overwrite, they can save it using in the format "name_numberGenerated"
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:falimorad
ID: 35008389
sorry one more, thing, as soon as it saves as to xlsx, it should close the xlxm file. Is there a way that when it saves as, it disregards any pop up windows, because when you are saving it to another format it will inform you that the xlsx format does not support macros.
0
 

Author Closing Comment

by:falimorad
ID: 35011832
Great Solution
0
 
LVL 50

Expert Comment

by:Dave Brett
ID: 35013192
thx for the grade :)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
: 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…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

838 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