创建一个从时间表

时间:2016-12-21 22:00:51

标签: excel vba macros

我有一张主表,我必须从另一张表中提取信息,这张表是从很久以前为公司创建的程序的XML文件生成的,但它会自动在excel中打开它。

该文件保存在Programs(x86)文件夹的某个文件夹中,因此名称将始终更改,也不仅仅是一个文件,每次打开新记录时它都会保存在那里

我需要提取的东西是

=Max (Column X:X)
=Count (Y:Y)
=Sum (Y:Y) (From that same last one)

我录制了一个宏,但是对于文件路径,它将获得我录制的宏。

除了手动更改文件路径之外,我不知道如何使其成为其他Excel工作表

我想知道是否有类似的东西(如果还有其他工作表从那里提取,只打开2张以避免代码崩溃?)可以创建,还是其他什么?

此外,这些列将始终具有相同的名称,但不是相同的位置,是否可以使用列的名称引用它?

这是我录制的宏

 Sub test_2()
' test_2 Macro
' asdad
' Keyboard Shortcut: Ctrl+Shift+T
    ActiveCell.Select<br>
    Application.CutCopyMode = False
    ActiveCell.FormulaR1C1 = _
        "=MAX('ExportReport27d8b91d-bafc-4437-a37d-90e53df817f8.htm'!C5)"
    ActiveCell.Offset(0, 1).Range("Table1[[#Headers],[TaxID]]").Select
    ActiveCell.FormulaR1C1 = _
        "=COUNT('ExportReport27d8b91d-bafc-4437-a37d-90e53df817f8.htm'!C11)"
    ActiveCell.Offset(0, 1).Range("Table1[[#Headers],[TaxID]]").Select
    ActiveCell.FormulaR1C1 = _
        "=SUM('ExportReport27d8b91d-bafc-4437-a37d-90e53df817f8.htm'!C11)"
    ActiveCell.Offset(1, 0).Range("Table1[[#Headers],[TaxID]]").Select
End Sub

我也愿意接受建议,也许是另一种方法。

3 个答案:

答案 0 :(得分:0)

可以打开FileDialog并让用户导航到该文件。你会感兴趣吗?这里的代码示例:

https://msdn.microsoft.com/en-us/library/office/ff865217.aspx

通过设置对话框的参数以允许用户从PDF文件中选择,我将上述内容改编为我的一个项目。这是我的修改版本:

plugins: [
    new webpack.DefinePlugin({
      'process.env': {
        // This has effect on the react lib size
        'NODE_ENV': JSON.stringify('production'),
      }
    }),
    new ExtractTextPlugin("bundle.css", {allChunks: false}),
    new webpack.optimize.AggressiveMergingPlugin(),
    new webpack.optimize.OccurrenceOrderPlugin(),
    new webpack.optimize.DedupePlugin(),
    new webpack.optimize.UglifyJsPlugin({
      mangle: true,
      compress: {
        warnings: false, // Suppress uglification warnings
        pure_getters: true,
        unsafe: true,
        unsafe_comps: true,
        screw_ie8: true
      },
      output: {
        comments: false,
      },
      exclude: [/\.min\.js$/gi] // skip pre-minified libs
    }),
    new webpack.IgnorePlugin(/^\.\/locale$/, [/moment$/]), //https://stackoverflow.com/questions/25384360/how-to-prevent-moment-js-from-loading-locales-with-webpack
    new CompressionPlugin({
      asset: "[path].gz[query]",
      algorithm: "gzip",
      test: /\.js$|\.css$|\.html$/,
      threshold: 10240,
      minRatio: 0
    })
  ],

可以根据列名称或标题行查找列。您可以使用Find方法获取range.column,这是我的一个项目中的一行:

