Solved

Excel Macro Programming, saving in another worksheet

Posted on 2011-02-28
6
307 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

770 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