【问题标题】:Create a macro that extracts information from a temporal sheet创建从临时表中提取信息的宏
【发布时间】:2017-05-07 11:00:16
【问题描述】:

我有一个主表,我必须从另一个表中提取信息,该表是从很久以前为公司创建的程序的 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

我也愿意接受建议,也许是不同的方法。

【问题讨论】:

    标签: excel vba macros


    【解决方案1】:

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

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

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

    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
    

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

    DataColNumber = wb.wks.Range("1:1").Find("Machine", LookIn:=xlValues, lookat:=xlWhole).Column
    

    【讨论】:

    • 谢谢,查找文件所需的时间就像我自己编写公式一样,列名查找器非常完美,非常有用
    【解决方案2】:

    如果文件名的开头始终是“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)"
    

    【讨论】:

      【解决方案3】:

      好吧,我终于搞定了,

      首先创建了这个

      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个值,它不直接使用

      然后我创建了这个,它复制粘贴到主表,它是恒定的,所以名称保留在那里,如果有的话我只是更改名称

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-16
        • 1970-01-01
        • 2011-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多