Solved

Excel Macro Programming, saving in another worksheet

Posted on 2011-02-28
6
306 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
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 the scrolling table in Microsoft Excel using the INDEX function.

914 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now