[英]VBA - copy different template sheets from a workbook, into multiple sheets of another workbook based on criteria on a summary excel sheet
I'm pretty new to VBA (3 days of exp), I have had a look through several forums but I can't find the solution.我对 VBA 很陌生(3 天的 exp),我浏览了几个论坛,但找不到解决方案。
I have 2 workbooks.我有2本工作簿。 The "master" workbook has a summary sheet with column A - List of names hyperlinked to a blank sheet each in the same workbook, the tabs are labelled the same as the name in the column.
“主”工作簿有一个汇总表,其中列 A - 超链接到同一工作簿中每个空白表的名称列表,选项卡的标签与列中的名称相同。 Column B has 1 or a combination of colour - there is 5 options (red, blue, green, blue & red, or red & green).
B 列有 1 种颜色或颜色组合 - 有 5 种选项(红色、蓝色、绿色、蓝色和红色或红色和绿色)。 I have a separate template workbook that has 5 template sheets each one corresponding to the colour: labelled red, blue, green, blue & red, or red & green.
我有一个单独的模板工作簿,其中有 5 个模板表,每个模板表对应于颜色:标记为红色、蓝色、绿色、蓝色和红色或红色和绿色。
I want a macro that will go through column B of my "master" workbook, and depending on the colour, copy the corresponding template from the template workbook and then go back to the master workbook click through the link in the adjacent column A, which will take it through to an empty sheet and paste the template.我想要一个宏,它将通过我的“主”工作簿的 B 列,并根据颜色,从模板工作簿中复制相应的模板,然后返回到主工作簿,单击相邻 A 列中的链接,将把它带到一个空的工作表并粘贴模板。 This should repeat to go through the entire column.
这应该重复以遍历整个列。
For example :例如 :
I have tried to write a code myself from what was available in other forums, but it only copy pastes onto the first 2 sheets of the "Master" workbook out of 10 sheets that requires the red template.我曾尝试从其他论坛中可用的内容中自己编写代码,但它仅将需要红色模板的 10 张工作簿中的“主”工作簿复制粘贴到前 2 张纸上。 I have only written it for 1 colour criteria so far since no point in adding multiple criteria if 1 isn't working:
到目前为止,我只为 1 个颜色标准编写了它,因为如果 1 不起作用,则添加多个标准没有意义:
Sub Summary()
Dim rng As Range
Dim i As Long
Set rng = Range("B:B")
For Each cell In rng
If cell.Value <> "Red" Then cell.Offset(0, -1).select
ActiveCell.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
Workbooks.Open Filename:= _
"T:\Contracts\Colour Templates.xlsx"
Sheets("Red Template").Select
Cells.Select
Selection.Copy
Windows("Master.xlsx").Activate
ActiveSheet.Range(“A1”).select
ActiveSheet.Paste
Next
End Sub
Okay, so here's some code to get you started.好的,这里有一些代码可以帮助您入门。 I based the names on the code you gave, which is why it was helpful.
我根据您提供的代码命名,这就是它有用的原因。 I've commented this a lot to try and aid your learning, there are only actually about a dozen lines of code!
我已经对此进行了大量评论以尝试帮助您学习,实际上只有大约十几行代码!
Note: this code will likely not work "as is".注意:此代码可能无法“按原样”工作。 Try and adapt it, look at the Object Browser (press
F2
in VBA editor) and documentation (add "MSDN" to Google searches) to help you.尝试并调整它,查看对象浏览器(在 VBA 编辑器中按
F2
)和文档(将“MSDN”添加到 Google 搜索)以帮助您。
Sub Summary()
' Using the with statement means any code phrase started with "." assumes the With bit first
' So ActiveSheet.Range("...") can now become .Range("...")
Dim MasterBook As Workbook
Set MasterBook = ActiveWorkbook
Dim HyperlinkedBook As Workbook
With MasterBook
' Limit the range to column 2 (or "B") in UsedRange
' Looping over the entire column will be crazy long!
Dim rng As Range
Set rng = Intersect(.UsedRange, .Columns(2))
End With
' Open the template book
Dim TemplateBook As Workbook
Set TemplateBook = Workbooks.Open(Filename:="T:\Contracts\Colour Templates.xlsx")
' Dim your loop variable
Dim cell As Range
For Each cell In rng
' Comparing values works here, but if "Red" might just be a
' part of the string, then you may want to look into InStr
If cell.Value = "Red" Then
' Try to avoid using Select
'cell.Offset(0, -1).Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
' You are better off not using hyperlinks if it is an Excel Document. Instead
' if the cell contains the file path, use
Set HyperlinkedBook = Workbooks.Open(Filename:=cell.Offset(0, -1).Value)
' If this is on a network drive, you may have to check if another user has it open.
' This would cause it to be ReadOnly, checked using If myWorkbook.ReadOnly = True Then ...
' Copy entire sheet
TemplateBook.Sheets("Red Template").Copy after:=HyperlinkedBook.Sheets(HyperlinkedBook.Sheets.Count)
' Instead of copying whole sheet, copy UsedRange into blank sheet (copy sheet is better but here for learning)
' HyperlinkedBook.Sheets.Add after:=HyperlinkedBook.Sheets.Count
' TemplateBook.sheets("Red Template").usedrange.copy destination:=masterbook.sheets("PasteIntoThisSheetName").Range("A1")
ElseIf cell.Value = "Blue" Then
' <similar stuff here>
End If
Next cell
End Sub
Use the Macro Recorder to help you learn how to do simple tasks:使用宏记录器帮助您学习如何完成简单的任务:
http://www.excel-easy.com/vba/examples/macro-recorder.html http://www.excel-easy.com/vba/examples/macro-recorder.html
Try to then edit the code, and avoid using Select
:然后尝试编辑代码,并避免使用
Select
:
How to avoid using Select in Excel VBA macros 如何避免在 Excel VBA 宏中使用 Select
I've been trying to get the code to work for the past week with no luck.过去一周我一直试图让代码正常工作,但没有运气。 I tried various modifications, which ends up giving different error codes.
我尝试了各种修改,最终给出了不同的错误代码。 The first Error I was getting was with
Set rng = Intersect(.UsedRange, .Columns(2))
“Object doesn't support this property or method” So then I changed this to just going through the entire column just to see if it would work.我得到的第一个错误是
Set rng = Intersect(.UsedRange, .Columns(2))
“对象不支持这个属性或方法”所以我把它改成只遍历整个列,看看它是否会工作。 Set rng = Range("B:B")
. Set rng = Range("B:B")
。 When I do that then it reads through and I get an error for Set HyperlinkedBook = Workbooks.Open(Filename:=cell.Offset(0, -1).Value)
with the error code: run time error 1004 Sorry we couldn't find 24 James.xlsx.当我这样做时,它会
Set HyperlinkedBook = Workbooks.Open(Filename:=cell.Offset(0, -1).Value)
并且我得到一个错误Set HyperlinkedBook = Workbooks.Open(Filename:=cell.Offset(0, -1).Value)
错误代码:运行时错误 1004 对不起,我们不能找到 24 个 James.xlsx。 Is it possible it was moved, renamed or deleted?”它有可能被移动、重命名或删除吗?” I believe that this line of the code is assuming that the hyperlink should open a different workbook with that name, however this is not the case.
我相信这行代码假设超链接应该打开一个具有该名称的不同工作簿,但事实并非如此。 The hyperlink on the summary sheet links through to other sheets on the same master workbook, only the templates are on a separate book.
汇总表上的超链接链接到同一主工作簿上的其他工作表,只有模板位于单独的工作簿上。 So to overcome this I tried changing this line as well and ended up with the code below, which manages to open the template workbook, and copy just the tab name onto the first sheet and then gives an error for the following line
TemplateBook.Sheets("Red").Copy ActiveSheet.Paste
, saying “subscript out of range”所以为了克服这个问题,我也尝试更改这一行并最终得到下面的代码,它设法打开模板工作簿,并将标签名称复制到第一张纸上,然后为以下行
TemplateBook.Sheets("Red").Copy ActiveSheet.Paste
,说“下标超出范围”
Sub Summary()
Dim MasterBook As Workbook
Set MasterBook = ActiveWorkbook
With MasterBook
Dim rng As Range
Set rng = Range("B:B")
End With
Dim TemplateBook As Workbook
Set TemplateBook = Workbooks.Open(Filename:="C:\Users\Desktop\Example template.xlsx")
Dim cell As Range
For Each cell In rng
If cell.Value = "Red" Then
cell.Offset(0, -1).Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
TemplateBook.Sheets("Red").Copy ActiveSheet.paste
ElseIf cell.Value = "Blue" Then
cell.Offset(0, -1).Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
TemplateBook.Sheets("Blue").Copy ActiveSheet.paste
End If
Next cell
End Sub
I tried several more variations but I just can't get it to copy the correct template, switch back to the master workbook, follow through the link on the summary sheet to the correct sheet (within the same master workbook), and paste the template.我尝试了更多变体,但我无法让它复制正确的模板,切换回主工作簿,按照摘要表上的链接到正确的工作表(在同一个主工作簿中),然后粘贴模板.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.