Function PDFLocator(InitialPath As String) as String
    'Declare a variable as a FileDialog object.
    Dim fd As FileDialog

    'Create a FileDialog object as a File Picker dialog box.
    Set fd = Application.FileDialog(msoFileDialogFilePicker)

    'Declare a variable to contain the path
    'of each selected item. Even though the path is aString,
    'the variable must be a Variant because For Each...Next
    'routines only work with Variants and Objects.
    Dim vrtSelectedItem As Variant

    'Use a With...End With block to reference the FileDialog object.
    With fd
        ' Set up the dialog box
        .AllowMultiSelect = False
        .ButtonName = "S&elect"
        .Filters.Clear
        .Filters.Add "Scanned Document", "*.pdf", 1
        .Title = "File Dialog Box"
        .InitialFileName = InitialPath
        .InitialView = msoFileDialogViewDetails

        'Use the Show method to display the File Picker dialog box and return the user's action.
        'The user pressed the button.
        If .Show = -1 Then

            'Step through each string in the FileDialogSelectedItems collection.
            For Each vrtSelectedItem In .SelectedItems

                'vrtSelectedItem is aString that contains the path of each selected item.
                'You can use any file I/O functions that you want to work with this path.
                'This example displays the path in a message box.
                'MsgBox "The path is: " & vrtSelectedItem
                PDFLocator = vrtSelectedItem

            Next vrtSelectedItem
        'The user pressed Cancel.
        Else
        End If
    End With

    'Set the object variable to Nothing.
    Set fd = Nothing
End Function

答案 1 :(得分:0)

如果文件名的开头始终是“ExportReport”,并且该文件夹中只有一个“ExportReport”文件,则可以遍历该文件夹中的所有文件,只查找该文件夹中的所有文件。文件名。

Function GetFileName() as String
    Dim MyObj As Object, MySource As Object, file As Variant
   file = Dir("c:\[path to Programs(x86)]\")
   While (file <> "")
      If InStr(file, "ExportReport") > 0 Then 'This is looking for the string "ExportReport" in each filename, and will exit when it finds a file that matches.
         GetFileName = file
         Exit Sub
      End If
     file = Dir
  Wend
End Function

然后您可以使用此输出代替您的文件名。例如:

filename = GetFileName
ActiveCell.FormulaR1C1 = _
        "=MAX('" & filename & "'!C5)"

答案 2 :(得分:0)

好的,所以我终于开始工作了,

首先创建了这个

Sub ExtractInfoOnTemp()
'
' Macro1 Macro
' Extracts "Max","Count" & "Sum" From Temporal File
'
' Keyboard Shortcut: Ctrl+Shift+T
'
ActiveCell.FormulaR1C1 = "=MAX(C[4])"
 Range("A1").Select

Selection.NumberFormat = "m/d/yyyy"
 Range("B1").Select

ActiveCell.FormulaR1C1 = "=COUNT(C[9])"
  Range("A1").Select

Selection.End(xlToLeft).Select
    Range("C1").Select

ActiveCell.FormulaR1C1 = "=SUM(C[8])"
  Range("C1").Select

Selection.Style = "Currency"
Selection.End(xlToLeft).Select

End Sub

这提取了第一行正在处理的工作表上的数据,只有3个值,它没有直接使用

然后我创建了这个,将其复制粘贴到Master工作表,on是常量,因此名称保留在那里,如果有的话我只需更改名称

Sub PrintInfoInMaster()
CurWkbk = ActiveWorkbook.Name
Dim c
Dim example As Range

Set example = Range("A1:C4")
Application.Run "PERSONAL.XLSB!ExtractInfoOnTemp"
ActiveCell.Range("A1:C1").Select
Selection.Copy
Windows("AllNonParSummary - MASTER.xlsx").Activate

'This is to look for the first empy cel from top to botom and select it     to paste the info
For Each c In Range("H:H").Cells 
If c = "" Then
c.Select
 Exit For
    End If

Next
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
ActiveCell.Offset(0, 1).Range("Table1[[#Headers],[ProviderLastName]]").Select
Windows(CurWkbk).Activate

End Sub