Vba Excel将特定数据单元从多个工作簿复制到主文件
我有不同员工姓名的各种工作手册,其中包含不同的项目编号和在这些项目上工作的时间。我试图将这些项目编号过滤到一个主文件(zmaster),其中包含特定项目编号的整行。我需要Excel在目录(包含所有员工工时文件的特定文件夹)中筛选匹配项,并将这些匹配项复制到zmaster文件中。过滤器是主文件的单元格A1(例如链接图片示例中的300000)。图1是主文件,图2是员工工时文件的示例 (一) (二) 此外,如果Excel能够过滤掉重复的内容(例如,第30周,主文件中的工时和员工姓名完全相同,很可能是重复的,应该忽略) 我是Excel vba的新手,发现/调整了以下宏。第一个复制目录中的所有数据并将其放入主文件。第二个过滤掉与单元格A1匹配的projectnumber。但是,这需要两个步骤,当我第二次运行第一个宏时,它还将收集已输入主文件的数据。此外,我的第二个宏将匹配项放置在与它们放置在员工工时文件中的位置相同的行号中,从而删除放置在同一行的主文件中的早期观察结果(例如,projectnumber 100000位于员工工时文件的第2行,因此复制到主文件的第2行,删除主文件的指标行) 第一个宏:Vba Excel将特定数据单元从多个工作簿复制到主文件,vba,excel,Vba,Excel,我有不同员工姓名的各种工作手册,其中包含不同的项目编号和在这些项目上工作的时间。我试图将这些项目编号过滤到一个主文件(zmaster),其中包含特定项目编号的整行。我需要Excel在目录(包含所有员工工时文件的特定文件夹)中筛选匹配项,并将这些匹配项复制到zmaster文件中。过滤器是主文件的单元格A1(例如链接图片示例中的300000)。图1是主文件,图2是员工工时文件的示例 (一) (二) 此外,如果Excel能够过滤掉重复的内容(例如,第30周,主文件中的工时和员工姓名完全相同,很可能是重
Sub LoopThroughDirectory()
Dim MyFile As String
Dim erow
Dim Filepath As String
Filepath = ("C:\test\”)
MyFile = Dir(Filepath)
Do While Len(MyFile) > 0
If MyFile = "zmaster.xlsx" Then
Exit Sub
End If
Workbooks.Open (Filepath & MyFile)
Range("A2:L9").Copy
ActiveWorkbook.Close
erow = Blad1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Paste Destination:=Worksheets("Sheet1").Range(Cells(erow, 1), Cells(erow, 4))
MyFile = Dir
Loop
End Sub
第二个宏:
Sub finddata()
Dim projectnumber As Integer
Dim finalrow As Integer
Dim i As Integer
Sheets("Blad1").Range("A1:H9").ClearContents
projectnumber = Sheets("Blad1").Range("A1").Value
finalrow = Sheets("Blad1").Range("A30").End(x1Up).row
For i = 1 To finalrow
If Cells(i, 1) = projectnumber Then
Range(Cells(i, 1), Cells(i, 12)).Copy
Range("A100").End(x1Up).Offset(1, 0).PasteSpecial x1pasteformulasandnumberformats
End If
Next i
Range("A1").Select
End sub
希望一切都很清楚,并提前感谢!这应该行得通
- 打开目录中的每个文件
- 检查文件名是否不是zmaster,是否包含xlsx
- 运行当前文件中的每一行,然后合并复制到主文件的范围
- 复制到主文件的最后一行加上1,这是第一个空行
Option Explicit Sub CopyToMasterFile() Dim MasterWB As Workbook Dim MasterSht As Worksheet Dim MasterWBShtLstRw As Long Dim FolderPath As String Dim TempFile Dim CurrentWB As Workbook Dim CurrentWBSht As Worksheet Dim CurrentShtLstRw As Long Dim CurrentShtRowRef As Long Dim CopyRange As Range Dim ProjectNumber As String FolderPath = "C:\test\" TempFile = Dir(FolderPath) Dim WkBk As Workbook Dim WkBkIsOpen As Boolean 'Check is zmaster is open already For Each WkBk In Workbooks If WkBk.Name = "zmaster.xlsx" Then WkBkIsOpen = True Next WkBk If WkBkIsOpen Then Set MasterWB = Workbooks("zmaster.xlsx") Set MasterSht = MasterWB.Sheets("Blad1") Else Set MasterWB = Workbooks.Open(FolderPath & "zmaster.xlsx") Set MasterSht = MasterWB.Sheets("Blad1") End If ProjectNumber = MasterSht.Cells(1, 1).Value Do While Len(TempFile) > 0 'Checking that the file is not the master and that it is a xlsx If Not TempFile = "zmaster.xlsx" And InStr(1, TempFile, "xlsx", vbTextCompare) Then Set CopyRange = Nothing 'Note this is the last used Row, next empty row will be this plus 1 With MasterSht MasterWBShtLstRw = .Cells(.Rows.Count, "A").End(xlUp).Row End With Set CurrentWB = Workbooks.Open(FolderPath & TempFile) Set CurrentWBSht = CurrentWB.Sheets("Sheet1") With CurrentWBSht CurrentShtLstRw = .Cells(.Rows.Count, "A").End(xlUp).Row End With For CurrentShtRowRef = 1 To CurrentShtLstRw If CurrentWBSht.Cells(CurrentShtRowRef, "A").Value = ProjectNumber Then 'This is set to copy from Column A to Column L as per the question If CopyRange Is Nothing Then 'If there is nothing in Copy range then union wont work 'so first row of the work sheet needs to set the initial copyrange Set CopyRange = CurrentWBSht.Range("A" & CurrentShtRowRef & _ ":L" & CurrentShtRowRef) Else 'Union is quicker to be able to copy from the sheet once Set CopyRange = Union(CopyRange, _ CurrentWBSht.Range("A" & CurrentShtRowRef & _ ":L" & CurrentShtRowRef)) End If ' ending If CopyRange Is Nothing .... End If ' ending If CurrentWBSht.Cells.... Next CurrentShtRowRef CopyRange.Select 'add 1 to the master file last row to be the next open row CopyRange.Copy MasterSht.Cells(MasterWBShtLstRw + 1, 1) CurrentWB.Close savechanges:=False End If 'ending If Not TempFile = "zmaster.xlsx" And .... TempFile = Dir Loop End Sub