简体   繁体   English

在查找和替换单元格值时重命名刚刚在循环中制作的表格

[英]Renaming Sheets just made within loops while finding and replacing cell value

My code currently creates 9 sheets.我的代码目前创建了 9 张纸。 It's to equal the number of cells in row 1 starting at column C in the 'LLP Disc Sheet', which is 9. This number changes whenever the user deletes cells in that row or add more cells.它等于“LLP 光盘表”中从 C 列开始的第 1 行中的单元格数,即 9。每当用户删除该行中的单元格或添加更多单元格时,此数字就会更改。

I now want to rename each sheet produced (the 9 sheets) to equal a cell value in the first row of the 'LLP Disc Sheet'.我现在想将生成的每张纸(9 张)重命名为等于“LLP 光盘表”第一行中的单元格值。 For instance, one sheet will be named 77777 (C1 in 'LLP Disc Sheet') the other sheet is 66666 (D1 in 'LLP Disc Sheet') , 55555 (E1 in 'LLP Disc Sheet') , and so on.例如,一张纸将命名为 77777(“LLP 光盘表”中的 C1),另一张纸将命名为 66666(“LLP 光盘表”中的 D1)、55555(“LLP 光盘表”中的 E1),依此类推。 I keep getting an error whenever I tried to do ActiveSheet.rename每当我尝试执行 ActiveSheet.rename 时,我都会收到错误消息

Lastly, I want to change B1's formula in each sheet of the 9 sheets produced to match the name of the sheet.最后,我想在生成的 9 张纸中的每张纸中更改 B1 的公式以匹配工作表的名称。 The 9 sheets produced are copies of the "MasterCalculator" So I tried to do find/replace and still had an error.生成的 9 张纸是“MasterCalculator”的副本,所以我尝试查找/替换,但仍然有错误。

Set rng = ws.Range("B1") 'initialize the target range variable
rng.Replace "=+'LLP Disc Sheet'!C", "=+'LLP Disc Sheet'!D"  'but I want to somehow loop it so it continues to K, because that's how many sheets there ar

Module 1模块一

Option Explicit
Sub NewSheets()
Dim i As Integer
Dim ws As Worksheet
Dim sh As Worksheet
Set ws = Sheets("MasterCalculator")
Set sh = Sheets("LLP Disc Sheet")
Application.ScreenUpdating = 0
For i = 2 To Range("A1:Z1").Cells.SpecialCells(xlCellTypeConstants).Count
Sheets("MasterCalculator").Copy After:=sh
Next I
End Sub

Does this work for the first bit?这对第一位有用吗?

You should add some error trapping in case there are no constants (and might need a check for valid sheet names).如果没有常量(并且可能需要检查有效的工作表名称),您应该添加一些错误捕获。

For the formula bit, sounds like you can replace the original sheet name with sh.name ?对于公式位,听起来您可以用sh.name替换原始工作表名称?

Sub NewSheets()

Dim i As Long
Dim ws As Worksheet
Dim sh As Worksheet

Set ws = Sheets("MasterCalculator")
Set sh = Sheets("LLP Disc Sheet")
Application.ScreenUpdating = 0

With ws.Range("A1:Z1").Cells.SpecialCells(xlCellTypeConstants)
   For i = 2 To .Count
       ws.Copy After:=sh
       sh.Name = .Cells(i).Value
   Next i
End With

Application.ScreenUpdating = 1

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM