Results 1 to 13 of 13

Thread: [RESOLVED] Please help a VBA newbe

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    14

    Resolved [RESOLVED] Please help a VBA newbe

    This is what I need.
    1. When the workbook is opened for the very first time, I need it to prompt (see existing macro) for the year that the information is being entered.
    2. This entry needs to be added to the end of all existing worksheets, i.e. Jan06, Feb06, ...... NOTE: This should only happen the first time it is opened!
    3. On closing, it nees to prompt for a "saveas" name so the original does not get overwritten.

    Thanks in advance.
    -robert My email

    p.s. In trying to learn, it seems as though a bunch of crap was attached to the VBA section. How and what do I clean up?
    Attached Files Attached Files
    Last edited by ROB IN RWC; Mar 20th, 2006 at 03:56 AM.

  2. #2
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Please help a VBA newbe

    Robert:

    I don't have zip capability here ... do all your sheets have default starting names like "Jan", "Feb", etc.? Then you append the Year name on them? Are there any sheets other than the "Jan" - "Dec" sheets?

    If the 'template' is set up right, on opening the workbook you can just test the sheet names for a sheet with the Date suffix already installed. If this is the case, you just continue with the open Workbook. If the suffixes are NOT installed, then you pop up your user input box for entering the year, and then write the suffix to the appropriate sheets. That satisfies points 1 and 2.

    What are the rules for your "SaveAs" in point 3?
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  3. #3
    Member
    Join Date
    Nov 2005
    Posts
    44

    Re: Please help a VBA newbe

    To get a new save name for your worksheet you could write something like this:

    (where excelApp is an excel.application object)

    VB Code:
    1. fName = excelApp.GetSaveAsFilename(InitialFilename:=fileName, FileFilter:="Excel Files (*.xls), *.xls")

    then you can save your workbook like this (where excelWB is excel.workbook object)
    VB Code:
    1. excelWB.SaveAs fileName:=fName
    2.         excelWB.Close savechanges:=True

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    14

    Re: Please help a VBA newbe

    How would I write the code for the testing of the tabs to see if the date information is present. Basically, how would I write the entire code?

  5. #5
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Please help a VBA newbe

    Incidently Rob, Welcome to the Forum! We hope your journey here is productive and pleasant!

    Hey ... please answer my question first!
    do all your sheets have default starting names like "Jan", "Feb", etc.? Then you append the Year name on them? Are there any sheets other than the "Jan" - "Dec" sheets?
    Can't write any code without knowing the details!

    I've been thinking about the process of creating a new book ... you need to do that too! I don't know how you have planned to do it, but if you have a "Template" book, it will never have the year embedded in the Sheet Names, and so you can always pop up the User Input prompt for the processing year. Then you can copy the appropriate template sheets into a new Workbook, and then rename the sheets with the correct year. This way, once a working book is created, it will have the correct sheet names, but it will not have the macro in it to worry about. Do youi have any other macros that will need to be included in the new book?

    These are the types of things that need to be laid out first before you start banging on code. I certainly wouldn't write any code without these answers. That doesn't mean that the code won't change as you learn more about the application ... hey, hacking is fun! But you need to build on a solid foundation.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    14

    Re: Please help a VBA newbe

    Check this out and it may answer your questions. My Webpage Again, I would like to use this as the template. When it is first opened, it would pull up a msg box asking for the year (format yy) then put that yy at the end of every tab.

    When closing, it would open a msg box asking for a SaveAs name, then save it accordingly.

    When it was reopened, it would not ask either of these again.

    I don't know the code for this and any help would be great.

    Thanks
    -Rob
    Last edited by ROB IN RWC; Mar 22nd, 2006 at 05:38 AM.

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    14

    Re: Please help a VBA newbe

    OK, again.

    I have an excel spreadsheet (.xls)
    The tabs at the bottom of the screen are Jan, Feb, etc... through Dec.
    I want to keep this original sheet as the template that is used year after year. I don't want it to be overwritten; so I need a fail safe.

    When the sheet is FIRST OPENED I would like it to prompt for a year in a msg box in the format of yy.
    I would like the answer to that question to rename the sheets, i.e. Jan06, Feb06, etc... through Dec06

    Once the user inputs their data, UPON CLOSE, I would like a msg box to pop up asking for a Save Name. That would save the sheet with the new name so as not to overwrite the original.

    I only want these msg boxes to appear the first time the spreadsheet is accessed. NOT on the second opening of the renamed sheet.

    Is that possible? If so, what is the code?

    Thanks.

  8. #8
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Please help a VBA newbe

    Once the user inputs their data, UPON CLOSE, I would like a msg box to pop up asking for a Save Name. That would save the sheet with the new name so as not to overwrite the original.
    If you save the template as a .xlt file then Excel will automatically prompt the user for a save name. That's the easiest way to handle that piece.

    Here'e the code to change the sheet name, appending the year number. You will need to copy this code into the code page for the "Thisworkbook" object.
    VB Code:
    1. Private Sub Workbook_Open()
    2. Dim sYearNum As String
    3. Dim wksWorkSheet As Worksheet
    4.    
    5.     'First we check to see if the name of the first sheet
    6.     'has been changed.
    7.     If ThisWorkbook.Worksheets(1).Name = "Jan" Then
    8.        
    9.         'If it hasn't, then prompt the user for the yearnum
    10.         'Continue to prompt until the year number is numeric
    11.         'and 2 characters long
    12.         Do While (Not IsNumeric(sYearNum) Or Len(sYearNum) <> 2)
    13.             sYearNum = InputBox("Please enter the Year Number (yy)")
    14.         Loop
    15.        
    16.         'loop through each sheet in the book
    17.         'appending the year num to the sheet name
    18.         For Each wksWorkSheet In ThisWorkbook.Worksheets
    19.             wksWorkSheet.Name = wksWorkSheet.Name & sYearNum
    20.         Next wksWorkSheet
    21.     End If
    22.    
    23. End Sub
    Last edited by DKenny; Mar 22nd, 2006 at 03:26 PM.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  9. #9
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Please help a VBA newbe

    So you want the Month Sheets in the master book to be used as templates. The user selects ONE sheet for entering data. You want to spin off that one sheet with a year code appended to the sheet name and save it as a new book. Correct?

    If this is correct, there will be no macro in the new book, and so it will never ask about changing the sheet name again. When the new book is reopened, you will just get the sheet with the year appended to the name.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  10. #10

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    14

    Re: Please help a VBA newbe

    Hey Kenny,
    It worked for changing the sheet names perfectly. Thank You.
    The part about the save as (using .xlt) isn't exactly what I was looking for.
    I am looking to have a msg box (just like the yy box) that will ask for a "SaveAs" name (required). The person who is going to use this sheet will invaribly overwrite the original by not knowing that they need to change the file name. I want it to be "any monkey could do it EASY"
    With the xlt it prompts with the question, "do you want to save" and that save could still overwrite the original.
    I hope that makes sense.
    -Rob

  11. #11
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Please help a VBA newbe

    he person who is going to use this sheet will invaribly overwrite the original by not knowing that they need to change the file name.
    If the original is saved as a template file(.xlt extension) ,e.g. RobReport.xlt, then when the user opens a copy it will automatically be named RobReport1, if they open another it will be called RobReport2, etc.
    When they try to save, the file will get a .xls extension and they will be prompted to give a location.
    All of this make it fairly difficult for JoeUser to overwrite your template...
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  12. #12

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    14

    Re: Please help a VBA newbe

    This is from another post and similar to what I am looking for.....

    I've got the following code to save a workbook with a user specific name and
    it checks to see if the file already exists and displays a message to inform
    the user, if the filename does not exist then it saves the workbook.


    All seems to work apart from the fact that if you click on 'cancel' on the
    save as alert box then the workbook still gets saved as 'false.xls'. I know
    there must be a bug in my code but unfortunately this is my first
    spreadsheet using VBA and I need some assistance.


    If anyone can point me in the right direction (ignore the astrLinks).


    Sub SaveSummarySheet()


    Dim astrLinks As Variant
    Dim stSaveName As String


    Application.DisplayAlerts = False
    ActiveWorkbook.Activate


    stSaveName = Application.GetSaveAsFilename( _
    fileFilter:="Excel Files (*.xls), *.xls")


    If FileExists(stSaveName) Then
    MsgBox stSaveName & " Already Exists", Buttons:=vbOKOnly +
    vbInformation, Title:="File Already Exists"
    SaveSummarySheet
    Else
    ActiveWorkbook.SaveAs Filename:=stSaveName,
    FileFormat:=xlWorkbookNormal, ReadOnlyRecommended:=True
    astrLinks =
    ActiveWorkbook.LinkSources(Type:=xlLinkTypeExcelLinks)
    MsgBox "File Saved As: " & stSaveName, Buttons:=vbOKOnly +
    vbInformation, Title:="File Saved"
    End If


    Sheets("Summary").Select
    Application.DisplayAlerts = True


    End Sub


    Function FileExists(stFile As String) As Boolean
    If Dir(stFile) <> "" Then FileExists = True
    End Function

  13. #13

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    14

    Re: Please help a VBA newbe

    Basically, when the user clicks on the close button, a msg box would pop up asking for the saveas name. That is the only was to close the document.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width