【问题标题】:Simple, but this won't enter the password on a protected excel sheet很简单,但这不会在受保护的 Excel 工作表上输入密码
【发布时间】:2019-01-11 07:43:18
【问题描述】:

我正在尝试让一个宏运行 150 个 Excel 工作簿,每个工作簿中有 5 个工作表,并在一个特定工作表上输入密码。

我已经删除了宏所做的其他内容,但是如果我删除了密码部分,宏会按应有的方式循环遍历所有文件。这意味着我必须手动输入密码。

密码不被接受,使工作表受到保护。

这是代码:

Option Explicit

Sub Examnew()    
    Dim rCell As Range, rRng As Range             'define loop names    
    Dim wbmaster As Workbook                      'name for master workbook    
    Dim wbtarget As Workbook                      'name for student workbook    
    Set wbmaster = ActiveWorkbook                 'set the name for the master

    'Student numbers in cells B3:B64 WARNING SET TO 2 STUDENTS ONLY FOR TEST
    'NOTE that st Nums are in col B with a duplicate in col A to collect results.

    Set rRng = wbmaster.Sheets("studentlist").Range("B3:B4”)

    For Each rCell In rRng '<                | loop through "students" range
        '<                                     | now open Student exam workbook and set to name "wbtarget"
        Workbooks.Open ("/Users/tester/Final_V1/" & rCell.Value & ".xlsx")

        Set wbtarget = Workbooks(rCell.Value & ".xlsx")
        Sheets("ANSWERS").Unprotect "Coursework2019"

        'Other stuff normally here…   
        wbtarget.Close (True) '<            | now save and close the student file...

    Next rCell   '<                          | next student number
End Sub

为任何帮助干杯。

【问题讨论】:

  • 你试过wbtarget.Sheets("ANSWERS").Unprotect "Coursework2019"吗?

标签: excel vba


【解决方案1】:
  1. 您的代码中有一些错误的括号。

    如果函数应该返回结果,参数的括号是必要的。如果过程/函数不返回结果,则不允许

    看这个例子:

    SomeProcedure(Parameter)         'wrong
    SomeProcedure Parameter          'correct
    
    result = SomeFunction(Parameter) 'correct
    result = SomeFunction Parameter  'wrong
    
  2. 您必须指定您的Sheets("ANSWERS") 在哪个工作簿中:wbtarget.Sheets("ANSWERS")

所以应该是这样的:

Option Explicit

Sub Examnew()    
    Dim rCell As Range, rRng As Range             'define loop names    
    Dim wbmaster As Workbook                      'name for master workbook    
    Dim wbtarget As Workbook                      'name for student workbook    
    Set wbmaster = ActiveWorkbook                 'set the name for the master

    'Student numbers in cells B3:B64 WARNING SET TO 2 STUDENTS ONLY FOR TEST
    'NOTE that st Nums are in col B with a duplicate in col A to collect results.

    Set rRng = wbmaster.Sheets("studentlist").Range("B3:B4")

    For Each rCell In rRng 
        Set wbtarget = Workbooks.Open("/Users/tester/Final_V1/" & rCell.Value & ".xlsx") 
        '^ set the open workbook directly to the variable

        wbtarget.Sheets("ANSWERS").Unprotect Password:="Coursework2019"
        '^ you must specify the workbook here!!!

        'Other stuff normally here…   
        wbtarget.Close SaveChanges:=True 'submit parameters without parenthesis!
    Next rCell
End Sub

【讨论】:

  • SomeProcedure(Parameter) 'wrong 如果只有一个参数,强制参数传递 ByVal 而不是 ByRef 是一种有效的语法。
  • @VincentG 哇不知道。感谢您的澄清。如果参数被显式声明为ByRef,它甚至可以工作,例如SomeProcedure(ByRef Parameter)。我认为这仍然很令人困惑,因为这些括号与其他括号不同(在它们的行为上)它有效,因为它与SomeProcedure (Parameter1), (Parameter2)result = SomeProcedure((Parameter1), (Parameter2)) 相同。大多数人不会认识到SomeProcedure (Parameter) 括号前的额外空格,也不会认识到现在有完全不同的行为。 VBA 设计不好。
  • 我将在稍后对其进行编辑和测试......很好奇一些被删除的代码在显然不应该根据我上面的错误时工作......
  • @SolarMike 正如文森特指出的那样,即使使用这些括号它也可以工作,因为您只使用了一个参数,这不是语法错误,而是行为改变ByRef -&gt; ByVal。所以括号在语法上是正确的,但括号的行为并不是你想象的那样。 • 尽管如此,因为在这种特殊情况下,如果参数提交ByValByRef 并没有什么不同,您的版本并没有显示不同的结果。但正确的方法是我在答案中显示的。 • 代码不起作用的实际问题是工作表缺少wbtarget.
  • 嗯,包括了功能正常的密码建议。但是将打开的工作簿直接设置为变量会导致出现“授予访问权限”窗口。回到两个单独的语句,它工作正常......所以,非常感谢。
猜你喜欢
  • 2019-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-07
  • 2011-02-06
相关资源
最近更新 更多