仍在学习Excel和VBA,如果代码很可怕,或者这个答案很简单,那么道歉。
我有一组与复选框相关联的工作表;如果选中该框,则它们会隐藏,并且“摘要”中与其关联的行将被取消隐藏,反之亦然。我正在尝试制作一组在“控制”工作表中重命名为特定单元格内容的工作表,然后操作,好像“控制”工作表中的名称一直是他们的名字。我尝试过设置变量,建立范围,每次都失败了。根据“控制”I9中的单元格重新命名现有工作表我需要做什么,然后从那一点开始操作,好像新名称一直是名称一样?
让复选框只是制作一个新工作表,根据需要进行格式化,然后将其命名为用户定义的变量会不会更好?
Application.ScreenUpdating = False
If CheckBox40.Value = True Then
DIM Name AS String
Name = Sheets("Control").Cells(9 "I")
ActiveWorkbook.Unprotect
Sheets("ALL 1").Unprotect
Sheets("SUMMARY").Unprotect
Sheets("ALL 1").Visible = -1
Worksheets("SUMMARY").Rows("25").EntireRow.Hidden = False
Worksheets("SUMMARY").Cells(25, 2).Value = "ALL 1"
Worksheets("SUMMARY").Cells(25, 3).Value = "INTEGRATED AUTOMATION"
Worksheets("SUMMARY").Cells(25, 4).Value = "='Control'!F26"
Worksheets("SUMMARY").Cells(25, 5).Value = "='Control'!G26"
Worksheets("SUMMARY").Cells(25, 6).Value = "='ALL 1'!$H$69"
Worksheets("SUMMARY").Cells(25, 7).Value = "='ALL 1'!$J$69"
Worksheets("SUMMARY").Cells(25, 8).Value = "='ALL 1'!$N$69"
Worksheets("SUMMARY").Cells(25, 9).Value = "='ALL 1'!$P$69"
Worksheets("SUMMARY").Cells(25, 10).Value = "=SUM(F25:I25)/D25"
Worksheets("SUMMARY").Cells(25, 11).Value = "=M25/F3"
Worksheets("SUMMARY").Cells(25, 12).Value = "='ALL 1'!$U$69"
Worksheets("SUMMARY").Cells(25, 13).Value = "=M25/$K$57"
ActiveWorkbook.Protect True, True
Sheets("ALL 1").Protect True, True
Sheets("SUMMARY").Protect True, True
Sheets("ALL 1").Name = Name
End If
If CheckBox40.Value = False Then
ActiveWorkbook.Unprotect
Sheets("ALL 1").Unprotect
Sheets("SUMMARY").Unprotect
Sheets("ALL 1").Visible = 2
Worksheets("SUMMARY").Rows("25").EntireRow.ClearContents
Worksheets("SUMMARY").Rows("25").EntireRow.Hidden = True
ActiveWorkbook.Protect True, True
Sheets("ALL 1").Protect True, True
Sheets("SUMMARY").Protect True, True
Sheets("ALL 1").Name = Name
End If
Application.ScreenUpdating = True
End Sub
答案 0 :(得分:2)
使用工作表变量来保存重命名的工作表,然后使用它来构建公式:
DIM myName AS String 'don't use "name" as the name of a variable, it's a property
Dim ws as worksheet
MyName = Sheets("Control").Cells(9 "I")
set ws = worksheets("All 1") 'point to "All 1"
ws.name = MyName 'rename All 1 to something else
Worksheets("SUMMARY").Cells(25, 12).Value = "='" & ws.name & "'!$U$69"
'keeps pointing to whatever All 1 is now called