简体   繁体   English

VBA Excel宏打开一个工作簿,但所有内容冻结,因为该文件试图运行自己的宏

[英]VBA Excel Macro Opens a workbook but everything freezes because that file tries to run its own macro

So, I have a macro that is supposed to go to a folder and cycle through each workbook in the folder and pull certain data. 因此,我有一个宏,应该去一个文件夹并循环浏览该文件夹中的每个工作簿并提取某些数据。 The folders of data have files from different years, and the newer files work just fine. 数据文件夹中包含不同年份的文件,并且较新的文件可以正常工作。 However when I open some of the older files, I have a problem. 但是,当我打开一些旧文件时,我遇到了问题。 The older files were saved as .xls files, but when you open them the only worksheet that shows says "EnableMacros", with a warning that the user must change their security settings to enable macros. 较旧的文件另存为.xls文件,但是当您打开它们时,显示的唯一工作表显示为“ EnableMacros”,并警告用户必须更改其安全设置才能启用宏。 Since my options and trust center settings are set to allow macros to run without restriction, when I open the workbook normally the EnableMacros tab shows for a second then is replaced by the normal tabs with the data in them. 由于我的选项和信任中心设置被设置为允许宏不受限制地运行,因此当我正常打开工作簿时,EnableMacros选项卡显示一秒钟,然后被包含数据的常规选项卡替换。 However, when I have my macro open up the files the program simply stops and the opened workbook just shows the EnableMacros tab and wont update to show the worksheets with the data. 但是,当我打开宏打开文件时,程序会停止,打开的工作簿仅显示EnableMacros选项卡,并且不会更新以显示带有数据的工作表。 I don't know how to work around this. 我不知道该如何解决。 And this is all complicated by the fact that I can't change anything in the workbooks I am opening since they are protected. 由于我受到保护,因此我无法更改正在打开的工作簿中的任何内容,因此一切都变得很复杂。 Help? 救命?

I am using the macro provided by The Spreadsheet Guru to go through the files in the folder, with my own modifications to pull the data that I want: 我正在使用Spreadsheet Guru提供的宏来浏览文件夹中的文件,并进行了自己的修改以提取所需的数据:

Sub LoopAllExcelFilesInFolder()
'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them
'SOURCE: www.TheSpreadsheetGuru.com

Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog

'transfer values with range
Dim rngcopySA As Range

'Optimize Macro Speed
  Application.ScreenUpdating = False
  Application.EnableEvents = False
  Application.Calculation = xlCalculationManual

'Retrieve Target Folder Path From User
  Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)

    With FldrPicker
      .Title = "Select A Target Folder"
      .AllowMultiSelect = False
        If .Show <> -1 Then GoTo NextCode
        myPath = .SelectedItems(1) & "\"
    End With

'In Case of Cancel
NextCode:
  myPath = myPath
  If myPath = "" Then GoTo ResetSettings

'Target File Extension (must include wildcard "*")
  myExtension = "*.xls*"

'Target Path with Ending Extention
  myFile = Dir(myPath & myExtension)

'Loop through each Excel file in folder
  Do While myFile <> ""
    'Set variable equal to opened workbook
      Set wb = Workbooks.Open(Filename:=myPath & myFile)

    'Ensure Workbook has opened before moving on to next line of code
      DoEvents

Here is where things break, because the correct worksheet doesn't show up, only the "EnableMacros" worksheet. 这是发生问题的地方,因为没有显示正确的工作表,所以只有“ EnableMacros”工作表。 The error that comes up says "Run-time error '1004': Select method of Worksheet class failed". 出现的错误提示“运行时错误'1004':Worksheet类的选择方法失败”。

    'Copy Range with Membership Data to this file
     wb.Worksheets("Service Areas").Select

      'transfer values with range
      wb.Worksheets("Service Areas").Range("A13:E13").Select
      Set rngcopySA = wb.Worksheets("Service Areas").Range(Selection, Selection.End(xlDown))

      ThisWorkbook.Activate
      Sheets("Old SA Files").Select
      Range("A" & Rows.count).End(xlUp).Offset(1).Resize(rngcopySA.Rows.count, rngcopySA.Columns.count).Cells.Value = rngcopySA.Cells.Value

'Save and Close Workbook
    wb.Close SaveChanges:=True

'Ensure Workbook has closed before moving on to next line of code
  DoEvents

 'Get next file name
   myFile = Dir
 Loop

ResetSettings:
 'Reset Macro Optimization Settings
  Application.EnableEvents = True
  Application.Calculation = xlCalculationAutomatic
  Application.ScreenUpdating = True

End Sub

As I said before, this works just fine for the newer files, it is only the older ones that have that "EnableMacros" tab that have the problem. 正如我之前说过的,这对于较新的文件来说效果很好,只有具有“ EnableMacros”选项卡的较旧文件才有问题。 I have also tried going through each of the old .xls files in a folder and hitting the Convert button to get out of compatibility mode, and so changed all the files to .xlsm (and changed the folder picker code to look for .xlsm files) but that had the exact same problem. 我还尝试遍历文件夹中的每个旧.xls文件,然后单击“转换”按钮退出兼容模式,因此将所有文件更改为.xlsm(并更改了文件夹选择器代码以查找.xlsm文件),但问题完全相同。

The person that has set up the spreadsheet has used xlVeryHidden to stop people from unhiding the sheets without using the macro 设置电子表格的人已使用xlVeryHidden阻止人们取消隐藏表格而不使用宏

To select one of the hidden sheets, you have to make it visible first, and that is simply 要选择其中一个隐藏的工作表,您必须先使其可见,这就是

Sheets("Service Areas").Visible = True 

you should then be able to select the sheet. 然后,您应该能够选择工作表。

Note that even if the sheet is hidden, you can still select the data and copy it, so you could change the code causing an issue to this: 请注意,即使隐藏了工作表,您仍然可以选择数据并复制它,因此您可以更改导致此问题的代码:

'Copy Range with Membership Data to this file
  'transfer values with range
  Set rngcopySA = wb.Worksheets("Service Areas").Range("A13:E13", wb.Worksheets("Service Areas").Range("A13:E13").End(xlDown))

  ThisWorkbook.Sheets("Old SA Files").Range("A" & Rows.count).End(xlUp).Offset(1).Resize(rngcopySA.Rows.count, rngcopySA.Columns.count).Cells.Value = rngcopySA.Cells.Value

note the complete lack of Select which avoids screen flicker and other issues . 请注意,完全没有Select ,可以避免屏幕闪烁和其他问题 See this question for more details on avoiding select. 有关避免选择的更多详细信息,请参见此问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM