+ Reply to Thread
Results 1 to 14 of 14

VB Projects and XLM Sheets cannot be saved in a macro-free workbook.

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-28-2012
    Location
    Klang, Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    113

    VB Projects and XLM Sheets cannot be saved in a macro-free workbook.

    Hello XL Guru,
    I got no idea why my new work book not working. While the old workbook i used the same code is doing fine but not this.

    Private Sub Workbook_Open()
    Dim wSheet As Worksheet
        For Each wSheet In Worksheets
    
            wSheet.Protect Password:="123", _
            UserInterFaceOnly:=True
    Next wSheet
    End Sub
    
    Sub SaveScreeningWithNewName()
        Dim NewFN As Variant
        ' Copy Screening to a new workbook
        Sheets(Array("Screening", "Agent-Sea", "Agent-Air", "B.Confirmation", "S.I.", "HBL")).Copy
        ActiveSheet.Shapes.Range(Array("****")).Delete
        NewFN = "\\CYCHUA-PC\Users\Public\Documents\Agent Record\" & Sheets(1).Range("G1").Text & Sheets(1).Range("H2").Value & " " & Sheets(1).Range("c3").Text & ".xlsx"
        ActiveWorkbook.SaveAs NewFN, FileFormat:=xlOpenXMLWorkbook
        ActiveWorkbook.Close
    End Sub
    Sub NextScreening()
        Sheets(1).Range("h1").Value = Range("h1").Value + 1
    End Sub
    
    Sub uncheck_all()
    Dim sh As Shape
    Application.ScreenUpdating = False
        For Each sh In ActiveSheet.Shapes
          If sh.Type = msoOLEControlObject Then
            If TypeName(sh.OLEFormat.Object.Object) = "CheckBox" Then sh.OLEFormat.Object.Object = False
          End If
          If sh.Type = msoFormControl Then
            If sh.FormControlType = xlCheckBox Then sh.OLEFormat.Object = False
          End If
        Next sh
    Application.ScreenUpdating = True
    End Sub
    
    Sub ClearUnlockedCells()
     
        Dim wks As Worksheet
     
        For Each wks In ThisWorkbook.Worksheets
            On Error Resume Next
            wks.UsedRange.Value = vbNullString
            Err.Clear: On Error GoTo -1: On Error GoTo 0
        Next wks
     
        Set wks = Nothing
     
    End Sub
    When i test run on the macro, it kept showing message as titled mentioned above.
    Greatly appreciated, if any1 would point out where my mistake is.

    I understand that macro could not run on a protected sheets. After google for hours, i found this userinterface thingy, still no luck.
    Attached Files Attached Files

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: VB Projects and XLM Sheets cannot be saved in a macro-free workbook.

    if the workbook being saved contains code you need file format xlOpenXMLWorkbookMacroEnabled
    Cheers
    Andy
    www.andypope.info

  3. #3
    Forum Contributor
    Join Date
    08-28-2012
    Location
    Klang, Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    113

    Re: VB Projects and XLM Sheets cannot be saved in a macro-free workbook.

    Thanks for your reply, Andy.

    Tried that too, when i google around last 2 hours ago.
    Window, pop out the message as " This extension can not be used with the selected file type. Change the file extension in the file name text......."

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: VB Projects and XLM Sheets cannot be saved in a macro-free workbook.

    This worked for me. Note I changed save path.

    Sub SaveScreeningWithNewName()
        Dim NewFN As Variant
        ' Copy Screening to a new workbook
        Sheets(Array("Screening", "Agent-Sea", "Agent-Air", "B.Confirmation", "S.I.", "HBL")).Copy
        ActiveSheet.Shapes.Range(Array("****")).Delete
        NewFN = "C:\temp\" & Sheets(1).Range("G1").Text & Sheets(1).Range("H2").Value & " " & Sheets(1).Range("c3").Text & ".xlsm"
        ActiveWorkbook.SaveAs NewFN, FileFormat:=xlOpenXMLWorkbookMacroEnabled
        ActiveWorkbook.Close
    End Sub
    Also you Workbook_Open code should be in the Thisworkbook object if you want it to execute on openning.

  5. #5
    Forum Contributor
    Join Date
    08-28-2012
    Location
    Klang, Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    113

    Re: VB Projects and XLM Sheets cannot be saved in a macro-free workbook.

    You mean this below?
    Private Sub Workbook_Open()
    Dim wSheet As Worksheet
        For Each wSheet In Worksheets
    
            wSheet.Protect Password:="123", _
            UserInterFaceOnly:=True
    Next wSheet
    End Sub
    Sorry to tell you. All of the code is i get help at excelforum and google.
    I understand partially from your replied above. But not really get it what object to change or something. Very sorry on this, i am a nut in VBA.

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: VB Projects and XLM Sheets cannot be saved in a macro-free workbook.

    the extension you are using is "xlsx" which needs to be "xlsm"

  7. #7
    Forum Contributor
    Join Date
    08-28-2012
    Location
    Klang, Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    113

    Re: VB Projects and XLM Sheets cannot be saved in a macro-free workbook.

    Same there. I am okay with it, if my new saved file is macro-enable or not.

    Before i come to board, i switch from .xslm and xslx. But both no luck.

  8. #8
    Forum Contributor
    Join Date
    08-28-2012
    Location
    Klang, Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    113

    Re: VB Projects and XLM Sheets cannot be saved in a macro-free workbook.

    I even un-protect all of my 14 sheets and test it to run again. But still the same.

  9. #9
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: VB Projects and XLM Sheets cannot be saved in a macro-free workbook.

    currently that code is in a standard code module. It needs to be in the Thisworkbook code module.

    in VBE (ALT+F11)
    in project view (F4)
    you should see Thisworkbook listed within Microsoft Excel Object for that VB Project

    Cut and paste the code in there. Then next time the workbook is opened the Open event will fire and the code will be run

  10. #10
    Forum Contributor
    Join Date
    08-28-2012
    Location
    Klang, Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    113

    Re: VB Projects and XLM Sheets cannot be saved in a macro-free workbook.

    Ok, got it now. Partially okay on my code for now. Just that my saved file name does not work as coded.

    Hold a sec., need to get rid of that.

  11. #11
    Forum Contributor
    Join Date
    08-28-2012
    Location
    Klang, Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    113

    Re: VB Projects and XLM Sheets cannot be saved in a macro-free workbook.

    Excelobject.png

    You mean "Thisworkbook" suppose to be listed on the left circled area?

  12. #12
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: VB Projects and XLM Sheets cannot be saved in a macro-free workbook.

    like this

    922380.png

  13. #13
    Forum Contributor
    Join Date
    08-28-2012
    Location
    Klang, Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    113

    Re: VB Projects and XLM Sheets cannot be saved in a macro-free workbook.

    Hi Andy, all set and good to go.
    Just that, i understand if i have another query, i should post a new thread again. If it is convenience for you, hope that you are able to advise me a little.

    Sheet(1) cell H1 is my reference number and newly saved file name.
    Is it possible to make exception on the below code, not to clear the contents on H1, so that another code of mine to +1 on the value is workable.

    Code - Clear Contents
    Sub ClearUnlockedCells()
     
        Dim wks As Worksheet
     
        For Each wks In ThisWorkbook.Worksheets
            On Error Resume Next
            wks.UsedRange.Value = vbNullString
            Err.Clear: On Error GoTo -1: On Error GoTo 0
        Next wks
     
        Set wks = Nothing
     
    End Sub
    Code +1 on H1
    Sub NextScreening()
        Sheets(1).Range("h1").Value = Range("h1").Value + 1
        uncheck_all
    End Sub

  14. #14
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: VB Projects and XLM Sheets cannot be saved in a macro-free workbook.

    Why not store the content in a variable, just before clearing the sheet, and write it back out after clearing the sheet.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1