【问题标题】:VBA to paste data into existing workbook without specifying workbook name?VBA 将数据粘贴到现有工作簿而不指定工作簿名称?
【发布时间】:2018-06-22 05:54:09
【问题描述】:

我正在创建一个将用作月度报告模板的工作簿(我们称之为“ReportWorkbookTest”),并且正在努力编写或记录一个宏,该宏会将数据从各种未指定的工作簿粘贴到 ReportWorkbookTest 中。

要创建月度报告,数据会从服务器导出到以报告导出日期/时间命名的 .xlsx 文件。因此,将粘贴信息的工作簿的名称将始终具有不同的名称。每月数据导出中的信息列将始终保持不变(列 D:G 和 I)。我已设法为两个指定的工作簿执行此操作,但无法转换为新的每月数据导出。

    Range("I4").Select
Windows("Export 2018-06-21 11.51.34.xlsx").Activate
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=9, Criteria1:= _
    xlFilterLastMonth, Operator:=xlFilterDynamic
Range("D2:G830,I2:I830").Select
Range("I2").Activate
Selection.Copy
Windows("ReportWorkbookTest.xlsm").Activate
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
    xlNone, SkipBlanks:=False, Transpose:=False

有没有办法设置 VBA,以便在运行宏时不需要指定工作簿名称?另外,如果每次导出的行数发生变化,如何指定宏只复制表中的活动行?

谢谢!

【问题讨论】:

  • 您如何设想宏“知道”从哪个文件复制数据?
  • 您需要提示选择文件,因为名称总是会改变,让用户选择文件和或文件,然后工作簿名称就无关紧要了。

标签: vba excel


【解决方案1】:

如果只打开这两个工作簿,您可以使用数字代替名称:

Workbooks(1)
and
Workbooks(2) 

Workbooks(1) 将是第一个打开的文件,更有可能是宏所在的 ReportWorkbookTest.xlsm,因此您可以提供应首先打开此文件的说明。如果打开的不止这两个工作簿,您可以尝试循环方法,这是一个使用示例:

Dim wkb as Workbook
Dim thisWb as Workbook
Dim expWb as Workbook
Set thisWb = ThisWorkbook
For Each wkb in Workbooks
    If wkb.Name Like "Export 2018-*" Then
        expWb = wkb
        Exit For
    End If
Next
If Not expWb Is Nothing Then
    'Found Export, do stuff like copy from expWb to thisWb
    expWb.Worksheets(1).Range("B20:B40").Copy
    thisWb.Sheets("PasteSheet").Range("A3").PasteSpecial xlValues
Else
    'Workbook with Export name not found
End If

【讨论】:

    【解决方案2】:

    这是您的框架,如果您要导入多个文件,那么我建议您使用向导。

    向导框架将是: 1)提示用户选择文件(您可能会检查的某种类型,可以是列名 - 标题) 2)如果它通过验证然后导入数据(并处理它) 2b)如果没有通过报告它不是一个有效的文件并再次提示 3) 提示输入下一个文件类型 ......

    我有一个像这样的项目,每个月需要 4 个不同的数据“转储”并将它们合并到一个摘要工作簿中。

    但是对于更改名称的单个文件,这里是一个框架: 如果只有一个工作表,您可以消除循环遍历所有工作表 您也可能不会将数据附加到已经存在的内容中,但这就是查找新的最后一行的目的。

    Option Explicit
    
    'Sub to get the Current FileName
    Private Sub getFN()
    
        Dim Finfo As String
        Dim FilterIndex As Long
        Dim Title As String
    
        Dim CopyBook As Workbook    'Workbook to copy from
        Dim CopySheet As Worksheet  'Worksheet to copy from
        Dim FN As Variant           'File Name
        Dim wsNum As Double         'worksheet # as you move through the Copy Book
        Dim cwsLastRow As Long      'copy worksheet last row
        Dim mwsLastRow As Long      'master worksheet last row
        Dim masterWS As Worksheet   'thisworkbook, your master worksheet
    
        Dim rngCopy1 As Range
        Dim rngCopy2 As Range
    
        Set masterWS = ThisWorkbook.Worksheets("Master Security Logs")
    
        'Set up file filter
        Finfo = "Excel Files (*.xls*),*.xls*"
        'Set filter index to Excel Files by default in case more are added
        FilterIndex = 1
        ' set Caption for dialogue box
        Title = "Select the Current AP Reconcile Workbook"
    
        'get the Forecast Filename
        FN = Application.GetOpenFilename(Finfo, FilterIndex, Title)
    
        'Handle file Selection
        If FN = False Then
            MsgBox "No file was selected.", vbExclamation, "Not so fast"
        Else
            'Do your Macro tasks here
            'Supress Screen Updating but don't so this until you know your code runs well
            Application.ScreenUpdating = False
    
            'Open the File
            Workbooks.Open (FN)
            'Hide the file so it is out of the way
            Set CopyBook = ActiveWorkbook
    
            For wsNum = 1 To CopyBook.Sheets.Count 'you stated there will be 8, this is safer
                'Do your work here, looks like you are copying certain ranges from each sheet into ThisWorkbook
                CopySheet = CopyBook.Worksheets(wsNum) '1,2,3,4,5,6,7,8
    
                'Finds the lastRow in your Copysheet each time through
                cwsLastRow = CopySheet.Cells(CopySheet.Rows.Count, "A").End(xlUp).Row
    
                'Set your copy ranges
                Set rngCopy1 = CopySheet("D2:D"&cwsLastRow) 'this is your D column
                Set rngCopy2 = CopySheet("I2:I"&cwsLastRow) 'this is your I column
    
                'so you would have to keep tabs on what the lastRow of this sheet is too and always start at +1
                mwsLastRow = masterWS.Cells(masterWS.Rows.Count, "A").End(xlUp).Row
    
                'Copy the ranges in where you want them on the master sheet
                'rngCopy1.Copy destination:= masterWS.Range("D"&mwsLastRow+1)
                'rngCopy2.Copy destination:= masterWS.Range("I"&mwsLastRow+1)
    
                'Clear the clipboard before you go around again
                Application.CutCopyMode = False
            Next wsNum
        End If
    
        'Close the workbook opened for the copy
        CopyBook.Close savechanges:=False 'Not needed now
    
        'Screen Updating Back on
        Application.ScreenUpdating = True
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-25
      • 1970-01-01
      • 2016-08-08
      • 2017-09-08
      • 1970-01-01
      • 1970-01-01
      • 2017-02-19
      • 1970-01-01
      相关资源
      最近更新 更多