我正在复制工作表并使用第3张表格中的单元格值重命名它。我唯一的问题是如何将新工作表声明为变量,因为我将在该工作表上工作?我在最后一行收到错误说“期望:结束声明”。
Dim wsNew As Worksheet
Dim wsIntro As Worksheet
Dim wsUp As Worksheet
Set wsUp = Worksheets("Sheet1")
Set wsIntro = Worksheets("Instructions")
Worksheets("Sheet1").Copy after:=Sheets(Worksheets.Count)
With ActiveSheet.UsedRange
.Value = .Value
End With
ActiveSheet.name = wsIntro.Range("b6").Value & wsIntro.Range("b7").Value
Dim wsAllo As Worksheet
Set wsAllo = "wsIntro.Range("b6").Value & wsIntro.Range("b7").Value"
答案 0 :(得分:2)
由于您尝试设置引用的工作表是ActiveSheet
,您只需更改
Set wsAllo = "wsIntro.Range("b6").Value & wsIntro.Range("b7").Value"
到
Set wsAllo = ActiveSheet
稍微重构您的代码会给出:
Dim wsNew As Worksheet
Dim wsIntro As Worksheet
Dim wsUp As Worksheet
Dim wsAllo As Worksheet
Set wsUp = Worksheets("Sheet1")
Set wsIntro = Worksheets("Instructions")
'You shouldn't use "Sheets(Worksheets.Count)" - it will sometimes not do
'what you expect (when you have Charts as well as Worksheets in the Workbook)
'Use either "Sheets(Sheets.Count)" to place the new sheet as the last sheet
'in the workbook or use "Worksheets(Worksheets.Count)" to place the new sheet
'after the last worksheet in the workbook (but possibly with Charts after that)
wsUp.Copy After:=Sheets(Sheets.Count)
Set wsAllo = ActiveSheet
With wsAllo
.Name = wsIntro.Range("b6").Value & wsIntro.Range("b7").Value
.UsedRange.Value = .UsedRange.Value
End With