目录和粘贴到母版图纸的VBA代码
因此,我有以下宏,它从包含多张工作表的工作簿的C列中提取唯一值,并将其粘贴到新页面。我确实意识到他们的问题是另一个类似的问题,但我不理解。有没有办法: 1) 在文件目录中执行此操作 2) 将新值放入主工作表,而不是在每个文件中创建新工作表:目录和粘贴到母版图纸的VBA代码,vba,excel,Vba,Excel,因此,我有以下宏,它从包含多张工作表的工作簿的C列中提取唯一值,并将其粘贴到新页面。我确实意识到他们的问题是另一个类似的问题,但我不理解。有没有办法: 1) 在文件目录中执行此操作 2) 将新值放入主工作表,而不是在每个文件中创建新工作表: Sub extractuniquevalues() Dim wks As Excel.Worksheet Dim wksSummary As Excel.Worksheet '----------------------------------------
Sub extractuniquevalues()
Dim wks As Excel.Worksheet
Dim wksSummary As Excel.Worksheet
'----------------------------------------------------------------------------------
On Error Resume Next
Set wksSummary = Excel.ThisWorkbook.Worksheets("Unique data")
On Error GoTo 0
If wksSummary Is Nothing Then
Set wksSummary = Excel.ThisWorkbook.Worksheets.Add
wksSummary.Name = "Unique data"
End If
'Iterate through all the worksheets, but skip [Summary] worksheet.
For Each wks In Excel.ActiveWorkbook.Worksheets
With wksSummary
If wks.Name <> .Name Then
If Application.WorksheetFunction.CountA(wks.Range("C:C")) Then
Call wks.Range("C:C").AdvancedFilter(xlFilterCopy, , .Cells(.Cells(.Rows.Count, 1).End(xlUp).Row + 1, 1), True)
End If
End If
End With
Next wks
End Sub
Sub-extractuniquevalues()
Dim以Excel格式工作。工作表
将wksSummary设置为Excel.工作表
'----------------------------------------------------------------------------------
出错时继续下一步
设置wksSummary=Excel.ThisWorkbook.Worksheets(“唯一数据”)
错误转到0
如果wksSummary不算什么,那么
设置wksSummary=Excel.ThisWorkbook.Worksheets.Add
wksSummary.Name=“唯一数据”
如果结束
'遍历所有工作表,但跳过[摘要]工作表。
对于Excel.ActiveWorkbook.Worksheets中的每个工作
与Wksummary一起
如果是工作名,那么是工作名
如果是Application.WorksheetFunction.CountA(wks.Range(“C:C”)),那么
调用wks.Range(“C:C”).AdvancedFilter(xlFilterCopy、.Cells(.Rows.Count,1)。End(xlUp)。Row+1,1),True)
如果结束
如果结束
以
接下来的工作
端接头
非常感谢您的帮助。您的两个请求都可以完成:(请参阅我的评论)
可能是@Jonathan的复制品这看起来像你想要的!非常感谢!那么,我唯一要更改的是目录名吗?我在执行这个宏时遇到了问题,对不起,我是一个新手,基本上你可以在上面的例子中更改任何字符串,但唯一必须的是第5行的目录。确保字符串以\结尾,并且目录存在。@Pho3nixHun,谢谢!那么,我是单独运行它们还是同时运行它们?如果是这样的话,我如何一次完成它们呢?
subyoursub(master\u wb作为工作簿,source\u wb作为工作簿)
是您必须修改的内容。在那里,您可以实现从工作簿中获取数据的逻辑master_wb
是您将放置数据的wb,而source_wb
是您从中获取数据的位置。所有其他逻辑由你决定。
Sub Main()
'Turn off alerts like "Do you really want to quit?"
Application.DisplayAlerts = False
Call LoopThroughDirectory("D:\Private\Excel\", "*.xls*")
'Turn alerts on
Application.DisplayAlerts = True
End Sub
Sub LoopThroughDirectory(dirPath As String, filter As String)
Dim filename
'Loop throug all of the files in the given directory
filename = Dir(dirPath & filter)
Do While Len(filename) > 0
' Filename variable contains the name of the file in the directory
' (dirPath & Filename) will be the full path to the file
' Lets call here another Sub which will open up workbooks for us.
OpenAnotherWorkbook (dirPath & filename)
'Move on to the next file
filename = Dir
Loop
End Sub
Sub OpenAnotherWorkbook(filePath As String)
'Your master workbook to copy to
Dim master_wb As Workbook
Set master_wb = ThisWorkbook
'Your source workbook to copy from
Dim source_wb As Workbook
Set source_wb = Application.Workbooks.Open(filePath)
'Call your subroutine
Call YourSub(master_wb, source_wb)
'Close source workbook after everything is done
source_wb.Close
End Sub
Sub YourSub(master_wb As Workbook, source_wb As Workbook)
' Do your stuff here
' For example:
'Find your master sheet
Dim master_ws As Worksheet
Set master_ws = GetOrCreateWorksheet(master_wb, "YourSheetName")
Dim source_ws As Worksheet
Set source_ws = source_wb.Sheets(1)
'Lets save some data from the another workbook to the master one.
Dim lastRowNo As Integer
lastRowNo = master_ws.UsedRange.Rows.Count
'If lastRowNo is 1 that means the worksheet is empty or only the headers had been initialized
If lastRowNo = 1 Then
'Create headers for the columns
master_ws.Cells(lastRowNo, 1).Value = "Workbook"
master_ws.Cells(lastRowNo, 2).Value = "Worksheet"
End If
'Give some value to the next empty row's first and second cell
'Source workbook's name
master_ws.Cells(lastRowNo + 1, 1).Value = source_wb.Name
'Source worksheet's name
master_ws.Cells(lastRowNo + 1, 2).Value = source_ws.Name
End Sub
Function GetOrCreateWorksheet(wb As Workbook, wsName As String) As Worksheet
Dim ws As Worksheet
'Loop through each sheet to find yours
For Each ws In wb.Sheets
If ws.Name = wsName Then
'If found return with it
Set GetOrCreateWorksheet = ws
Exit Function
End If
Next ws
'If not exists, create one and return with it
Set ws = wb.Sheets.Add
ws.Name = wsName
Set GetOrCreateWorksheet = ws
End Function