VBA Excel从模板和列表生成唯一页面 [英] VBA Excel Generate Unique Pages From Template and List
问题描述
我已经为此工作了一段时间(我完全没有VBA经验),但是我一直在收到错误消息,所以我们在这里.
I have been working on this for sometime now (I am not experienced in VBA at all), but I keep getting errors so here we are.
设置:
为简单起见,我在工作簿中有两个工作表.第一个是每日订单",是我所有产品的列表,每行是不同的产品(约1,000),每列表示有关该产品的不同信息(例如ID,成本,重量等).
第二个是模板",是一个定价模板,当给出产品信息时,它将生成一个定价表.
Setup:
For simplicity purposes I have two worksheets in a workbook. The first, "Daily Order", is a list of all my products, each row being a different product (approx. 1,000), each column indicating different information about the product (ie id, cost, weight, etc).
The second, "Template", is a pricing template that, when given product information, will generate a pricing table.
目标: 创建一个VBA宏以遍历每日订单"工作表的每一行,并为每一行制作一份模板工作表并将某些信息附加到新工作表中.
Objective: Create a VBA Macro to loop through each row of the "Daily Order" worksheet and for each row make a copy of the template sheet and append certain information to that new sheet.
什么不起作用:
Sub GeneratePriceBook()
Dim rw As Range
Dim temp As Worksheet
Dim ws As Worksheet
Dim daily As Worksheet
Set daily = Worksheets("Daily Order")
Set temp = Worksheets("Template")
temp.Activate
For Each rw In daily.Rows
temp.Copy After:=Sheets(Sheets.Count)
Set ws = Sheets(Sheets.Count)
ws.Name = rw.Value
With ws
.Range("A6").Formula = "='Daily Order'!B" & rw.Row
.Range("B6").Formula = "='Daily Order'!B" & rw.Row
.Range("A3").Formula = "='Daily Order'!Q" & rw.Row
.Range("E36").Formula = "='Daily Order'!M" & rw.Row
.Range("E36").Formula = "='Daily Order'!Y" & rw.Row
.Range("E37").Formula = "='Daily Order'!L" & rw.Row
End With
Next rw
End Sub
错误类型:
我花了很多时间试图找出某些我认为可能会造成麻烦的区域,但最终还是出现424个错误或1004个错误.
I have spent considerable time trying to isolate out certain areas that I think might be troublesome, but I keep ending up with either 424 errors or 1004 errors.
再次,我将不胜感激. 谢谢!
Again, I would greatly appreciate any help. Thank you!
推荐答案
我认为这是您要寻找的:
I think this is what you're looking for:
Sub GeneratePriceBook()
Dim wsDaily As Worksheet
Dim wsTemp As Worksheet
Dim lVisibility As XlSheetVisibility
Dim strSheetName As String
Dim rIndex As Long
Dim i As Long
Set wsDaily = Sheets("Daily Order")
Set wsTemp = Sheets("Template")
lVisibility = wsTemp.Visible 'In case template sheet is hidden
wsTemp.Visible = xlSheetVisible
For rIndex = 2 To wsDaily.Cells(Rows.Count, "A").End(xlUp).Row
'Ensure valid sheet name
strSheetName = wsDaily.Cells(rIndex, "A").Text
For i = 1 To 7
strSheetName = Replace(strSheetName, Mid(":\/?*[]", i, 1), " ")
Next i
strSheetName = Trim(Left(WorksheetFunction.Trim(strSheetName), 31))
'Make sure the sheet name doesn't already exist
If Not Evaluate("IsRef('" & strSheetName & "'!A1)") Then
wsTemp.Copy After:=Sheets(Sheets.Count)
With Sheets(Sheets.Count)
.Name = strSheetName
.Range("A6").Formula = "='" & wsDaily.Name & "'!B" & rIndex
.Range("B6").Formula = "='" & wsDaily.Name & "'!B" & rIndex 'You are referencing the same cell as in A6?
.Range("A3").Formula = "='" & wsDaily.Name & "'!Q" & rIndex
.Range("E36").Formula = "='" & wsDaily.Name & "'!M" & rIndex
.Range("E36").Formula = "='" & wsDaily.Name & "'!Y" & rIndex 'You are putting a second formula in E36?
.Range("E37").Formula = "='" & wsDaily.Name & "'!L" & rIndex
End With
End If
Next rIndex
wsTemp.Visible = lVisibility 'Set template sheet to its original visible state
Set wsDaily = Nothing
Set wsTemp = Nothing
End Sub
这篇关于VBA Excel从模板和列表生成唯一页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!