VBA - 保存工作簿而无法更改内容

时间:2016-05-18 08:15:02

标签: excel vba excel-vba

我有一个宏,可以在VBA中创建工作簿的副本。我想要这个副本"只读",但属性ReadOnly := True不起作用。 你能帮助我吗 ?这是代码:

第一个宏:

Sub SaveXL()

Dim Nom2 As String
Dim Jour2 As String
Dim FPath2 As String
Jour2 = Format(Now(), "yyyymmdd - h\hmm")
Nom2 = Jour2 & " Pricelist"
FPath2 = Sheets("PARAM").Range("B33").Value
On Error GoTo fin4
fichier = Application.GetSaveAsFilename(FPath2 & Nom2, "Fichiers Excel (*.xls), *.xls")
If fichier <> "Faux" Then
    ActiveWorkbook.SaveCopyAs fichier
    VBA.SetAttr fichier, vbReadOnly
    Test GetAName(fichier)
Else
    MsgBox "Le fichier n'a pas été enregistré"
End If
Exit Sub
fin4:         MsgBox "La création de l'excel a échoué"
End Sub

第二个:

Sub Test(targetWorkbookName As String)
Dim F As Integer, C As Integer, derniereligne
Dim targetWorkbook As Workbook
On Error Resume Next
Set targetWorkbook = Workbooks(targetWorkbookName)
On Error GoTo 0
If (targetWorkbook Is Nothing) Then _
    Set targetWorkbook = Workbooks.Open(Filename := targetWorkbookName, ReadOnly := True)

For F = 1 To Sheets.Count
    ActiveSheet.Select
    For C = 15 To 2 Step -1
        ActiveSheet.Columns(C).Select
        Selection.End(xlDown).Select
        derniereligne = ActiveCell.Row
        If ActiveSheet.Columns(C).Hidden = True Then
            ActiveSheet.Columns(C).Delete
        End If
    Next C
Next F
Application.DisplayAlerts = False
Sheets("PARAM").Delete
 ActiveWorkbook.ActiveSheet.Shapes.Range(Array("Button 2")).Select
            Selection.Delete
 ActiveWorkbook.ActiveSheet.Shapes.Range(Array("Button 9")).Select
            Selection.Delete

targetWorkbook.SaveAs Filename:=targetWorkbookName,    FileFormat:=xlOpenXMLWorkbook
End Sub

谢谢!

3 个答案:

答案 0 :(得分:2)

如果您想使工作簿不可保存,您可以执行以下操作:

ThisWorkbook模块中使用:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

 Cancel = True

End Sub

然后到达即时窗口(按 Ctrl + G )并输入:

Application.EnableEvents = False - 点击输入
   ThisWorkbook.Save - 点击输入
   Application.EnableEvents = True - 点击输入

现在,当用户尝试保存工作簿时,它只会取消保存,这意味着数据不能永久覆盖。

答案 1 :(得分:0)

ActiveWorkbook.ChangeFileAccess Mode:=xlReadOnly

答案 2 :(得分:0)

只读是文件系统权限,而不是由Excel控制的权限

只读建议使用它的Excel控制版本,提示用户以只读方式打开它(但是他们可以选择否)。

要以只读方式保存工作簿的副本,您需要: -

  1. 使用SaveCopyAs
  2. 保存副本
  3. 打开副本
  4. 使用SaveAs保存副本,并将属性ReadOnlyRecommended设置为true
  5. 删除第一条指令中的上一个副本
  6. 以下是一个小例子: -     Public Sub Make_Copy_ReadOnlyRec()     Dim WkBk作为Excel.Workbook

    'Using SaveCopyAs
    ThisWorkbook.SaveCopyAs Environ("UserProfile") & "\Desktop\Temp.xlsm"
    
    'Open the copy
    Set WkBk = Application.Workbooks.Open(Environ("UserProfile") & "\Desktop\Temp.xlsm")
    
        'Use save as to make it read only recommended
        WkBk.SaveAs Environ("UserProfile") & "\Desktop\Sample.xlsm", XlFileFormat.xlOpenXMLWorkbookMacroEnabled, , , True
    
        'Close the now read only recommended copy
        WkBk.Close
    Set WkBk = Nothing
    
    'Delete the original copy
    Kill Environ("UserProfile") & "\Desktop\Temp.xlsm"
    
    End Sub