打开工作表,从外接程序执行功能,然后保存 [英] Open a sheet, execute a function from an add-in, and save

查看:41
本文介绍了打开工作表,从外接程序执行功能,然后保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要运行一千个excel文件(.xlsx)才能获取数据.每个文件有很多工作表,并且在每个工作表中,单元格A1包含Excel加载项(Morningstar Excel加载项)的功能.现在,我必须手动打开每个文件.加载加载项时,将执行单元格A1中的功能,并且单元格A1中显示正在处理...".我将不得不等待几秒钟或几分钟才能返回数据.工作表中填满数据后,我会将其另存为csv文件.

I need to runs a thousand of excel files (.xlsx) to get data. Each file has many sheets, and in each sheet, cell A1 contains a function of an Excel add-in (Morningstar Excel add-in). Right now, I have to manually open each file. When the add-in loads, the function in cell A1 gets executed and cell A1 displays "Processing...". I would have to wait a few seconds or minutes for the data to return. Once the sheet is filled with data, I would save it as a csv file.

如何自动执行此过程?

我写了一个宏来打开excel文件并将工作表另存为CSV文件.但是,它绕过了数据请求和下载过程.我添加了等待几秒钟的选项,但Excel文件以冻结状态打开,即未加载加载项并且单元格A1中的功能未运行.我该怎么办:

I have written a macro to open the excel files and save the sheets as CSV files. However, it bypasses the data requesting and downloading process. I added the option to wait a few seconds but the Excel files opens in a frozen status, i.e. the add-in is not loaded and the function in cell A1 does not run. How can I:

  1. 打开文件
  2. 确保加载了加载项
  3. 确保每张工作表的单元格A1中的功能都可以运行
  4. 检查是否有任何数据.一种方法是检查单元格A10是否不为空
  5. 将工作表另存为CSV文件

到目前为止,这是我的代码:

Here is my code so far:

Sub morningstar_VBA()
'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them

Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim filename As String
Dim path_to_save As String
Dim FldrPicker As FileDialog
Dim w As Long

'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 = "*.xlsx*"

'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
    For w = 1 To Worksheets.Count
        With Worksheets(w).Copy
            'the ActiveWorkbook is now the new workbook populated with a copy of the current worksheet
            With ActiveWorkbook
                filename = .Worksheets(1).Name
                path_to_save = "E:\Morningstar_download\test\" & filename
                .SaveAs filename:=path_to_save, FileFormat:=xlCSV
                DoEvents
                .Close savechanges:=False
            End With
        End With
    Next w

    wb.Close savechanges:=True

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

    'Get next file name
    myFile = Dir
  Loop

'Message Box when tasks are completed
  MsgBox "Task Complete!"

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

End Sub

推荐答案

包括以下代码以刷新加载项,即确保运行加载项中的所有功能.不确定其他加载项

include the code below to refresh add-ins, i.e. making sure all functions from add-ins run. Not sure about other add-ins

Set cmd = Application.CommandBars("Cell").Controls("Refresh All")
    cmd.Execute

这篇关于打开工作表,从外接程序执行功能,然后保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