Solved

Excel Macro Programming, saving in another worksheet

Posted on 2011-02-28
6
303 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

746 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

12 Experts available now in Live!

Get 1:1 Help Now