Solved

Excel Macro Programming, saving in another worksheet

Posted on 2011-02-28
6
310 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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: 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.

Question has a verified solution.

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

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 …
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

705 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